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}

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement