# Is there any way to make custom binary infix operators right associative?

**URL:** https://discourse.julialang.org/t/is-there-any-way-to-make-custom-binary-infix-operators-right-associative/3202
**Category:** General Usage
**Tags:** infix
**Created:** [April 13, 2017, 1:19pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-make-custom-binary-infix-operators-right-associative/3202 "2017-04-13T13:19:48Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [April 13, 2017, 1:19pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-make-custom-binary-infix-operators-right-associative/3202/1 "2017-04-13T13:19:49Z")

</div>

If I define some new binary infix operator, it appears to always be left associative by default, i.e.

```julia-auto
julia> ⨳(a,b) = (a,b)
julia> 1⨳2⨳3
((1,2),3)

```

Is there any to make it right associative instead? I had guessed defining something like this might work but it doesn’t,

```julia
⨳(args...) = foldr(⨳,args)

```

the call appears to always go to the two-argument function first with assumed left assosciativity.

---

<div class="post-metadata">

### Author: ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)
#### Post date: [April 13, 2017, 1:25pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-make-custom-binary-infix-operators-right-associative/3202/2 "2017-04-13T13:25:05Z")

</div>

Associativity is a property of the operator. The only way to do this is to use a right associative binary operator. I believe arrows fit this description, as do assignments.

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [April 13, 2017, 1:30pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-make-custom-binary-infix-operators-right-associative/3202/3 "2017-04-13T13:30:43Z")

</div>

Yep, you’re right about arrows,

```julia
julia> →(a,b) = (a,b)
julia> 1→2→3
(1,(2,3))

```

Any idea where there’s a list of what Julia assumes for each operator?

I guess it doesn’t make much sense to me that Julia picks the assosciativity based on the unicode character. Shouldn’t I, the programmer defining what these operators mean, get to pick?

---

<div class="post-metadata">

### Author: ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)
#### Post date: [April 13, 2017, 1:39pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-make-custom-binary-infix-operators-right-associative/3202/4 "2017-04-13T13:39:39Z")

</div>

In order to define associativity the way you have, operators have to be parsed n-ary. Currently the only n-ary operators are `+` and `*` which are usually associative anyway. Unfortunately, even with n-ary parsing, mixing different operators with the same precedence will have the operator’s default associativity.

The reason operator associativity cannot be defined by the programmer in general (except via nary parsing, if applicable) is that the precedence of the operator must be known at parse time, whereas what the operator means is only knowable at runtime in general.

---

<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: [April 13, 2017, 1:50pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-make-custom-binary-infix-operators-right-associative/3202/5 "2017-04-13T13:50:32Z")

</div>

> [@marius311](#):
>
> Any idea where there’s a list of what Julia assumes for each operator?

I believe every operator listed as `prec-arrow` or `prec-power` in [https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm](https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm) is right-associative.

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [April 13, 2017, 2:00pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-make-custom-binary-infix-operators-right-associative/3202/6 "2017-04-13T14:00:24Z")

</div>

Very helpful, explanation, thanks. So yea, it seems that for operators parsed as n-ary my attemped solution above works, e.g.

```julia
julia> import Base: +
julia> +(args::String...) = foldr(+,args)
julia> +(a::String, b::String) = "($a,$b)"
julia> "a"+"b"+"c"
"(a,(b,c))"

```

So I think really what I’d want is more operators to be parsed n-ary like + and \* are (e.g. if my ⨳ above was included I’d be good). Which leads me to find [https://github.com/JuliaLang/julia/issues/7368](https://github.com/JuliaLang/julia/issues/7368) where I’ll probably comment about this particular usefulness of parsing more things as n-ary, which doesn’t seem to have been mentioned there.

---

<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: [September 25, 2017, 10:30pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-make-custom-binary-infix-operators-right-associative/3202/7 "2017-09-25T22:30:17Z")

</div>

This PR updates the documentation to list operator associativity as well as precedence:  
[https://github.com/JuliaLang/julia/pull/23754](https://github.com/JuliaLang/julia/pull/23754)

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [April 20, 2023, 1:35am UTC](https://discourse.julialang.org/t/is-there-any-way-to-make-custom-binary-infix-operators-right-associative/3202/8 "2023-04-20T01:35:17Z")

</div>

> [@fengyang.wang](#):
>
> In order to define associativity the way you have, operators have to be parsed n-ary. Currently the only n-ary operators are `+` and `*`

I wish there were more n-ary operators in Julia. There are cases where it’s more efficient to operate on all arguments at once instead of processing one after the other. Is there any hope that the parsing gets changed? For example, an infix operator `op` could be parsed to something like `nary(op, x...)` with default method `foldl(op, x)` or `foldr(op, x)`. That would be similar to `Base.literal_pow` for powers with constant exponents.
