Built-in function for spreading objects into tuples

Right now you can easily spread an array into a tuple using unpack.

x = {1, 2, 3}

one, two, three = unpack(x)

This is useful but it only works for arrays. I often find myself wanting a spread function that works for non-array tables.

x = {a=1, b=2, c=3}

a, b, c = spread(x, 'a', 'b', 'c')

I want this to be built-in because it’s very useful.

4 Likes
local function spread(t,f,...)
	if f == nil then
		return
	end
	return t[f],spread(t,...)
end

There have been also similar feature requests for destructuring tables:

local x = {a=1,b=2,c=3}
local {a,b,c} = x
3 Likes