[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Ask userdata type from Lua
- From: Andrew Gierth <andrew@...>
- Date: Sat, 01 Jul 2023 00:05:30 +0100
>>>>> "bil" == bil til <biltil52@gmail.com> writes:
bil> Hi,
bil> is there some preferred / recommended way, to ask for the type of
bil> a userdata inside some Lua program?
Best method: the code creating the userdata should provide a library
function (not a method) something like mylib.type(x) which returns a
suitable string if x is a userdata of (one of) the library's types and
nil otherwise. This is for example what cqueues does (and see also
math.type in lua 5.3+).
bil> Or should I best present a "tostring metafunction" for each of my
bil> userdata,
Having a __tostring metamethod (and a __name metavalue) is useful in
general, but you should not rely on it for type identification. You also
should not assume that you can get at the debug library, since you may
be running in an environment in which it is absent for security reasons.
--
Andrew.