Table API could be a lot better

I opened an old project and found table.foreach has been deprecated. I have harped on expanding the table API, but instead it’s just getting slimmer. This is just a bit laughable to be honest. Why is every developer expected to copy and paste the example of Deep Copy into their game if they want to make a deep copy of something? This should be built into the API because it is a trivial common function that should be optimized once and abstracted away from for the developer’s sake. In general, data structures are important parts of building games, yet we don’t really get these common tools in our toolbelt. Why not?

1 Like

No it isn’t? Roblox Documentation doesn’t say that it’s deprecated at all.

You should probably make a feature request for it, since it’s very unlikely to be seen by an engineer here.

table.foreach is archaic syntax that has been deprecated since Lua 5.1, which released in 2006. You should have been using pairs and ipairs 16 years ago.

Luau also has generalized iteration syntax which you can use.

There are lots of considerations for copy operations, shallow or deep. For example:

  • How do you handle a recursive structure? local a = {}; a.b = a
  • How do you handle tables as keys? {[{}] = 1}
  • Do you reference the same metatable in the resulting table? Do you deep copy the metatable? Do you just not consider them at all?
  • Do you copy the table’s frozen state?

You can see some implementations with different compromises here:

Luau includes table.clone which is a shallow cloning function:

Returns a copy of the input table that has the same metatable, same keys and values, and is not frozen even if t was. The copy is shallow: implementing a deep recursive copy automatically is challenging, and often only certain keys need to be cloned recursively which can be done after the initial clone by modifying the resulting table.

7 Likes

I usually use ipairs, but I found table.foreach and decided to try that instead. It wasn’t until when I posted that the script intellisense told me table.foreach is deprecated.

You make a good point when it comes to the choices of deep copy, but perhaps my example was poorly picked. My point was to reduce the need for developers to build their own data structure and algorithm libraries by providing a more complete toolkit of these operations. Some operations don’t require any developer decisions and can be packaged up into a permanent built-in module.

I, like most people, still cannot create a feature request anywhere despite having a rather old account and moderate activity bursts.

It looks like they finally marked it as deprecated in intellisense this month. It has been deprecated in Lua 5.1 since its release - it looks like they just forgot to reflect that:

Your response still seems unwarranted. You knew of the iteration syntax that is commonly used (pairs/ipairs) but instead chose to try a function you saw, and then were disappointed when it was marked deprecated. That doesn’t make the API “laughable”. Luau’s table API isn’t “getting slimmer”, it’s been iterated on for years. This is just an intellisense update to reflect a 16-year-old deprecation. Nothing was actually removed - you can still use the redundant function if you want!

You’re looking for a Luau language feature, not an engine feature. You can make a GitHub issue for that - GitHub - Roblox/luau: A fast, small, safe, gradually typed embeddable scripting language derived from Lua

This comment by zeuxcg might be relevant: Enhanced / extended auxiliary table and string functions · Issue #395 · Roblox/luau · GitHub

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.