What does tuple splatting do?

If t is a tuple of 2 elements of type A and B, what does

a, b = t

do ? Is it equivalent to

a = t[1]
b = t[2]

or does it do something else ?
While updating Polyhedra to Julia v0.7, I hit a weird issue where a and b have types Vector instead of A and B as expected. Moreover, when asking for the length of b I get a number that seems to large to have a vector of this size fitting in memory.

You can see what it does by using @code_lowered, for example

f(t) = begin a, b = t; end
@code_lowered f([:a, Float64])

gives

CodeInfo(
1 1 ─ %1 = (Base.indexed_iterate)(t, 1)                                     β”‚
  β”‚        a = (Core.getfield)(%1, 1)                                       β”‚
  β”‚        #temp# = (Core.getfield)(%1, 2)                                  β”‚
  β”‚   %4 = (Base.indexed_iterate)(t, 2, #temp#)                             β”‚
  β”‚        b = (Core.getfield)(%4, 1)                                       β”‚
  β”‚        #temp# = (Core.getfield)(%4, 2)                                  β”‚
  └──      return t                                                         β”‚
)

So it’s iterating over t (the indexed_iterate call) and putting the result in the a and then b variables.

(I think the indexed_iterate function returns a tuple of the value and the iterator state and the subsequent getfield is just getting the first element of that tuple, the value.)

4 Likes