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

**URL:** <https://discourse.julialang.org/t/ternary-operator-associates-from-right-to-left/39179>\
**Category:** New to Julia\
**Tags:** documentation\
**Created:** [May 9, 2020, 1:58pm UTC](https://discourse.julialang.org/t/ternary-operator-associates-from-right-to-left/39179 "2020-05-09T13:58:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![GerhardTotschnig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gerhardtotschnig/32/215516_2.png) [@GerhardTotschnig](https://discourse.julialang.org/u/GerhardTotschnig)\
**Post date:** [May 9, 2020, 1:58pm UTC](https://discourse.julialang.org/t/ternary-operator-associates-from-right-to-left/39179/1 "2020-05-09T13:58:55Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 9, 2020, 2:35pm UTC](https://discourse.julialang.org/t/ternary-operator-associates-from-right-to-left/39179/2 "2020-05-09T14:35:17Z")

</div>

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?

```julia
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

```

---

<div class="post-metadata">

**Author:** ![GerhardTotschnig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gerhardtotschnig/32/215516_2.png) [@GerhardTotschnig](https://discourse.julialang.org/u/GerhardTotschnig)\
**Post date:** [May 9, 2020, 2:38pm UTC](https://discourse.julialang.org/t/ternary-operator-associates-from-right-to-left/39179/3 "2020-05-09T14:38:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 9, 2020, 2:45pm UTC](https://discourse.julialang.org/t/ternary-operator-associates-from-right-to-left/39179/4 "2020-05-09T14:45:53Z")

</div>

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](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity-1) 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.

---

<div class="post-metadata">

**Author:** ![GerhardTotschnig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gerhardtotschnig/32/215516_2.png) [@GerhardTotschnig](https://discourse.julialang.org/u/GerhardTotschnig)\
**Post date:** [May 9, 2020, 3:13pm UTC](https://discourse.julialang.org/t/ternary-operator-associates-from-right-to-left/39179/5 "2020-05-09T15:13:49Z")

</div>

For && is also written " Both `&&` and `||` associate to the right":  
[https://docs.julialang.org/en/latest/manual/control-flow/#Short-Circuit-Evaluation-1](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:

> **[Operator associativity](https://en.wikipedia.org/wiki/Operator_associativity)**
>
> In programming language theory, the associativity of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses. If an operand is both preceded and followed by operators (for example, ^ 3 ^), and those operators have equal precedence, then the operand may be used as input to two different operations (i.e. the two operations indicated by the two operators). The choice of which operations to apply the operand to, is determined by the as...

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?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 9, 2020, 3:20pm UTC](https://discourse.julialang.org/t/ternary-operator-associates-from-right-to-left/39179/6 "2020-05-09T15:20:15Z")

</div>

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:

```julia
> 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.
