1. 19

  2. 6

    Does Elixir not support binary_to_existing_atom or list_to_existing_atom? I’ve always seen / used those functions for working with outside data. I’m also surprised that this article does not touch on records, when I played with Elixir I had to deal with them heavily.

    1. 5

      Elixir has String.to_existing_atom/1, but it requires the user to catch ArgumentError in the case the atom does not already exist. I prefer exception handling to be reserved for truly exceptional situations, and anyway, it’s easier to use strings as keys.

      I don’t mention records in the article by name, but technically, a keyword list is a list of two-element records. This post doesn’t deal with records in specific because it’s focused on key-value data structures.

      Thanks for your comments!

      1. 1

        Sure, those are in :erlang:

        Interactive Elixir (1.2.1) - press Ctrl+C to exit (type h() ENTER for help)
        iex(1)> :erlang.binary_to_existing_atom "Elixir", :utf8
        iex(2)> :erlang.binary_to_existing_atom "foobar", :utf8
        ** (ArgumentError) argument error
            :erlang.binary_to_existing_atom("foobar", :utf8)