You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
local interface IFoo
get_type: function(self): string
end
local record Foo is IFoo where self:get_type() == "foo"
end
function Foo:get_type():string
return "foo"
end
function Foo.new():Foo
return setmetatable({}, { __index = Foo })
end
local function create_foo():IFoo
return Foo.new()
end
local function process_foo<T>(foo:IFoo):T
-- This works:
-- return foo as T
-- This does not:
-- Does not compile due to errors:
-- "cannot resolve a type for foo here"
-- "foo (of type IFoo) can never be a T"
assert(foo is T)
return foo
end
local foo1 = create_foo()
local _foo2:Foo = process_foo(foo1)
assert(foo1 is Foo) -- this works fine
The text was updated successfully, but these errors were encountered:
foo is T is an odd construct, because, since T is an unconstrained type variable, there is no reasonable Lua-expansion for that expression that would produce a valid runtime assertion. It might be the case that we should detect that and provide a better error message.
Note that unlike, say, Rust, we do not monomorphize generic functions1, so the fact that process_foo is used with the concrete Foo type is not enough to direct code generation for that assert() line.
1 - that is, we do not generate a non-generic copy of the function for each concrete type the generic function is applied to.
Marking this as a "good first issue": detecting that the rb type is a TypeVarType in the if node.op.op == "is" section and then reporting that is cannot be applied to type variables shouldn't be too hard!
Example:
The text was updated successfully, but these errors were encountered: