# Chained comparison evaluation order

**URL:** <https://discourse.julialang.org/t/chained-comparison-evaluation-order/8999>\
**Category:** General Usage\
**Created:** [February 11, 2018, 5:30pm UTC](https://discourse.julialang.org/t/chained-comparison-evaluation-order/8999 "2018-02-11T17:30:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [February 11, 2018, 5:30pm UTC](https://discourse.julialang.org/t/chained-comparison-evaluation-order/8999/1 "2018-02-11T17:30:47Z")

</div>

This came straight from the [official documentation](https://docs.julialang.org/en/stable/manual/mathematical-operations/#Chaining-comparisons-1). I don’t understand why `v(2)` should be evaluated before `v(1)` in the first example although technically it shouldn’t matter (unlike short circuit operators)… It just seems counterintuitive.

```julia
julia> v(x) = (println(x); x)
v (generic function with 1 method)

julia> v(1) < v(2) <= v(3)
2
1
3
true

julia> v(1) < v(2) 
1
2
true

julia> v(2) <= v(3)
2
3
true

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [February 11, 2018, 8:58pm UTC](https://discourse.julialang.org/t/chained-comparison-evaluation-order/8999/2 "2018-02-11T20:58:12Z")

</div>

For the record, this is different from Python:

```python
>>> def v(x):
... print(x)
... return x
... 
>>> v(1) < v(2) < v(3)
1
2
3
True

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 11, 2018, 9:26pm UTC](https://discourse.julialang.org/t/chained-comparison-evaluation-order/8999/3 "2018-02-11T21:26:42Z")

</div>

The evaluation order is undefined and could be changed. You should probably not be counting on side effects from expressions in comparison chains. IIRC, @jeff.bezanson did it this way because it could be implemented slightly more efficiently/easily. That said, I would support making the evaluation order defined and guaranteed left-to-right.
