Skip to content

FR: Allow 'self' return type to allow return type changing with inheritance #1505

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

Open
paul-reilly opened this issue Aug 29, 2022 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@paul-reilly
Copy link

When annotating that one class has inherited from another, there is currently no way to annotate a function that returns it's current type that the current type should change when the class is inherited. Something along these lines, or something that accomplishes the same goal would be fantastic.

---@class Foo
Foo = {}

---@return self    --<-- 'self' refers to Foo so Foo.new() returns Foo
function Foo.new() end

---@class Bar : Foo
Bar = {}

local b = Bar.new() --<-- Bar.new() is inherited so the 'self' return type is now Bar
-- b should now be type Bar

Thanks!

@sumneko sumneko added the enhancement New feature or request label Aug 29, 2022
@carsakiller
Copy link
Collaborator

This is a bit of a hacky way around this for now:

---@class Foo
Foo = {}

---@return Foo
function Foo.new() end

---@class Bar : Foo
---@field new fun(): Bar
Bar = {}

local b = Bar.new()

By assigning the @field of new to a function that returns Bar, we are able to overwrite the return type.

@paul-reilly
Copy link
Author

By assigning the @field of new to a function that returns Bar, we are able to overwrite the return type.

Thanks, that's definitely a lot better doing that once at the definition rather than casting and overriding warnings when creating objects.

@sumneko sumneko added this to the 3.6.0 milestone Sep 19, 2022
sumneko added a commit that referenced this issue Nov 3, 2022
@sumneko
Copy link
Collaborator

sumneko commented Nov 3, 2022

The generic inheritance has not been implemented for the time being.

@sumneko sumneko modified the milestones: 3.6.0, 3.7.0 Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants