Suggestion: type(v, true) returns more detailed type

76 views
Skip to first unread message

Bruno Silvestre

unread,
Jul 16, 2025, 11:34:15 AMJul 16
to lu...@googlegroups.com
Hi,

Any chance of adding a boolean parameter to the type() function to return the more specific type? This would align with the "lua_is*()" functions from C and allow checking for more specific types in Lua code.

For example:
type(3) -- "number"
type(3, true) -- "integer"
type(3.14) -- "number"
type(3.14, true) -- "number"
type(lu) -- "userdata"
type(lu, true) -- "lightuserdata"
type(u) -- "userdata"
type(u, true) -- "userdata"
type(cf) -- "function"
type(cf, true) -- "cfunction"

regards
--
brunoos

Sainan

unread,
Jul 16, 2025, 11:42:50 AMJul 16
to lu...@googlegroups.com
Could theoretically be implemented in pure Lua using math.type for int/float distinction, debug.getinfo to check for C functions, and pcall + setmetatable will give you the full type name of "light userdata".

-- Sainan

Sainan

unread,
Jul 16, 2025, 11:48:44 AMJul 16
to lu...@googlegroups.com
function typex(v)
local t = type(v)
if t == "number" then
return math.type(v)
elseif t == "function" then
if debug.getinfo(v).short_src == "[C]" then
return "cfunction"
end
elseif t == "userdata" then
if string.find(select(2, pcall(setmetatable, v)), "got light userdata") then
return "lightuserdata"
end
end
return t
end

-- Sainan

Tomás Guisasola

unread,
Jul 16, 2025, 11:50:46 AMJul 16
to lu...@googlegroups.com
Hi Bruno

> Any chance of adding a boolean parameter to the type() function to return the more specific type? This would align with the "lua_is*()" functions from C and allow checking for more specific types in Lua code.

Have you considered an alternative? type() could return two values:
the second one could be this more specific type.

Regards,
Tomás

Dmitry Meyer

unread,
Jul 16, 2025, 12:05:57 PMJul 16
to lu...@googlegroups.com
On 2025-07-16 15:50, Tomás Guisasola wrote:

> Have you considered an alternative? type() could return two values:
> the second one could be this more specific type.

The change you've suggested is more invasive, some code may break if
type() suddenly starts to return two values instead of one.

Spar

unread,
Jul 16, 2025, 12:13:44 PMJul 16
to lu...@googlegroups.com
typex is everything you need.
What would be better if type could be overridden by metamethods
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/43f1d6d2-5589-4812-abfd-657d3cb303c1%40undef.im.

Bas Groothedde

unread,
Jul 16, 2025, 12:13:55 PMJul 16
to lu...@googlegroups.com

> On 16 Jul 2025, at 18:06, Dmitry Meyer <m...@undef.im> wrote:
To be fair, there might be a lot of code that is written with the expectation that ‘type’ accepts one argument only. ‘type(func_with_2_results())’ might also break due to this change. Still, I think something like the OP suggestion would be useful.

~b

eugeny gladkih

unread,
Jul 16, 2025, 1:29:03 PMJul 16
to lu...@googlegroups.com
imagine existing code

local a, b = type(some), 1

--
Yours sincerely, Eugeny.


Sainan

unread,
Jul 16, 2025, 1:50:26 PMJul 16
to lu...@googlegroups.com
> local a, b = type(some), 1

Strange example because the return of type(some) will take up exactly 1 value, so b will always be 1.

Mulret only works on the last expression in a list.

-- Sainan

Bruno Silvestre

unread,
Jul 16, 2025, 2:20:41 PMJul 16
to lu...@googlegroups.com
'typex' was my first option (in line with lua_tointegerx), but then I thought keeping 'type' would be more intuitive and avoid a new function.

The problem is the legacy code... I forget that people play around with Lua's parameter and return semantics a lot (the "A20 line" of Lua).

Well, I stick to the idea, even if the mechanism would be different.

regards
--
bruno

Tomás Guisasola

unread,
Jul 16, 2025, 3:02:24 PMJul 16
to lu...@googlegroups.com
Hi

> > local a, b = type(some), 1
>
> Strange example because the return of type(some) will take up exactly 1 value, so b will always be 1.
>
> Mulret only works on the last expression in a list.

I doubt there are any declaration like:

local a, b = type(some)

But am not certain about:

local t = { type(some) }

Or:

f(type(some)) -- in the case that f accepts more than one value

Although I recognize this proposal (to let type return two values)
would introduce incompatibility, I don't think it would be a big
problem at all. Besides, I consider a boolean argument a very bad
choice, and also consider a second function (to work as almost the
same as the actual `type()`) as a waste. But this is a matter of
taste. Won't add much more to this discussion, though.

Regards,
Tomás

Bruno Silvestre

unread,
Jul 16, 2025, 4:09:09 PMJul 16
to lu...@googlegroups.com
If the information can be quickly computed, it's a good idea... and using metamethods, one can return: type, subtype, subsubtype, etc.

I don't know if several subtypes are useful, but the flexibility is classy.
--
bruno


--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.

eugeny gladkih

unread,
Jul 16, 2025, 7:08:40 PMJul 16
to lu...@googlegroups.com
indeed, just drop “, 1” expecting b to be nil

--
Yours sincerely, Eugeny.


Reply all
Reply to author
Forward
0 new messages