Comparing tuples with and without parentheses

I’m a bit confused about the rules concerning tuples, parentheses, commas, and how Julia interprets them.

Could someone explain to me the following behavior?

julia> (1.0, 2.0) == (1.0, 2.0)  # Nice
true

julia> (1.0, 2.0) .== (1.0, 2.0)  # Charming 🤩
(true, true)

# Surprising perhaps:
julia> (1.0, 2.0) == 1.0, 2.0  # I get it, stuck at the comma
(false, 2.0)

julia> (1.0, 2.0) .== 1.0, 2.0  # Sum of the last previous, fine
((true, false), 2.0)

# I am completely lost in those two:
julia> 1.0, 2.0 == 1.0, 2.0  # WTF?
(1.0, false, 2.0)

julia> 1.0, 2.0 .== 1.0, 2.0  # Same?
(1.0, false, 2.0)

There are places where Julia is more pedantic than, e.g., Python when it comes to parentheses, and this seems to be the case. I’m used to assignments like

julia> a, b = 1, 2
(1, 2)

julia> a
1

julia> b
2

It is OK to use (...) in for loops like (Python won’t require them),

julia> for (i, a) in enumerate("confused by tuples")
           println("$i: $a")
       end
1: c
2: o
3: n
4: f
5: u
6: s
7: e
8: d
9:  
10: b
11: y
12:  
13: t
14: u
15: p
16: l
17: e
18: s

But the examples on tuple comparison above confuses me a bit. Julia’s parser seems a bit “greedier” than Python’s, if that makes sense. What are the exact rules concerning tuples, parentheses, commas, etc.?

1.0, 2.0 == 1.0, 2.0: this makes perfect sense when you look at the commas. These create a tuple of three values.

9 Likes

Commas have lower precedence than all operators except for assignment (=, +=, etc).

As far as I can tell, the precedence of tuple construction in Python is basically the same?

>>> (1.0, 2.0) == (1.0, 2.0)
True
>>> (1.0, 2.0) == 1.0, 2.0
(False, 2.0)
>>> 1.0, 2.0 == 1.0, 2.0
(1.0, False, 2.0)
10 Likes

That’s probably

because we are greedy

(just joking)

3 Likes

Now I get it! Thank you all for the clarifications. I mixed up comparison and assignment syntaxes which by the way are the same as in Python, as @stevengj pointed out.