user441521
user441521

Reputation: 6998

Lua 5.3 unpacking

I'm trying to unpack a table of variables into a function call as arguments. In short I'm looking at mimicking dependency injection into my process by doing this. When I do this at https://www.lua.org/cgi-bin/demo, it tells me unpack is nil. It seems it was removed? Is there an alternative way to do this instead of passing a table that has the object instances in it? I like the idea of specifying each object in my PostStart() call.

Object1 = {}

function Object1:Create()
   local obj = {}

   obj.name = "Object1"

   return obj
end

Object2 = {}

function Object2:Create()
   local obj = {}

   obj.name = "Object2"

   return obj
end

function PostStart(obj1, obj2)
   print(obj1.name, obj2.name)
end

objs = {}
table.insert(objs, Object1:Create())
table.insert(objs, Object2:Create())

PostStart(unpack(objs))

Upvotes: 3

Views: 4651

Answers (1)

lhf
lhf

Reputation: 72422

The online Lua demo runs the latest version of Lua, which currently is 5.3.

In Lua 5.2, unpack was moved to table.unpack.

Upvotes: 8

Related Questions