teal-language/tl

Type identifying where clause doesn't work when concrete type is provided as a generic argument

Offen

#920 geöffnet am 26.01.2025

 (6 Kommentare) (0 Reaktionen) (1 zugewiesene Person)Lua (160 Forks)batch import
good first issuesemantics

Repository-Metriken

Stars
 (2.810 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 1T 6h) (1 gemergte PR in 30 T)

Beschreibung

Example:

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

Contributor Guide