lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Jun 20, 2022 at 08:24:04PM +0700, Budi wrote:
> How to determine the correct data type used in function argument? e.g.
> 
> function incr (n)
>  -------------------    which type of what?
>  n = n .. "foo"
>  n = n + 1
> end

Something like this?

function incr(n)
	if type(n) == "string" then
		n = n .. "foo"
	elseif type(n) == "number" then
		n = n + 1
	else
		return nil, "unhandled type " .. type(n)
	end

	return n
end

B.