lua-users home
lua-l archive

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


What you propose is a different thing: you do not access a property of a table, instead you use a property of another _META object (in the *hope* that it is referencing the table as one of its keys).
This would change the language radically.

And I don't see any syntaxic benefit to replace "getmetatable(t).metafield" by "_META[t].metafield" (which is still not warrantied to work if you want to perform any access to a metafield).

I'm not inventing the [[...]] notation: it is commonly used in ECMAScript/_javascript_ and a core part of the language (all objects in _javascript_ have also an associated metatable, whose access can be restricted/protected when needed if it is shared across multiple objects). C++ has the concept of "runtime type info" and "virtual tables" for some of the same goals, but they are not directly accessible and not part of the core language (hidden as a needed hidden part of the implementation, and only exposed via some platform-specific reflection/debugging API).


Le mar. 19 mai 2020 à 17:29, Andrea <andrea.l.vitali@gmail.com> a écrit :
Philippe,

I will split my reply in two because your email is quite rich. Here the reply about metatables:

Why not "t[!]" or "t[~]" to refer to the metatable with the index notation (except that the index is not valid _expression_ here, it's a syntaxic construct)
The alternative would be to give a meaning to t[nil]: trying to read index nil normally generates an exception (with the default getindex accessor) but could as well allow reading/writing the metatable 
:  
- t[['m']] would access to the member 'm' of the metatable of t as a shortcut to getmetatable(t)['m']
- t[[[m']]] would access to the member 'm' of the metatable of the metatable of t as a shortcut to getmetatable(getmetatable(t))['m']

Lua syntax is clean and beautiful. Maybe there is a way to respect the spirit of the language, We can adopt a solution similar to _ENV that has replaced getfenv/setfenv, The metatable is a property of tables (and types in some case) you can then choose to have a property table:

_META[t] = mt   -- same as setmetatable(t,mt)

mt = _META[t]   -- same as mt = getmetatable(t)

This is similar to the solution adopted by _ENV so looks clean (at least to me). It would not be an actual look up in the META properties table, instead it would be map to the functions that are in place today.

   Andrea


--
Andrea Vitali