Think of it this way, an Atom is just an interned/flyweight string. In essence you can think of it as that anytime an atom is put in code or so it is looked up in a global table to get a unique integer that points to it in that table, think of it as the index to that string in the table, then you use the 'atom', which is actually that integer index, everywhere instead. Doing something like `String.to_existing_atom(...)` just performs a lookup in that table for the string, if it does not exist it raises the exception, if it does exist then it returns the index/atom. Doing something like `String.to_atom(...)` performs a lookup in that table, if it exists it returns it, if not then it makes a new entry. Regardless, you cannot have duplicates as it is conceptually similar to a BiMap (in C++ terms at least), thus any atom of a string is the same as any other atom of that string.
On Wednesday, April 11, 2018 at 11:39:48 AM UTC-6, Onorio Catenacci wrote: > > Hmm. I guess I just hadn't considered the behavior of to_atom in the case > of an existing atom. Now that you explain it that way, that makes perfect > sense. > > Thanks! > > > On Wednesday, April 11, 2018 at 1:05:45 PM UTC-4, Justin Wood wrote: >> >> You should be able to use `String.to_atom/1` just fine for your use case. >> It will not actually create a new atom if it is already in your system. >> >> Or am I missing something about your use case? >> >> Justin >> >> On 2018-04-11 12:59, Onorio Catenacci wrote: >> >> Hi all, >> >> I ran across a case where I wanted to try to convert a string to an >> existing atom or create a new one if there is no such atom. So I hacked >> this together: >> >> defmodule String.Extensions do >> def to_existing_or_new_atom(s) when is_binary(s) do >> a = >> try do >> String.to_existing_atom(s) >> rescue >> ArgumentError -> String.to_atom(s) >> end >> end >> end >> >> I find it hard to believe that I'm the first person to come across this >> issue and so I'm inclined to think there's a good reason that this isn't >> already part of the string module. I'm guessing that probably we want to >> see if we try to convert to an existing atom and it's not there. >> >> So: >> >> a.) Would this be a welcome PR? >> >> b.) Assuming it wouldn't am I correct in assuming we want the exception >> thrown if we try to convert to a non-existent atom? As I say this seems so >> obvious I find it very hard to believe I'm the first one to run across >> this. I did a quick search but didn't really turn anything up on this. >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "elixir-lang-core" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/elixir-lang-core/cb21dfeb-154f-486e-941c-12e2d0bc19e4%40googlegroups.com >> >> <https://groups.google.com/d/msgid/elixir-lang-core/cb21dfeb-154f-486e-941c-12e2d0bc19e4%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> >> >> -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/6c328107-1cd9-41e1-b808-a9d2cd7cb423%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
