-
Notifications
You must be signed in to change notification settings - Fork 118
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
Bug: using interface in map as function parameter fails to compile #914
Comments
local interface A
end
local record B is A
end
local c: B
local d: {B}
local function Test_1(a: A)
end
local function Test_2(a: {A})
end
Test_1(c) -- Compiles fine
Test_2(d) -- Compiles fine And it seems not bugged for all types. For example, if nested in array, both compile without error. |
local interface A
end
local record C is A
end
local c: C
local function Test(a: {string: A})
end
Test(c) --- Compiles fine. Why? And as I kept testing, more weird stuff happened... I have no idea why this is valid. |
Can you test it with the branch from PR #911 ? thank you! |
Sorry, the test case I presented above is misleading, this is actually nothing to do with generic. Seems using interface in map just causes problem. local interface A
end
local record B is A
end
local c: B
local d: {string: B}
local function Test_1(a: A)
end
local function Test_2(a: {string: A})
end
Test_1(c) -- Compiles fine
Test_2(d) -- Error: argument 1: in map value: B is not a A |
@hishamhm Hi, any updates on this issue? |
I haven't been able to take a closer look at this yet. Thank you for the test cases! |
This happens because, as of today, Teal uses invariant typing for maps — that is, map keys and values types must be identical. This is the rationale why: local interface Animal
name: string
end
local interface Bird is Animal
end
local interface Cat is Animal
end
local cucko: Bird = { name = "Cucko" }
local dodos: {Animal: Bird} = {}
local function Test_1(a: Animal)
print("hello, " .. a.name)
end
local function Test_2(a: {string: Animal})
local f: Cat = { name = "Felix" }
a["felix"] = f
end
Test_1(cucko) -- Compiles fine
Test_2(dodos) -- If this compiled, we would have stored a cat in a map of birds! The two possible solutions for this problem are:
Solution 1 is what Java does for managing generics with the |
(also note that there is an existing workaround which makes your test case compile: local function Test_2<T is A>(a: {string: T}) that makes the map value type specialize at the call site, and that makes the invariant check pass. However, this still allows the "store cat in the map of birds" problem I mentioned before. So in that regard we're already being unsafe, which is a further argument for just going with solution 2. |
@hishamhm Thanks for your clarification. Personally, I am also in favor of solution 2. If we already know |
@BLKTower regarding your "weird issue" here is a simple version: local record R
end
local x: R = {}
local function test(a: {string: R})
end
test(x) This works, but if you add any field to the record R it does not. This makes sense because a record with no field is basically an empty map, and so it matches the type of the argument. Similarly this works (as intended): local record R
end
local record R2
foo: R
end
local x: R2 = {}
local function test(a: {string: R})
end
test(x) |
@catwell Hi, thanks for your explanation. Though, I still feel such |
Not sure how I feel about the fact that it works transparently, but what about my second example? Would you also want |
IMHO, yes, I do want local record R
end
local record R2
foo: R
end
local x: R2 = {}
local function test(a: {string: R})
a["foo2"] = {}
end
test(x)
local r = x.foo2 -- Error: invalid key 'foo2' in record 'x' of type R2 It's valid to add an entry |
I thought about it a bit and I think it should probably not work without a cast, because then @BLKTower your remark about other languages made me think of this because that is the reason why in TypeScript |
I see. TIL, I can do |
Since this issue is closed and about a different problem I think you should create a new ticket for this one so @hishamhm can track it. |
Seems using interface in map just causes compiler error:
If changed map to array, both compile fine:
Desired behavior:
Test_2
compiles without error.The text was updated successfully, but these errors were encountered: