[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: metatable-powered aliasing
- From: "Soni \"They/Them\" L." <fakedme@...>
- Date: Fri, 8 May 2020 12:48:46 -0300
in lua, everything is tables, so I can do this:
mt = {}
mt.__index = function(t, k)
if k == "colour" then
return t.color
end
-- optionally do other stuff
return nil
end
mt.__newindex = function(t, k, v)
if k == "colour" then
t.color = v
end
rawset(t, k, v)
end
and everything works! and this just isn't possible in (most) other
languages. (side note: would be nice if the return value of __newindex
could be used instead of calling rawset. so e.g. if you return true, it
rawsets, otherwise it doesn't. anyway...)
... well almost everything: a proper solution would have to scan the
table before setting the metatable, to handle the case where the table
was created with "colour" instead of "color". however, I think this is a
pretty good start for a potential "accessibility module" for Lua.
thoughts? :3
(motivation: real ppl, working on real projects, found it extremely
frustrating to deal with an API using both spellings of "color". this
would solve that problem by allowing them to pick one and stick with it,
regardless of whatever mess upstream modules are using. well, upstream
modules would have to explicitly support it, but they could use the
spellings however they want internally, and still expose a consistent
API. and it's the public API that matters.)
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org