LuaLS / LuaLS/lua-language-server

[Request] Improve support for custom number types

オープン
#1,904 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

feat/LuaCats Annotations question
主要言語
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:
image
rather than this:
image

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:
image
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:

  1. Completely disabling type-checking. It works, obviously, but it would be preferable to not have to give up on such a useful feature.
  2. Manually casting every literal value. This works too, but is extremely unpractical and results in bloating code.
  3. Somehow make it so that number * fixed resolves to fixed instead of number. But that sounds like a very arbitrary solution to me.
  4. Defining fixed as 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.
  5. 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).

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

integernumber の組み込み定義から始め、fixed などのカスタム数値型を含む算術演算に対する型チェッカーの動作を追跡します。カスタム数値型が、casts を必要とせずに有用な型チェック情報と補完またはホバー情報を引き続き保持し、既存の integer の動作も維持されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
lua
領域
devtools
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。