ChatGGPT
(Chat GPT)
June 15, 2025, 6:46pm
1
I am trying to make a function that takes in any function as its first parameter and then returns that same function But I cannot figure out the type checking for it
I could do
function foo<A..., R...>(func: (A...) -> (R...)): (A...) -> (R...)
return func
end
But it does not support arguments and also it is too long and unreadable
Pls help! Any support would be appreciated
averlst
(aver)
June 15, 2025, 6:53pm
2
You could try letting the type solver infer it.
function foo<A..., R...>(func: (A...) -> (R...))
return func
end
ChatGGPT
(Chat GPT)
June 15, 2025, 6:55pm
3
Yeah but now it is not showing arguments
hazame4
(Linus_TechTips)
June 15, 2025, 6:55pm
4
Does adding an “?” to the first R fix it? (in the previous code)
averlst
(aver)
June 15, 2025, 6:56pm
6
I’m not sure if the type solver is able to do that.
ChatGGPT
(Chat GPT)
June 15, 2025, 7:00pm
8
Now it is just doing this
(I put an extra ? at the end of the Second R because it wouldn’t stop warning without it)
averlst
(aver)
June 15, 2025, 7:07pm
9
Seems like it does carry arguments over (somewhat), but no documentation.
1 Like
The only way to do it, is like this:
function a<Args..., Rets...>(f: (Args...) -> (Rets...)): (Args...) -> (Rets...)
return f
end
local b = a(function(hi: string): number
return 0
end)
b() -- (string): number
This method does support the types of the arguments.
1 Like
averlst
(aver)
June 15, 2025, 7:11pm
11
You do lose all the argument names and documentation unfortunately.
1 Like
That is intentional for now. Function types do not hold the information of the argument names, only the types. And that is enough in most cases.
1 Like
ChatGGPT
(Chat GPT)
June 17, 2025, 1:30am
13
That is just the same thing as this
Chat GPT:
I could do
function foo<A..., R...>(func: (A...) -> (R...)): (A...) -> (R...)
return func
end
But it does not support arguments and also it is too long and unreadable
ChatGGPT
(Chat GPT)
June 17, 2025, 1:34am
14
Also I think I kind of found a solution
function foo<T> (func: T & (...any) -> (...any)): T
return func
end
But the only problem is that It autocompletes the members of the first parameter even if it is not a function
But At least it warns the user if the first parameter is not a function
I will not mark this as solution yet because there is Probably a better way to do this