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.
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.
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: