lua-users home
lua-l archive

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


On Sat, May 16, 2020 at 6:11 AM Andrea wrote:
> Why get/setmetatable() are necessary?
> One could simply set a special key such as __metatable by writing something like
> mt = {}
> t = {}
> t.__metatable = mt

Tables are general purpose, you may want to use __metatable by your
own. It is not so strange, just think to a dictionary of all the words
found in a text (and pass to it the lua manual ;) ). It would be
better  to use a "Special table key" returned by a standard function,
e.g. calling it "metakey":

mt = {}
t = {}
t[metakey()] = mt

I do not think it is better than the current set/getmetatable.
Moreover it would make more complex to reproduce the behaviour of

t = setmetatable({}, {__metatable="Not your buisness"})
print(getmetatable(t))

Probably, the better way to make this work, is to use a special syntax, e.g.

mt = {}
t.@ = m

This is nice because it exposes the metatable system as structural
part of the language, e.g. you can not bypass it by re-defining
set/getmetatable. But for the same reason it is also less flexible.
Then, to regain this flexibility you have to invent some
__setmetatable, __getmetatable metamethod (say it 3 time quickly), to
hook into the system, with some weird result (to hook into the
metatable assignment you have to set a metatable with
__set/getmetatable method...).

By contrast, get/setmetatable are very simple and they keep all the
flexibility we need.
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org