astery

astery

Why `<-` listed as `Left` associativity operator in elixir docs?

Here is the list and it is at the bottom of the table.

I would expect that <- would have a right associativity like =. Because in both operators when we match statements:

{:ok, _} = Some.function()

and

with {:ok, _} <- Some.function() do
  # ...
end

Pattern is on the left side and one evaluated is on the right. So I’ve expected Right associativity.

What’s wrong with my reasoning?

Marked As Solved

IvanIvanoff

IvanIvanoff

Associativity plays a role when multiple operators are chained. It controls the order of grouping operands to operators with the same precedence.

= being with Right associativity means that a = b = 10 will be evaluated as (a = (b = 10)) and not as ((a = b) = 10).

- is also left associative, meaning that 5 - 4 - 3 is evaluated as ((5 - 4) - 3) == -2. If it was right associative it would have been evaluated as (5 - (4 - 3)) == 4.

When the operator is used a single time, the associativity does not play a role – 5 - 3 == 2 no matter if - is left or right associative

<- is left associative not only in the docs, but also in the parser. So there is no error in the documentation.

I was not able to actually come up with an example where multiple <- are chained, something like this does not work:

with a <- b <- 10 do
  {a, b}
end

If I had to guess before looking it up, I would have guessed that <- is non-associative. Example for non associative operator is the unary not. A single not true is valid, but chaining multiple nots is not: not true not false is a syntax error.

Also Liked

IvanIvanoff

IvanIvanoff

In these examples of with and for the comma , separates the expressions. The comma itself is left associative, which is what controls the order of evaluation.

You can check that by replacing the left associative <- with the right associative = and still being able to use the result of the previous results:

with a = 5,
     b = a + 10,
     c = b + 20 do
  {a, b, c}
end
# => {5, 15, 35}

Last Post!

IvanIvanoff

IvanIvanoff

In these examples of with and for the comma , separates the expressions. The comma itself is left associative, which is what controls the order of evaluation.

You can check that by replacing the left associative <- with the right associative = and still being able to use the result of the previous results:

with a = 5,
     b = a + 10,
     c = b + 20 do
  {a, b, c}
end
# => {5, 15, 35}

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

We're in Beta

About us Mission Statement