marick

marick

Why is the syntax tree for a 2-tuple different than for other tuples?

Because Elixir code is parsed into 3-tuples, there needs to be a different notation for an embedded 3-tuple. It is what I think of as “constructor” notation:

iex(12)> quote do: {1, 2, 3}
{:{}, [], [1, 2, 3]}

That’s true for other tuple lengths, which makes sense: why not be consistent?

Except for 2-tuples, which are represented unchanged:

iex(7)> quote do: {1, 2}
{1, 2}

My hunch is that’s because lists are unchanged:

iex(8)> quote do: [1, 2, 3]
[1, 2, 3]

… and making 2-tuples unchanged means that keyword lists are also unchanged:

iex(11)> quote do: [a: 1, b: 2]         
[a: 1, b: 2]

I can’t think of a case where treating 2-tuples like other tuples would cause ambiguity, though. Is this just to make literal keyword lists more readable?


Maps also have a constructor representation:

iex(13)> quote do: %{a: 1}
{:%{}, [], [a: 1]}

Why is that needed?

Marked As Solved

dorgan

dorgan

It’s because the Elixir AST is optimized for developer ergonomics when writing macros, it’s not a goal to be regular(in which case it would be more verbose).

Having them as 2-tuples, in combination with lists being literals in the AST, is what makes it easier to write macros for keyword blocks, like

defmacro schema(table, do: fields) do ...

If the AST were regular, you’d need to do something like this

defmacro schema(table, [{:{}, _, [:do, fields]}]) do ...

And you can imagine how it would be more complicated for other macros. Processing the keyword syntax for Ecto queries would be much more verbose, for example.

Also Liked

mudasobwa

mudasobwa

Creator of Cure

FWIW, Elixir properly handles 2-element tuples in uniform AST notation, for the sake of convenience.

defmodule Tuple2 do
  defmacro t(list), do: {:{}, [], list}
end

require Tuple2
Tuple2.t([:a, :b])
#⇒ {:a, :b}

Last Post!

mudasobwa

mudasobwa

Creator of Cure

FWIW, Elixir properly handles 2-element tuples in uniform AST notation, for the sake of convenience.

defmodule Tuple2 do
  defmacro t(list), do: {:{}, [], list}
end

require Tuple2
Tuple2.t([:a, :b])
#⇒ {:a, :b}

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement