Feature Request: Typestate / Self-Type Refinement for Method Calls
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Accessibilité débutants
- 45/100
Piste de recherche
L’issue ne nomme aucun fichier d’implémentation ni aucun test. Commencez par examiner comment LuaLS gère actuellement les ---@cast explicites et les appels de méthodes, puis évaluez la portée de l’affinement proposé : appels directs, portée actuelle et annotations explicites. Le travail sera considéré comme terminé lorsqu’un design d’annotations sera arrêté, avec un affinement des capacités sensible au flux, la complétion automatique et les diagnostics fonctionnant sans cast manuel.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Problem
Lua is a highly dynamic language, and many real-world Lua frameworks mutate object capabilities at runtime.
This pattern is especially common in:
- game engines
- UI frameworks
- ECS architectures
- builder APIs
- runtime mixin systems
- userdata bindings from native engines
Currently, LuaLS/LuaCats cannot properly express:
"after calling this method,
selfnow has additional capabilities/methods"
This becomes a major DX limitation for frameworks that dynamically extend objects.
Real-world Example
Consider a UI framework where all elements start as a generic UIElement.
---@class UIElement
local UIElement = {}
Calling:
element:setupUIImage()
injects image-related functionality into the element at runtime:
element:setImage(...)
However, LuaLS still sees element as only UIElement.
The only current workaround is manual casting:
element:setupUIImage()
---@cast element UIImage
element:setImage(material)
This works technically, but becomes repetitive and hurts DX significantly in large codebases.
Comparison to TypeScript
TypeScript supports type refinement after method calls through assertion signatures such as:
asserts this is SomeType
This allows APIs to safely refine object types after capability-changing methods.
A good real-world example is discord.js, where Interaction objects can be refined into subtypes through methods like:
interaction.isButton()
interaction.isChatInputCommand()
After refinement, TypeScript understands the new subtype automatically.
Lua frameworks often use similar runtime patterns, but LuaLS currently cannot model them.
Proposed Solution
Introduce a LuaCats annotation for self-type refinement / capability injection.
Possible syntax examples:
---@injects UIImage
function UIElement:setupUIImage() end
or:
---@mutates self UIImage
function UIElement:setupUIImage() end
or:
---@becomes UIImage
function UIElement:setupUIImage() end
Expected Behavior
After:
element:setupUIImage()
LuaLS would understand:
element :: UIElement & UIImage
allowing:
element:setImage(...)
without requiring manual casts.
Why This Matters
This would greatly improve support for:
- dynamic Lua architectures
- runtime capability injection
- engine bindings
- UI frameworks
- builder APIs
- fluent APIs
- mixin systems
while keeping Lua's dynamic nature intact.
This feature would also reduce:
- repetitive
---@cast - noisy annotations
- inaccurate supersets in base classes
- degraded autocomplete quality
Additional Notes
This feature does not need to become full typestate analysis or advanced dependent typing.
Even a pragmatic flow-sensitive refinement system limited to:
- direct method calls
- current scope
- explicit annotations only
would already provide massive DX improvements.
Example Use Case
---@class UIImage
---@field setImage fun(self: UIImage, material: string)
---@class UIElement
---@injects UIImage
function UIElement:setupUIImage() end
local element = UIElement.new()
element:setupUIImage()
element:setImage("icon")
Expected:
- autocomplete works
- diagnostics recognize
setImage - no manual cast required
Thanks for all the amazing work on LuaLS and LuaCats.
This would significantly improve tooling support for dynamic Lua patterns commonly used in real-world frameworks and game engines.
- Langage dominant
- Lua
- Étoiles
- 4.4k
- Forks
- 442
- Merge moyen
- 8 j 9 h
- PR mergées (30 j)
- 1
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de LuaLS/lua-language-server
-
enhancement
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
LuaLS/lua-language-server#1776 ·
-
泛型for迭代器的类型推导漏掉了带__call的类 Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 65/100
LuaLS/lua-language-server#3463 · 5 commentaires ·
-
Difficulté 4/5 3-5 jours Accessibilité débutants 55/100
LuaLS/lua-language-server#3461 ·
-
Difficulté 3/5 1-2 jours Accessibilité débutants 68/100
LuaLS/lua-language-server#3460 · 1 commentaire · 1 réaction ·
-
Difficulté 3/5 1-2 jours Accessibilité débutants 68/100
LuaLS/lua-language-server#3459 · 1 réaction ·
Toutes les issues de LuaLS/lua-language-server
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
openSUSE/python-rpm-macros#219 ·
-
bug mini.bracketed
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
-
accessibility bug typst
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
quarto-dev/quarto-cli#14947 · 2 commentaires ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
HenrikBengtsson/CBI-software#134 ·
-
Nmap
Difficulté 1/5 Moins d'une heure Accessibilité débutants 85/100