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] <javascript:>. > 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/d56d4494-ef56-41ab-a8bd-72913bc32213%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
