[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Named metatables (type-named data), similar to typedef of structs in C
- From: bil til <flyer31@...>
- Date: Thu, 24 Oct 2019 21:19:09 -0700 (MST)
Hi,
I think it would be very nice, if the function setmetatable( table,
metatable) would support also the case, that the 2nd parameter metatable is
a string.
Thus then there needs to be way to link a string (or a name) to a metatable
in some fast way - maybe this could be done internally through the registry.
Background info: After my horrific "tweak _tt to get 46bit numbers in LUA32"
has failed, as in lua 5.4 the __tt was shrinked from int to byte (thank you
for this, I assume this will shrink the RAM consumption of lua considerably,
very nice for microcontroller applications...), I am still thinking how to
best support in some "standard lua way" (thus if possible without C
userdata) int64 time or encoder values in lua32. This would have no need to
be speed optimized, but at least some "flexible way", which can be easily
handled by the lua user.
If such a metatable could be e. g. called "timetype", then I could usually
hold all time-date long ints in some "2-integer" table (hi and low value of
this int64, separated in two int32 values). And to add them conveniently, I
would then say
setmetatable( table, "timetype")
And this metatable linked to "timetype" then would handle all the special
operations like add, sub etc. for int64 / long values... .
Until this point of course this could be done also without this "naming"
concept of metatables.
But I am also still in thinking about integrating such "time" tables in my
string.pack/unpack... . I will blog an updated pack/unpack spec in my blog
"string.pack with bit resolution" later today or tomorrow. But in any case
besides bit resolution support (+unique format string definition for
lua32<>lua64), I would also very much like to support table parameters in
pack/unpack.
If named metatables are possible, then I could specify this in the FORMAT
string in a form "t'typename'", this would be very nice.
(for packing, I could find other methods, e. g. look for a metamethod
"__pack" in the metatable - the pack furnction gets the table over the
stack, so the pack function can easily check whether this table has some
metatable or not assigned. ... but for unpacking, this will NOT work, as for
unpacking the "metamethod-type" must be specified in the format string - it
could also be formatted in the packed string, this also would be another
option ... but anyway in this case also it should be possible to be "named",
especially if I want to use it to transfer data from one lua-system to
another... ).
Another application where this would be nice, is the function pair
string.format and string.tonumber. string.tonumber does not exist yet, but
it would be really nice to have a function string.tonumber(str, fmt), where
then fmt can be nil for the standard formatiing, but it could also be "%f" /
"%d" to force return of float or integer, and then it would be nice to have
a format "%t'typename'", as well as it would be nice to have this
"%t'typename'" possibility to format table elements in str.format. (this
functions then could look for some metatable support for "__tostring" or
"__tonumber" or something similar...).
... or is this a crazy idea of a lua newbee... possibly working already by
some other method?
--
Sent from: http://lua.2524044.n2.nabble.com/Lua-l-f2524044.html