Closed unittensor closed 1 month ago
In your case, all of these are equivalent:
-- case 1
asd(function<boolean>(Toggled: boolean)
BoolValue(Toggled)
end)
-- case 2
asd(function<T>(Toggled: T)
BoolValue(Toggled)
end)
-- case 3
local function callback<T>(Toggled: T)
BoolValue(Toggled)
end
asd(callback)
This would be a good counterexample:
asd(function<{ x: number }>(Toggled) -- SyntaxError: Expected identifier, got '{'
print(Toggled)
end)
In case 1, the type in the angle brackets is not referring to the concrete type boolean = true | false
, but a new type created by the anonymous function definition. In general, the angle brackets in a function definition define type parameters, they don't refer to existing types.
The best workaround I can think of is moving the type parameter to asd
instead of Callback
and passing the parameter value to asd
directly.
--!strict
type Callback<T> = (Toggled: T) -> ()
local function asd<T>(Callback: Callback<T>, Value: T)
Callback(Value)
end
local BoolValue: (boolean) -> () = print
local NumberValue: (number) -> () = print
asd(BoolValue, true)
asd(NumberValue, 1)
It would probably be a good idea to "reserve" existing types from type parameters so they can't be confused.
Since this is user error, I'm going to close out this issue. You can open a new issue if you want to suggest some particular behavior for naming a generic the same thing as a primitive type, but the type error in this case is right.
Since this is user error, I'm going to close out this issue. You can open a new issue if you want to suggest some particular behavior for naming a generic the same thing as a primitive type, but the type error in this case is right.
Ah! thank you, thats my fault for the misunderstanding
With the code below in strict mode an error is prompted "Type 'T' could not be converted into 'T'". Im able to replicate this issue multiple times:
Example 1 using
T
asboolean
:Example 2 using
number
asT
generic:Workarounds? Change the required function argument type of
BoolValue
toany
: