0

I am struggling with Elixir's List, Tuple, Keyword List etc. Whats wrong with this list?

This works

iex> [1, one: "one"] #=> [1, {:one, "one"}]

But this doesnt

iex> [one: "one", 1] #=> Syntax error before 1
Bala
  • 11,068
  • 19
  • 67
  • 120

1 Answers1

3

That's just how the Elixir syntax is defined in the parser: the identifier : expr syntax (corresponds roughly to the kw rule in the parser linked before) is only accepted at the end of a list literal.

Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • @Bala to archive want you wanna try in your second example, you can use `++` operator: `iex(11)> [one: "one"] ++ [1] #=> [{:one, "one"}, 1]` or directly write your tuple with the correct syntax `{:one, "one"}`. Mixing up different "magic" syntax spellings makes it difficult for any parser to guess what you really want ;-) – Chilian Nov 09 '20 at 13:58
  • BTW: In this context it might also be advisable to look at the topic "Improper lists" => https://stackoverflow.com/questions/1919097/functional-programming-what-is-an-improper-list – Chilian Nov 09 '20 at 14:11