DEV Community

Paul Walker
Paul Walker

Posted on • Originally published at solarwinter.net on

1

Unexpected Lists

Well - unexpected to me, 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 it 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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Remove the keywords and everything works fine.

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

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

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay