Hacker News new | ask | show | jobs
by rkangel 1534 days ago
Elixir (and Erlang) have atoms which are exactly the same thing. They're useful in any dynamic programming language - in a static one, the equivalent is different values of an enum.

Python notably doesn't, and as such you get functions that take arguments that are strings with special meaning, which I always found a bit clunky even before I discovered Ruby.

2 comments

I think Erlang has them for a more specific reason: in general it eschews all forms of data definition (records are just a fancy syntax for tuples with an atom at the start), which makes hot code reloading and transparent network communication simpler.

Both cases would require some synchronization of data structures (across time or over the network), and with user-defined types this can get complicated, and atoms make the lack of user-defined types much more pleasant (and more performant than strings).

Python has had enums [1] since Python 3.4 (2014). You can easily convert back and forth between enum values and their string and numeric values.

I don't know anything about Ruby or Erlang so I don't know if that's really relevant, just your comment seems to imply it doesn't.

[1] https://docs.python.org/3/library/enum.html

Yes, that's fair. I suppose I was describing my process of "discovery" and learning with Python which was significantly before 2014. Even now though, enums are not usually the normal way of doing things in public APIs, but that's presumably at least in part because of the history.
Also, unlike Ruby, string literals are usually interned in CPython (I think below a certain size), so they have at least some of the performance benefits of symbols in Ruby.