List operator associativity in the documentation

The documentation should discuss operator associativity somewhere (presumably on the “Mathematical Operations and Elementary Functions” page). The Operator precedence table on that page is quite helpful, but it would be even better if it also listed the operator associativity within each precedence level, like this table does for C. If there’s a built-in function that gives the associativity for an operator (like Base.operator_precedence does for precedence), then that would also be good to mention.

BTW, I don’t actually know the associativity of the various common operators, so if anyone happens to, but doesn’t want to take the time to change the documentation, then please feel free to let me know here :slight_smile:.


Relatedly, the documentation should mention that the anonymous function operator -> behaves in an exceptional way in terms of operator precedence. For example, Base.operator_precedence(:->) and Base.operator_precedence(:(=)) return 0 and 1 respectively, implying that the assignment operator = binds more tightly than -> does. But Julia parses the expression f = x -> x^2 as f = (x -> x^2) instead of as (f = x) -> x^2 (i.e. a constant anonymous function that returns x^2 when called with 0 or 1 arguments), as one would expect from the results of Base.operator_precedence. Perhaps Base.operator_precedence should even have an exception for :->?

operator_precedence returns 0 for anything that is not recognized as an operator, not for things with the lowest precedence.

julia> Base.operator_precedence(:foo)

The -> arrow is not actually in the precedence list of the other binary operators, because it is parsed specially. This is why operator_precedence(:->) returns 0 for it.

1 Like

I see. Perhaps this should be mentioned in the documentation as well.

Operator precedence with all recognised symbols can be found at

Assignment (=, etc.), conditional (a ? b : c), arrows, lazy OR/AND and power ops are right associative, the rest, left. Anon. functions and where (if you consider it an op) are slightly strange as is ^ when combined with a preceding minus operation.


I too was confused about this, and needed to programmatically know how different operators associate (which is not entirely trivial), so I opened up a PR which addresses the issue:

Please feel free to comment with any suggestions. :slight_smile: