# List operator associativity in the documentation

**URL:** <https://discourse.julialang.org/t/list-operator-associativity-in-the-documentation/5080>\
**Category:** Internals & Design\
**Tags:** documentation\
**Created:** [July 26, 2017, 10:28pm UTC](https://discourse.julialang.org/t/list-operator-associativity-in-the-documentation/5080 "2017-07-26T22:28:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tparker](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tparker](https://discourse.julialang.org/u/tparker)\
**Post date:** [July 26, 2017, 10:28pm UTC](https://discourse.julialang.org/t/list-operator-associativity-in-the-documentation/5080/1 "2017-07-26T22:28:12Z")

</div>

The documentation should discuss operator associativity somewhere (presumably on the “[Mathematical Operations and Elementary Functions](https://docs.julialang.org/en/stable/manual/mathematical-operations/)” page). The [Operator precedence](https://docs.julialang.org/en/stable/manual/mathematical-operations/#Operator-Precedence-1) 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](http://en.cppreference.com/w/c/language/operator_precedence) 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 🙂.

---

<div class="post-metadata">

**Author:** ![tparker](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tparker](https://discourse.julialang.org/u/tparker)\
**Post date:** [July 28, 2017, 12:14am UTC](https://discourse.julialang.org/t/list-operator-associativity-in-the-documentation/5080/2 "2017-07-28T00:14:00Z")

</div>

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 `:->`?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 28, 2017, 2:02am UTC](https://discourse.julialang.org/t/list-operator-associativity-in-the-documentation/5080/3 "2017-07-28T02:02:59Z")

</div>

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

```julia
julia> Base.operator_precedence(:foo)
0

```

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.

---

<div class="post-metadata">

**Author:** ![tparker](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tparker](https://discourse.julialang.org/u/tparker)\
**Post date:** [July 28, 2017, 7:03am UTC](https://discourse.julialang.org/t/list-operator-associativity-in-the-documentation/5080/4 "2017-07-28T07:03:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ZacLN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zacln/32/2748_2.png) [@ZacLN](https://discourse.julialang.org/u/ZacLN)\
**Post date:** [July 28, 2017, 9:29am UTC](https://discourse.julialang.org/t/list-operator-associativity-in-the-documentation/5080/5 "2017-07-28T09:29:40Z")

</div>

Operator precedence with all recognised symbols can be found at [https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm#L9](https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm#L9).

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.

---

<div class="post-metadata">

**Author:** ![Evey](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evey/32/214967_2.png) [@Evey](https://discourse.julialang.org/u/Evey)\
**Post date:** [September 19, 2017, 8:50am UTC](https://discourse.julialang.org/t/list-operator-associativity-in-the-documentation/5080/6 "2017-09-19T08:50:23Z")

</div>

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: [https://github.com/JuliaLang/julia/pull/23754](https://github.com/JuliaLang/julia/pull/23754)

Please feel free to comment with any suggestions. 🙂
