From 8e0901dd603af1eae1941609c628170935f567ec Mon Sep 17 00:00:00 2001 From: Unnawut Leepaisalsuwanna <921194+unnawut@users.noreply.github.com> Date: Sun, 14 Jan 2018 12:33:54 +0700 Subject: [PATCH 1/2] Add notes for using string keys in colon syntax --- lib/elixir/lib/keyword.ex | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/elixir/lib/keyword.ex b/lib/elixir/lib/keyword.ex index 2260538317a..b460cc7cd35 100644 --- a/lib/elixir/lib/keyword.ex +++ b/lib/elixir/lib/keyword.ex @@ -20,10 +20,16 @@ defmodule Keyword do iex> [{:active, :once}] [active: :once] - The two syntaxes are completely equivalent. Note that when keyword - lists are passed as the last argument to a function, if the short-hand - syntax is used then the square brackets around the keyword list can - be omitted as well. For example, the following: + The two syntaxes are completely equivalent. + + Using the colon syntax will automatically convert string keys to atoms: + + iex> ["exit_on_close": true] + [exit_on_close: true] + + Note that when keyword lists are passed as the last argument to a function, + if the short-hand syntax is used then the square brackets around the keyword list + can be omitted as well. For example, the following: String.split("1-0", "-", trim: true, parts: 2) From 97b95a6c6a74b10c6c16f61c4fca1320677c0c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 15 Jan 2018 09:32:52 +0100 Subject: [PATCH 2/2] Update keyword.ex --- lib/elixir/lib/keyword.ex | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/elixir/lib/keyword.ex b/lib/elixir/lib/keyword.ex index b460cc7cd35..cc8883b6a3d 100644 --- a/lib/elixir/lib/keyword.ex +++ b/lib/elixir/lib/keyword.ex @@ -20,13 +20,14 @@ defmodule Keyword do iex> [{:active, :once}] [active: :once] - The two syntaxes are completely equivalent. - - Using the colon syntax will automatically convert string keys to atoms: + 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"`. + Note that when keyword lists are passed as the last argument to a function, if the short-hand syntax is used then the square brackets around the keyword list can be omitted as well. For example, the following: