News for hackers. Stuff that matters.
Re:A History of Lua (2001) by petee on Wednesday August 10, 2022 @07:02AM (#32410074)
Learning it recently for a small game design, I was surprised that you couldnt easily copy a table, and even more suprised you needed "metatables" to do it...the extra steps seem far from simple.

Maybe I'm just missing something. If I recall, I was simply trying to create a new 'object' (game character) from a template but it required metatable boiler plate that just made no sense to me.

Many languages, this is simply a = b, or at most allocating some space first.

Re:A History of Lua (2001) by scythe on Wednesday August 10, 2022 @08:55AM (#32411105)
>I was surprised that you couldn't easily copy a table

Lua's designers have a sort of maxim "if it can't be done perfectly, let the user roll their own". This can be frustrating, but for table-copying it's not surprising. Do you copy subtables by reference or value? What about recursive tables? Mutually recursive subtables? Closures in tables? And then userdata can contain references that are opaque to the language runtime. It's not easy to define the behavior of deep copy in the fully general case.

Re:A History of Lua (2001) by boffinAudio on Wednesday August 10, 2022 @07:51AM (#32410402)
You might find it handy to add lua-enumerable.lua to your standard set of Lua-project modules:

https://github.com/mikelovesrobots/lua-enumerable/blob/maste...

See: table.dup()

Re:A History of Lua (2001) by nmz on Wednesday August 10, 2022 @01:54PM (#32415952)
NOTE: Doesn't copy metamethods, doesn't copy subtables either.
Re:A History of Lua (2001) by DayDollar on Wednesday August 10, 2022 @08:00AM (#32410483)
In many scripting languages this is not the case, and you are ever only passing around references. Python comes to mind.

Lots of people obviously have a problem with this as it deeply different from the algebra derived assignment syntax.

local a = b creates a local copy, but even then, its not a deepcopy, containing references to other tables.

Re:A History of Lua (2001) by kzrdude on Wednesday August 10, 2022 @09:07AM (#32411254)
python has both .copy() methods and a copy module. That's not to highlight Python's strengths, but an indication that it's a complex topic in Python - it has footguns here. The copy module might even do the wrong thing for a given object.
Re:A History of Lua (2001) by naniwaduni on Wednesday August 10, 2022 @10:20AM (#32412430)
The algebra-derived assignment syntax kind of falls apart with mutation anyway.