to_existing_or_new_atom

46 views
Skip to first unread message

Onorio Catenacci

unread,
Apr 11, 2018, 12:59:57 PM4/11/18
to elixir-lang-core
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.


Justin Wood

unread,
Apr 11, 2018, 1:05:45 PM4/11/18
to elixir-l...@googlegroups.com

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

--
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/cb21dfeb-154f-486e-941c-12e2d0bc19e4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Onorio Catenacci

unread,
Apr 11, 2018, 1:39:48 PM4/11/18
to elixir-lang-core
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!

OvermindDL1

unread,
Apr 11, 2018, 4:52:22 PM4/11/18
to elixir-lang-core
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.
Reply all
Reply to author
Forward
0 new messages