arcanemachine
Should the Elixir formatter allow trailing commas?
I saw this topic getting some traction over on the Erlang Forums, so I want to resurrect this topic here (previous discussion here):
Should the Elixir formatter allow trailing commas?
- Yes
- No
0
voters
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
Hi everyone :waving_hand:
Posting here to showcase and announce that Metamorphic is now officially live on a public-facing domain at htt...
New
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project.
My initial shotgu...
New
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
Just a general thread to post chat/news/info relating to AI/ML stuff that may be relevant for Nx now or in the future. Got anything to sh...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Introducing AshStorage! Attachment and file management that slots directly into your resources :smiling_face_with_sunglasses:
I had hope...
New
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










First 10 of 41 Posts!
arcanemachine
I copied this from my post on the Erlang Forums, since it’s one of my favorite side-effects of permitting trailing commas:
I’m not here to extol the virtues of Javascript, but they did get one thing very right by allowing optional trailing commas.
As mentioned by others, this allows for simplified diffs, but one major benefit IMO is that some of the auto-formatters (e.g. Prettier) allow the trailing comma to be used to define how a (short) list of items is formatted.
Given a short list of items:
a = [1, 2, 3]If a trailing comma is placed at the end of a list, e.g.:
[1, 2, 3,]Then the formatter will automatically split it into multiple lines when it is applied (e.g. on save, if auto-format-on-save is enabled):
(This looks silly for a short list like this, but it demonstrates the point. It is nice to use for more complex data structures.)
Conversely, if the trailing comma is removed, then shorter lines will automatically be merged back to a single line. Longer lines will be split into multiple lines as needed (to maintain the configured max line length), and will have the trailing comma, as one would expect.
This feature is nested as well, so you have the ability to determine which structures can be placed on one line, and which should be split up into multiple lines:
It’s a small syntactic difference that can lead to much more readable code, IMO.
Elixir removes trailing commas by default (a mistake IMO, as it precludes the ability to apply the aforementioned formatter trick), but there is a custom formatter called Freedom Formatter which apparently implements this feature. (I have not used it myself since I prefer to stick to conventional code styles when possible, even if I prefer the customization.)
D4no0
For some reason I always thought that the trailing comma was always removed as it was causing a compilation error
.
arcanemachine
I think in macros, that is true, but not for lists, maps, or other collections. So that would be one thing consider if that is the case.
EDIT: The code examples in my first post all execute successfully in IEx.
Indeed, a macro with a trailing comma will raise a compilation error. For example, this code:
produces this error:
dimitarvp
Rust’s formatter even forcibly adds a trailing comma at places (IIRC) and I love that. Simpler diffs indeed (not in long
useblocks that get auto-wrapped by the same formatter however; those are unsalvageable).So yeah I’m all for that, though I’m extremely skeptical that Elixir will have core language changes at this point. Even if it’s an optional thing that does look simple to us but likely carries a huge price to implement well.
lud
This is because you do not have the square brackets. The parser does not know if some call is a macro.
+1 for trailing commas. They are already supported by the language, so no core change here. Just the behaviour of the formatter to change. Except for function calls but that’s far less useful IMO.
Having the formatter not removing them would already be a great change.
sodapopcan
No because I need clarification. There are some good points about inline vs not in the linked thread. I don’t particularly like
[1, 2, 3,]and especially notfoo("bar",). Also, I’m pretty indifferent to them in general. I appreciate them very much for diffs but I enjoy not having them when reading. I’d almost prefer this be solved in git, kinda like how you can ignore whitespace in blames (and all the various other blame options).arcanemachine
There’s a difference between supporting trailing commas, and enforcing them.
Yeah, that would be ugly and unnecessary IMO (as a one-liner).
sodapopcan
Well solid “no” then since I like that it’s not configurable. I don’t even like that line length is configurable. I’d be fine with all or nothing but that would require enforcing syntax in the scenarios that is problematic for not currently supporting them. Though I wouldn’t cry about it if it became an option.
I also realize I didn’t fully read your second post before responding
arcanemachine
I don’t think it would need to be configurable (unless I’m missing something)… it could just permit trailing commas at the end. Both
[1, 2, 3]and[1, 2, 3,]would be untouched by the formatter. Presumably, this same could work for single-line and multi-line statements as well. It would be up to the user whether or not to use them.I also prefer to stick to formatter defaults so I’m with you there.
Good point. When I add the square brackets, the trailing comma works (as long as I disable my editor’s format-on-save feature).
Right, I hadn’t thought about trailing commas there either, but I wouldn’t be in a rush to support that.
xpg
This is funny timing. I had this discussion with a couple of colleagues on Friday.
Personally I have no strong preference, but it seems that people that have been using Typescript or Rust are quite fond of those trailing commas.
I would prefer that the formatter neither adds or removes trailing commas, as to leave it up to the author.
But I also know that this may lead to quite a few “we need to agree on how to do this”-discussions, as there are different preferences.
Currently, the formatter makes a choice, which we can disagree with, but it is consistent. If it allows both, codebases will be more inconsistently formatted.
I guess the interesting thing is how many people to prefer the default to be changed from removing trailing commas to adding them. Make half developer happy, the other half unhappy?
Last Post!
sodapopcan
I don’t even know how those play into LSP as I don’t use nvim. There is a great LSP for vim9 that isn’t hard to configure (it could be simpler but it’s just a copy/paste) but it’s more like you said which is integrating it with my ingrained workflow. All I really care about from LSP is rock-solid jump to definition and I have yet to work on project big enough (in the era of LSP) where tags aren’t sufficient (and even just having the runtime path configured properly makes
gfenough in a lot of cases) soooo I see little point adding more daemons onto the pile unless totally necessary. Also, aren’t we just gonna have computers write our code for us now? Do we even need editors at all?(sorry getting off-topic)