Skip to content

Specify variable return type #1539

@carsakiller

Description

@carsakiller

Currently, you cannot specify the type (or name) of a variable @return:

---@return ... Description!

---This is not allowed
---@return ... string Description!

This problem is more complex than just adding that ability though, take string.match() as an example:

string.match() is defined as returning ..., meaning its returns are marked as unknown. When using the no-unknown diagnostic, this results in a warning/error. In reality, it can only ever return string(s) or nil:

local text = "Key: 123456789"

local match = string.match(text, "Key: (%d+)")
print(match) -- 123456789


local match, match2 = string.match(text, "(Key): (%d+)")
print(match, match2) -- Key, 123456789


local match = string.match(text, "Pass: (%d+)")
print(match) -- nil

So maybe a new syntax should be introduced:
---@return nil|string... match Description!
or
---@return string|nil ... Description!

This would then result in the following:

local match, match2 = string.match(foo, bar)
-- match -> nil | string
-- match2 -> nil | string

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationHas to do with documentation either in the wiki or in the repo

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions