Skip to content
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

Some trouble with inheritance of interface #879

Open
fperrad opened this issue Dec 22, 2024 · 2 comments
Open

Some trouble with inheritance of interface #879

fperrad opened this issue Dec 22, 2024 · 2 comments
Labels
semantics Unexpected or unsound behaviors

Comments

@fperrad
Copy link
Contributor

fperrad commented Dec 22, 2024

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

@hishamhm
Copy link
Member

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 hishamhm added the semantics Unexpected or unsound behaviors label Dec 22, 2024
@fperrad
Copy link
Contributor Author

fperrad commented Dec 22, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semantics Unexpected or unsound behaviors
Projects
None yet
Development

No branches or pull requests

2 participants