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

Type 'T' could not be converted into 'T' #1256

Closed
unittensor opened this issue May 14, 2024 · 3 comments
Closed

Type 'T' could not be converted into 'T' #1256

unittensor opened this issue May 14, 2024 · 3 comments
Labels
invalid This doesn't seem right

Comments

@unittensor
Copy link

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 as boolean:

--!strict

type Callback = <T>(Toggled: T) -> ()

local function asd(Callback: Callback)
	Callback(true)
end

local function BoolValue(Value: boolean)
	print(Value)
end

asd(function<boolean>(Toggled: boolean)
	BoolValue(Toggled) --Type 'boolean' could not be converted into 'boolean'
end)

Example 2 using number as T generic:

--!strict

type Callback = <T>(Toggled: T) -> ()

local function asd(Callback: Callback)
	Callback(1)
end

local function BoolValue(Value: number)
	print(Value)
end

asd(function<number>(Toggled: number)
	BoolValue(Toggled) --Type 'number' could not be converted into 'number'
end)

Workarounds? Change the required function argument type of BoolValue to any:

- local function BoolValue(Value: number)
+ local function BoolValue(Value: any)
--!strict

type Callback = <T>(Toggled: T) -> ()

local function asd(Callback: Callback)
	Callback(1)
end

local function BoolValue(Value: any) --👈
	print(Value)
end

asd(function<number>(Toggled: number)
	BoolValue(Toggled) --👌
end)
@unittensor unittensor added the bug Something isn't working label May 14, 2024
@goldenstein64
Copy link

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.

@aatxe aatxe added invalid This doesn't seem right and removed bug Something isn't working labels May 22, 2024
@aatxe
Copy link
Collaborator

aatxe commented May 22, 2024

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.

@aatxe aatxe closed this as completed May 22, 2024
@unittensor
Copy link
Author

unittensor commented May 25, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Development

No branches or pull requests

3 participants