Skip to content

Conversation

unnawut
Copy link
Contributor

@unnawut unnawut commented Jan 15, 2018

Following ex_doc's #821 discussion...

This is the example provided in Keyword docs:

iex> [{:exit_on_close, true}]
[exit_on_close: true]

And because below, elem(0) is a string, it is not a Keyword. So it remains a list of tuples:

iex> [{"exit_on_close", true}]
[{"exit_on_close", true}]

But the code below, which is currently not documented, is actually a valid keyword syntax, and the string key automatically gets converted to an atom:

iex> ["key": "value"]
[key: "value"]

So I thought it would be nice to document this in the docs. ❤️

@josevalim josevalim merged commit ca3b841 into elixir-lang:master Jan 15, 2018
@josevalim
Copy link
Member

❤️ 💚 💙 💛 💜

@umamaistempo
Copy link
Contributor

Just a note, on the following code

["key": "value"]

"key": is not a string but an atom. For example

:"key" = :key

because :"key" is equivalent to erlang's 'key'; the quotes in atoms allow you to escape more complex atom names, eg: :"atom with spaces" etc, so, ["foo": :bar] means [foo: :bar] because both are literally the same thing (there is no conversion)

@whatyouhide
Copy link
Member

whatyouhide commented Jan 15, 2018

@mememori yep, that's what the PR meant to point out right?

@unnawut
Copy link
Contributor Author

unnawut commented Jan 15, 2018

@mememori I believe what you are pointing out is mainly to do with my understanding of Elixir and use of word (sorry!). Jose made an update to the PR before the merge, which probably already addressed your point. :)

The two syntaxes are completely equivalent. When using the colon syntax,
the key is always an atom, even when wrapped in quotes:

  iex> ["exit_on_close": true]
  [exit_on_close: true]

This mirrors the quoted atom syntax such as :"exit_on_close".

@umamaistempo
Copy link
Contributor

@unnawut yup, i was just explaining that detail, it has nothing to do with the PR.

Thanks for the contrib 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants