Skip to content

key: value syntax sugar in tuples #13681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
LostKobrakai opened this issue Jun 21, 2024 · 0 comments
Closed

key: value syntax sugar in tuples #13681

LostKobrakai opened this issue Jun 21, 2024 · 0 comments

Comments

@LostKobrakai
Copy link
Contributor

Elixir and Erlang/OTP versions

Erlang/OTP 27 [erts-15.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Interactive Elixir (1.17.0) - press Ctrl+C to exit (type h() ENTER for help)

Operating system

Mac OS

Current behavior

{1, a: :b} is valid syntax and evalutes to {1, [a: :b]}.

This came up recently on https://elixirforum.com/t/keyword-inside-tuple-shorthand-syntax-a-1-b-2/63960, but also had someone ask on the slack beginner channel about it today.

Expected behavior

On https://hexdocs.pm/elixir/syntax-reference.html there are two sections related to a: :b key/value pairs.

It's mentioned as part of the definition of maps and keyword list, when within %{…} and […] (here). Given here it's taken as a keyword list, but without explicit square brackets, the section on optional square brackets is also relevant (here).

Elixir also supports a syntax where if the last argument of a call is a keyword list then the square brackets can be skipped.

Calls are earlier in the document defined to be non-qualified calls (fun(…)) or qualified calls (Mod.fun(…)). Both include

…, must start with characters and then follow the same rules as variables, which are optionally followed by parentheses, and then arguments.

Tuple creation doesn't fit that picture painted by those parts of the syntax reference. I would've expected that square brackets would be required for a keyword list being the last element of a tuple.

@michalmuskala showed on slack that the following is also valid syntax, but in that case it doesn't evaluate to a valid value:

iex(2)> quote(do: <<1, a: 1, b: 2>>)
{:<<>>, [], [1, [a: 1, b: 2]]}

I can't say what's wrong here, documentation or implementation, but coming from the documentation the current behaviours looks like a bug. If that's not a bug then I think the documentation needs adjustment.

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

No branches or pull requests

1 participant