josevalim
Convert 'charlists' into ~c"charlists"
Hi everyone,
We are considering deprecating 'charlists' in Elixir in favor of ~c"charlist". In many languages, 'foobar' is equivalent to "foobar", that’s not the case in Elixir and we believe it leads to confusion. If we were to deprecate this functionality, users would get a warning/error upfront about the usage of single-quotes.
The first step in this journey is to change the formatter to rewrite 'charlist' into ~c"charlist". This will basically make the migration painless for most developers.
With this in mind, @sabiwara has submitted an awesome PR that implements this change in the Elixir codebase. Perhaps, more importantly, he has also simulated PRs to important projects to access the impact of this change. Here they are:
- phoenix: charlists_as_sigils · sabiwara/phoenix@75ba56c · GitHub
- ecto: charlists_as_sigils · sabiwara/ecto@aaf55d1 · GitHub
- plug: Comparing elixir-plug:main...sabiwara:check-charlists-as-sigils · elixir-plug/plug · GitHub
- gettext: Comparing elixir-gettext:main...sabiwara:check-charlists-as-sigils · elixir-gettext/gettext · GitHub
- postgrex: Comparing elixir-ecto:master...sabiwara:check-charlists-as-sigils · elixir-ecto/postgrex · GitHub
- phoenix_html: https://github.com/phoenixframework/phoenix_html/compare/master...sabiwara:phoenix_html:check-charlists-as-sigils?expand=1
- mint: Comparing elixir-mint:main...sabiwara:check-charlists-as-sigils · elixir-mint/mint · GitHub
As you can see, the changes are minimal to most projects, including postgrex and mint which integrate with OTP libraries. So from a compatibility point of view, this change is relatively straight-forward.
However, should we go ahead with this change? There would be two reasons for such:
-
If single quotes raised/warned in the future, would that lead to better learning and user experience?
-
If
[97, 98, 99]prints as~c"abc"in IEx instead of'abc', is that more, less, or equally confusing? (let’s assume for now that we will either print one or the other, in order to avoid side-tracking the discussion)
Thoughts are welcome!
Most Liked
benwilson512
I can count on one hand the number of times when I wanted a list of printable ints to be shown as a charlist in the ~7 years I’ve been doing Elixir. Also in that time I’ve answered why it does that countless times to new people and in not one of those cases were they interacting with Erlang and needed it to be printed as text. I think the time has come to legitimately question whether this should still be the default given how easy it is to opt in to the behavior.
Love the change.
olivermt
A thousand times yes, also for noobs trying to speak to erlang libs.
Eiji
Amazing! ![]()
Definitely!
Personally I would like to vote for ~c(abc) instead. This would make many beginners think that ~c is “some kind of function”, so significant % of those should take a look at Kernel documentation at start before asking on forum/slack etc. sigil_c documentation would be then a “core/starting point of understanding the charlists”.
I believe we need to enhance said sigil_c documentation a lot. Even if it would not describes charlists in this @doc it still should point to a separate guide describing “what and why” is it.
LostKobrakai
I’d expect this to be a useful change. Raising the signal of differenciating binaries from charlists should be a net positive. Though I’m not sure about the use of double quotes as the sigil delimiters. They might be more confusing to people unaware of charlists, but I’d probably prefer them as someone aware of them.
I don’t think we can get rid of the need of being aware of charlists and what they are/how they work. So the best we can do is make people aware of them earlier, rather than later, at dev/built time, rather than in production. This change would support that.
I’d argue both are equally confusing. But I don’t think they’re equal in helping people to resolve that confusion.
To someone unaware of charlists and the fact that single quote are not interchangable with double quotes in elixir 'abc' doesn’t hold any signal of “this is something you don’t know about yet”. At best they wonder why there’s single quotes suddenly, but from my experience that doesn’t help much without knowing the significance of that. ~c"abc" on the other hand does hold more signal, even if the person might still wonder why there’s a sigil in front of the “string”. It hopefully leads them to learning about sigils (if unknown before) and sigil_c docs will lead them to learn about charlists.
Having provided explanations to many on slack as well as having tried to improve the documentation of charlists a few years ago I still feel like the most difficult part of explaining charlists is the fact that formatting a charlists as text is only a matter of representation of a list of integers, but it doesn’t mean it’s not a list of integers anymore. Using sigils hopefully makes this less mind bending, given other sigils are also used to represent a certain value via a different representation of it. ~D[2022-08-10] is a textual representation of a date struct, ~r/\d*/ defines a regex struct via its text format, …. At best people might have been in contact with sigils before, but given how many code challenges/katas/… accidentally produce charlists this is certainly nothing to rely on.
josevalim
This is not a breaking change. Old code will still work fine, albeit with a warning, and effective support for single quotes would be removed only in 2.0, which would be several years from now (if it happens).







