ERROR: syntax: unexpected semicolon in tuple around REPL

Why can’t I write this compound expression? (The context is a Givens rotation)

julia> A = rand(-9:9, 2, 2)
2×2 Matrix{Int64}:
 -9  -9
 -1   1

julia> t = pi/3
1.0471975511965976

julia> c, s = cos(t), sin(t); [c s; -s c] * A
2×2 Matrix{Float64}:
 -5.36603  -3.63397
  7.29423   8.29423

julia> (c, s = cos(t), sin(t); [c s; -s c] * A)
ERROR: syntax: unexpected semicolon in tuple around REPL[4]:1
Stacktrace:
 [1] top-level scope
   @ REPL[4]:1

Likely because (; ...) is reserved syntax for NamedTuple. Generally speaking, () is for creating tuple and it isn’t a compound expression, even just writing:

julia> (c, s = cos(t), sin(t))
ERROR: syntax: invalid named tuple element "sin(t)" around REPL[6]:1

You can write: begin c, s = cos(t), sin(t); [c s; -s c] * A end

2 Likes

Interestingly, this works

julia> A = rand(-9:9, 2, 2); t = pi/3;

julia> ((c, s) = (cos(t), sin(t)); [c s; -s c] * A)
2×2 Matrix{Float64}:
  4.29423   3.5
 10.5622   -6.06218

It’s just one of those arbitrary parser decisions when looser convenient syntax overlaps. ((c, s) = (cos(t), sin(t)); [c s; -s c] * A) happened to work because the extra parentheses isolated the comma from the outer parentheses, so it was no longer recognized as a plain or named tuple:

julia> 1; 2, 3
(2, 3)

julia> (1; 2, 3)
ERROR: syntax: unexpected semicolon in tuple around REPL[33]:1
Stacktrace:
 [1] top-level scope
   @ REPL[33]:1

julia> (1; (2, 3))
(2, 3)

Use the less ambiguous multi-line syntax like begin blocks to evade these overlaps.

1 Like

Note that the expression does parse, because it’s needed for function arguments:

julia> :(c, s = cos(t), sin(t); [c s; -s c] * A)
:((c, s = cos(t), sin(t); [c s; -s c] * A))

However, it’ts parsed as

(c, (s = cos(t)), sin(t); [c s; -s c] * A)

It’s only during lowering that Julia forbids this form.