tl icon indicating copy to clipboard operation
tl copied to clipboard

Some trouble with inheritance of interface

Open fperrad opened this issue 11 months ago • 2 comments

local interface ifoo
   f_bar: function(obj: ifoo)
   f_baz: function(obj: ifoo)
   f_bazz: function(obj: ifoo)
   m_bar: function(self: ifoo)
   m_baz: function(self: ifoo)
end

local record foo is ifoo
   -- methods seem inherited with a transformation `self: ifoo` --> `self: foo`
   -- functions seem raw inherited
   f_bazz: function(obj: foo)  -- function could be overloaded in a compatible way (foo is an ifoo)
end

function foo.f_bar (obj: ifoo)
   print(obj)
end

function foo.f_baz (obj: foo)  -- type signature of 'f_baz' does not match its declaration in foo: argument 1: foo is not a ifoo
   print(obj)
end

function foo.f_bazz (obj: foo)
   print(obj)
end

function foo.m_bar (self: ifoo)  -- NO ERROR: but ifoo is not an instance, and this behavior is well documented
   print(self)
end

function foo.m_baz (self: foo)
   print(self)
end

After some thinging, the behavior is correct (function must be overloaded like f_bazz). But the message causes some trouble : foo is not a ifoo

fperrad avatar Dec 22 '24 17:12 fperrad

To get the "transformation" on the inheriting type, you can use the special type self in the interface definition (f_bazz: function(obj: self)).

Thanks for the feedback on the error message: it would probably be better to say instead got foo, expected ifoo.

hishamhm avatar Dec 22 '24 18:12 hishamhm

self usually brings the message invoked method as a regular function: consider using ':' instead of '.'

fperrad avatar Dec 22 '24 18:12 fperrad