# "Ternary operator", (?:) associates from right to left?

In the manual ist is written for the ternary operator
a ? b : c
“to facilitate chaining, the operator associates from right to left.”
See: https://docs.julialang.org/en/latest/manual/control-flow/#man-conditional-evaluation-1

Thanks Hernrique for the solution below:
So it seems rigth association does tell how to put the brackets, but not that the bracketed term is evaluated first.

julia>test(x, y) =
println( (println(“A5”);x) <= (println(“A6”);y) ? (println(“A7”);x) <= (println(“A8”);y) ?
(println(“A1”);“x is Type1”)
: (println(“A2”);“x is Type2”)
: (println(“A3”);“x is Type3”)
)

julia>test3(2, 2)

A5
A6
A7
A8
A1
x is Type1

I understand the result. But I do not see why that behaviour is called operator associates from right to left.

Best greetings Gerhard

I am not sure of what you expected. You passed the values `2` and `2`, those values enter the first clause of the outer ternary operator (because `2 <= 2`) and then enter the first clause of the inner ternary operator (that has the exact same test, is this intended?). The ternary operator is basically a simplified `if` notation focusing in expressions (returning values) instead of statement blocks. Would you find it strange to have such return from the following code?

``````function test4(x, y)
if (println("A5"); x) <= (println("A6"); y)
if (println("A7"); x) <= (println("A8"); y)
println("A1")
return "x is Type1"
else
println("A2")
return "x is Type2"
end
else
println("A3")
return "x is Type3"
end
end
``````

I just try to find out what is the meaning of:
“to facilitate chaining , the operator associates from right to left .”
For me it seems everything is evaluated from left to rigth.
I just want to find an example where the operator associates from rigth to left.

If you are thinking why “A5” appears before “A6” (and the same for “A7” and “A8”). This is because this depends on the associativity of the comparisons operators (like `<=`) that you can see here it is non-associative but a footnote at the end of page says it defaults to left-to-right associativity, AND it has a higher precedence than the ternary operator. None of your tests check the associativity of the ternary operator itself, I will think a little about how to do it.

For && is also written " Both `&&` and `||` associate to the right":
https://docs.julialang.org/en/latest/manual/control-flow/#Short-Circuit-Evaluation-1
But the test shows:
julia> t(x) = (println(x); true)
julia> f(x) = (println(x); false)
julia> t(1)&&t(2)&&t(3)
1
2
3

But if you cite wikipedia:

Consider the expression `a ~ b ~ c` . If the operator has right associativity, the expression would be interpreted as `a ~ (b ~ c)` .

So it should be, t(1)&& ( t(2)&&t(3))
2
3
1
if && is rigth associate as described. It seems I miss some point?

Associativity only matters when the precedence of the operators is the same. The only operator with the same associativity of the ternary operator is the ternary operator. So the test needs to just use ternary operators. The right associativity means:

``````> true ? false : true ? 1 : 2
false
> true ? false : (true ? 1 : 2)
false
> (true ? false : true) ? 1 : 2
2
``````

In other words, “implicit” parenthesis are placed this way, not the other way, when no explicit parenthesis are given.

3 Likes