Unexpected Lists in Elixir

Well - unexpected to me at the time, though part of the language.

I've been putting something together in Phoenix, and one of the pages kept throwing an error that it couldn't find item_path/3. This was odd, as the generating code in the template had four arguments.

Same happened when I broke into IEx to poke at it:

iex(1)> Routes.page_path(conn, :index, user_id: user.id, item_id: item.id)
** (UndefinedFunctionError) function AppWeb.Router.Helpers.item_path/3 is undefined or private. Did you mean one of:

  * item_path/4
  * item_path/5

People familiar with Elixir can already spot the problem I imagine...

In Elixir, key: value, key: value forms what's known as a keyword list. What I hadn't expected was that the language would automatically convert those last two arguments into a keyword list. Apparently it did:

iex(2)> i one: 1, two: 2
Term
  [one: 1, two: 2]
Data type
  List

Remove the keywords and everything works fine.

iex(3)> Routes.item_path(conn, :index, user.id, item.id)
"/users/1/item/2"

Until the next bug at least. :-) Too much time recently writing JavaScript and reviewing Python I think!

Subscribe to Paul Walker

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe