LuaLS / LuaLS/lua-language-server

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

Open
#1,505 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

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.

```lua
---@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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Foo/Bar Lua example in the issue and trace how @return annotations and inherited methods are interpreted. Done means a constructor inherited by Bar infers its result as Bar rather than Foo, with coverage for the demonstrated behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.