Reputation: 17
I wanted to iterate over triplets in a tuple and found a weird comma detail in Julia is that what we should expect?
julia> for (k, n, d) in (("x1", "x2", "x3"),); @show k, n, d; end;
(k, n, d) = ("x1", "x2", "x3")
# However if I remove , it doesn't work
julia> for (k, n, d) in (("x1", "x2", "x3")); @show k, n, d; end;
ERROR: BoundsError
Stacktrace:
[1] indexed_next(::String, ::Int64, ::Int64) at ./tuple.jl:56
[2] anonymous at ./<missing>:?
Upvotes: 1
Views: 139
Reputation: 31342
Yes, this is behaving as expected. Parentheses alone do not create tuples. If they did, simply basic math expressions like 2*(3+4)
wouldn't work. Parentheses without commas or semicolons in them are used for precedence groupings or function calls. That's why you need that explicit trailing comma in the one-tuple case.
((x,y,z))
is the same as (x,y,z)
.
((x,y,z),)
constructs a one-tuple that contains (x,y,z)
.
Upvotes: 5