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