Is it possible to detect if the argument in the function is a "no value" (or "void") as opposed to a "nil" value?

Let’s pick a random builtin function, math.abs.
If it did this:

math.abs();

It’ll throw math.abs():1: missing argument #1 (number expected).
but
if I did this:

math.abs(nil);

It throws math.abs(nil):1: invalid argument #1 (number expected, got nil) instead

Is it possible to achieve detect this in a function for my curiousity?
IIRC If no argument are inputted in function it’ll return nil

function a(b)
	print(b);
end;
a(); --> nil
a(nil); --> nil

but math.abs and many other builtin functions seems to bypass this as proven by the error message.
I also noticed that return instead of return nil returns a void value

function a()
	return;
end;
math.abs(a());

throws math.abs():1: missing argument #1 (number expected).
but

function a()
	return nil;
end;
math.abs();

throws math.abs(nil):1: invalid argument #1 (number expected, got nil)

1 Like

You can use the ellipsis ... and select along with it.

local function argc(...)
    return select("#", ...)
end

print(argc("hi", 5, false)) -- 3
print(argc()) -- 0
print(argc(nil)) -- 1

So if select("#", ...) is 0 then no arguments were passed.

3 Likes