Named metatables (type-named data), similar to typedef of structs in C

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Named metatables (type-named data), similar to typedef of structs in C

bil til
This post was updated on .
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 table must be CREATED by the unpack function, so any
necssary "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

Reply | Threaded
Open this post in threaded view
|

Re: Named metatables (type-named data), similar to typedef of structs in C

Javier Guerra Giraldez
On Thu, 24 Oct 2019 at 23:19, bil til <[hidden email]> wrote:
>
> 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.


variables are names:

local timetype = { __tostring = timetostring, __add = addtwotimes }

local starttime = setmetatable ({hour=13, minute=25}, timetype)
local endtime = setmetatable({hout=16, minute=12}, timetype)



--
Javier

Reply | Threaded
Open this post in threaded view
|

Re: Named metatables (type-named data), similar to typedef of structs in C

bil til
Thank you... so this was silly question, I hope I understand now ...

So if my transmitter wants to pack with timetype info, this is straight
forward, as timetype is linked to starttime and endtime, and transmitter
pack function gets the starttime and endtime.

And if the receiver lua device wants to unpack and has "timetype" string
info in the formatstring somehow, the receiver can create the corresponding
table quite easily like this (timetype needs to be globally defined then,
but this is anyway clear in my application):

strTypeInfo= "timetype"
...the receiver extracts this somehow from the format string, and then
somehow gets these two tables starttime, endtime, and then it can use the
following to connect starttime, endtime to the timetype timeinfo:
lua_getglobal( strTypeInfo)
lua_setmetatable( ...)

... very neat .. I hope I REALLY got this and did not overlook anything...



--
Sent from: http://lua.2524044.n2.nabble.com/Lua-l-f2524044.html

Reply | Threaded
Open this post in threaded view
|

Re: Named metatables (type-named data), similar to typedef of structs in C

bil til
In reply to this post by Javier Guerra Giraldez
One further question:

My function "pack" gets the variables starttime and endtime, and through
getmetatable it gets this metatable typeinfo.

Is there a straight forward lua function, to get the name "typeinfo" of this
metatable as string, or would you recommend to look through _G to get this
"typeinfo" string? Or any more easy possibility?



--
Sent from: http://lua.2524044.n2.nabble.com/Lua-l-f2524044.html