LuaLS / LuaLS/lua-language-server
[Request] Improve support for custom number types
まだ誰も着手していません。
- 主要言語
- Lua
- スター
- 4.4k
- フォーク
- 442
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
In the builtin definitions, the integer type is defined as a child class of the number type:
---@class integer : number
Despite that, it is still possible to write such code without any type-checking errors:
---@param lives integer
local function printLives(lives)
print(lives)
end
---@type number
local lives = 9.0
printLives(lives)
I think this is fine, it makes sense.
However, this behaviour seems to be hardcoded in the extension.
Many of the functions from the API I use expect fixed-point values as parameters; such arguments are indicated by the fixed type, which is an alternate number type defined in much the same way as integer:
---@class fixed : number
To represent literal fixed-point values, the API provides a unit contant which is used something like this:
move(4*UNIT, 10*UNIT, UNIT*3/2) -- 40.0, 100.0, 1.5
Technically, fixed is just a regular number, however it is extremely helpful to the user to see this:

rather than this:

In older versions of the extension, this works perfectly fine; however, recent versions perform type-checking, which is a very useful feature but does not work nicely with the fixed type:

It looks like the extension considers number * fixed to be a number, which is a reasonable guess, but causes the error shown in the image above.
To solve this, I have thought of the following solutions:
- Completely disabling type-checking. It works, obviously, but it would be preferable to not have to give up on such a useful feature.
- Manually casting every literal value. This works too, but is extremely unpractical and results in bloating code.
- Somehow make it so that
number * fixedresolves tofixedinstead ofnumber. But that sounds like a very arbitrary solution to me. - Defining
fixedas an alias:---@alias fixed number. It prevents the error from occuring, and the code is readable, but the completion/hover hints show "number" instead of "fixed", like in the second image. - Forking the extension to add a special case in the type checker.
There may also be other solutions I am not aware of, but my first impression is that solution 4 suits my usecase best, and would probably be perfect if there was some way to make an alias that behaves like in the first image (much like typedef in C, for instance).
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
integer と number の組み込み定義から始め、fixed などのカスタム数値型を含む算術演算に対する型チェッカーの動作を追跡します。カスタム数値型が、casts を必要とせずに有用な型チェック情報と補完またはホバー情報を引き続き保持し、既存の integer の動作も維持されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- lua
- 領域
- devtools
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 25/100