[erlang-questions] Why should you ever use atoms?
Sat Oct 10 19:17:11 CEST 2015
On 2015年10月10日 土曜日 11:46:20 Thomas Gebert wrote:
> I know this is probably kind of a newbie question, but I figured this
> would be the place to ask it: if atoms aren't garbage collected, why
> should I use them?
> I could be way off here, but wouldn't the string be garbage collected?
> Is there a benefit to atoms that I'm missing?
[Spoiler: This is a non-answer, in the sense that I urge you to discover the answer yourself through experience with the environment.]
This reminds me of me about 7 years ago, before I had been able to give myself any time with languages that featured atoms.
There are *many* good reasons for having atoms (it would be interesting if ROK shows up and enumerates a few of them -- pun noted, to include probable demolition of the pun). If you have ever written anything in C, used databases with serious features, or messed around with very basic hash-like data structures you will instantly appreciate the utility of enums and enum-like data. Even if you haven't, though, just write a few programs in Erlang and you will discover on your own why atoms are useful.
At first you must simply have faith that they are useful enough that people we assume to have deeper knowledge than ourselves saw fit to include them. Until we find a pattern of cases that invalidate that assumption we should just go with it. Life is too short to start second guessing people who have produced demonstrably useful things we wish to use until we actually know better (as opposed to pretending that we do).
Anyway, without getting even more preachy than this response already is, just go with the flow for now, use atoms where they seem appropriate, and your sense of "appropriate" and "common use case" will mature. At that point you will suddenly realize why this is useful. For me, at least, this sort of experience has more impact than an academic explanation does initially -- after a bit of time getting used to the mentality, though, the academic explanations become very obviously compelling (and suddenly you will appreciate two things: weird features *and* academic explanations).
At the very least, who wants to write `<<"something">>` when they can write `something` instead? There is a very clear semantic case for having a data type that "is its own value".
More information about the erlang-questions