gleam-lang / suggestions

📙 A place for ideas and feedback
26 stars 2 forks source link

Atom constants #98

Closed lpil closed 3 years ago

lpil commented 4 years ago

It would be useful to be able to create other string like literals such as atoms.

One option would be to have the const syntax convert a string literal into an atom for use elsewhere in the program. This would work by having const check the annotation, and then converting the string into an atom if the annotation is Atom

We would need to promote Atom into the prelude from the stdlib for this.

Syntax suggestions

const ok_atom: Atom = derive Atom("ok")

This matches the derive syntax suggested in gleam-lang/gleam#565

sharno commented 3 years ago

Given that atoms are really common in the ecosystem, do you see any downsides for having simpler syntactic support for atoms like Elixir’s :atom for example?

lpil commented 3 years ago

They're common in the ecosystem but I'm not sure they should be common in Gleam. They're basically strings so a custom type is normally more precise and more safe in Gleam APIs, so much so I'm still not sure about this feature here.

We can always add it later, it is much harder to remove a feature!

sharno commented 3 years ago

Indeed, I forgot about how types map to atoms. So, in general it's much better to depend on defining custom types instead of calling atom.from_string