lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Wed, Feb 12, 2020 at 4:59 AM Pavel <temp213@gorodok.net> wrote:
I like the idea of having a "table-type" default metatable, like for numbers
and strings.
And to set this metatable to "table" library (like for strings).
then things like t = {"one","two","three"}:insert("four"):sort(...) should
work.
patch is here: http://lua-users.org/lists/lua-l/2019-03/msg00080.html

Creating tables with special function that sets the metatable correctly also
fine, but would be nice to have an option to set it once for all the newly
created tables.


 This is a reasonable patch, and would certainly constitute a convenience under some circumstances.

I think the reason Lua doesn't work this way, is that it imposes a speed penalty on any nil-returning lookup, to every table so created.

I can imagine realistic workflows in which a negative table lookup happens hundreds of thousands of times in a hot loop, like searching a large file for a single hash.  With this patch, the runtime would have to check the special table metatable for every one of those lookups, which would impact performance. 

Strings are different, any method chain that returns nil (e.g. ("foo"):not_defined()) is going to be a bug. 

Again, making a personal copy of Lua work this way is perfectly reasonable. But I don't think it should be added to the language, for this reason.

cheers,
-Sam.