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
-- Not using generics works as expected:-- local interface IFoo-- get_value: function(self): integer-- end-- local record Foo is IFoo-- _value: integer-- end-- function Foo:get_value():integer-- return self._value-- end-- function Foo.new(value: integer):Foo-- local fields = { _value = value }-- return setmetatable(fields, { __index = Foo })-- end-- local function create_foo(value: integer):IFoo-- local foo = Foo.new(value)-- return foo-- end-------------------------- But with generics we get error:-- "in return value: got Foo<T>, expected interface IFoo"-- in create_foo functionlocalinterfaceIFoo<T>get_value: function(self): TendlocalrecordFoo<T>isIFoo<T>_value: TendfunctionFoo:get_value():Treturnself._valueendfunctionFoo.new(value: T):Foo<T>localfields= { _value=value }
returnsetmetatable(fields, { __index=Foo })
endlocalfunctioncreate_foo<T>(value: T):IFoo<T>localfoo=Foo.new(value)
returnfoo-- Have to do this instead for now:-- return foo as IFoo<T>end------------------------localfoo=create_foo(5)
print(foo:get_value())
The text was updated successfully, but these errors were encountered:
Example below:
The text was updated successfully, but these errors were encountered: