lua-users home
lua-l archive

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


This would work only if "__metatable" is added as a reserved keyword of the language and reserved as well as keys in tables (note that tables can even be keyed by empty string, only the nil key is special, but it's not usable for this purpose because Lua assigned a role for nil keys: they must throw an error if they get dereference). So my proposal was to use a specific reserved "keyword" for the metatable pseudo-index, and "!" (not quoted as a string) came as the easiest solution with additional benefits that get/setmetatable do not offer:
* "t[!].x" would be always valid without having to explicitly write any test if "t" currently has a metatable or not, it would automatically create an empty table if needed before assigning the member "x".
  This would simplify a lot the life of Lua programmers. And this does not restrict any sandboxing (internally this will still use some get/setmetatable() function call from the local environment, possibly sandboxed as well)
* "t = {1,2, ! = {member: 'this is a member of the metatable'}}": this allows serialization of data using tables, AND attached metatables in a simple constructor syntax (impossible to do for now with setmetatable without using separate instructions)





Le sam. 16 mai 2020 à 06:11, Andrea <andrea.l.vitali@gmail.com> a écrit :
Why get/setmetatable() are necessary?

One could simply set a special key such as __metatable by writing something like 

mt = {}
t = {}
t.__metatable = mt 

Instead of writing 

setmetatable(t,mt)

And similarly for getmetatable()

So what is the reason to keep these functions? 

This question is born from looking at the language evolution: there were set/getfenv() and now there is just a special __ENV

    Andrea 
--
Andrea Vitali






_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org