LuaLS / lua-language-server

A language server that offers Lua language support - programmed in Lua
https://luals.github.io
MIT License
3.35k stars 318 forks source link

Be confused to Backticks #2949

Open ChouUn opened 2 days ago

ChouUn commented 2 days ago

Issue Description

I noticed two examples about backticks. The first is in @generic Capture with Backticks

---@class Vehicle
local Vehicle = {}
function Vehicle:drive() end

---@generic T
---@param class `T` # the type is captured using `T`
---@return T       # generic type is returned
local function new(class) end

-- obj: Vehicle
local obj = new("Vehicle")

The second is in @param Generic Function Parameter

---@class Box

---@generic T
---@param objectID integer The ID of the object to set the type of
---@param type `T` The type of object to set
---@return `T` object The object as a Lua object
local function setObjectType(objectID, type) end

--> boxObject: Box
local boxObject = setObjectType(1, "Box")

Are there any differences between them?

---@return T
---@return `T`

Additional Notes

BTW, how can I annotate this:

---@class X
---@field A integer
---@field B string
---@field C boolean[]

---@param x X
---@param key string
---@return ?
function getter(x, key)
    return x[key]
end

Is it possible to use backticks?

CppCXY commented 2 days ago

应该没有区别

Issues-translate-bot commented 2 days ago

Sumneko Lua translate bot


There should be no difference

ChouUn commented 2 days ago

应该没有区别

I guess so too XD 我猜也是 XD I think it would be best to unify the usage, to reduce unnecessary ambiguity. 感觉最好能统一一下用法,减少不必要的歧义。 And it seems there is no good solution for the Additional Notes part? 另外 Additional 部分感觉一直没有很好的解决方式?

CppCXY commented 2 days ago

应该没有区别

I guess so too XD 我猜也是 XD I think it would be best to unify the usage, to reduce unnecessary ambiguity. 感觉最好能统一一下用法,减少不必要的歧义。 And it seems there is no good solution for the Additional Notes part? 另外 Additional 部分感觉一直没有很好的解决方式?

additional部分是什么意思

ChouUn commented 2 days ago

additional部分是什么意思

In the Additional Notes section of the body, what should the ---@return ? look like? Issue 正文中的 Additional Notes 那一节,请问 ---@return ? 部分应该怎么填

CppCXY commented 2 days ago

不过以后应该会出现类似:

---@param a `T`
---@return System.`T` 

这样的类型变换功能,

你要的功能在typescript里面叫做Indexed type, 这个实现看起来比较复杂如果实现了的话, 他有可能是:

---@param key `T`
---@return X[`T`]
Issues-translate-bot commented 2 days ago

Sumneko Lua translate bot


However, something like this should appear in the future:

---@param a `T`
---@return System.`T`

Such type conversion function,

The function you want is called Indexed type in typescript. This implementation looks more complicated. If implemented, it may be:

---@param key `T`
---@return X[`T`]
ChouUn commented 2 days ago

你要的功能在typescript里面叫做Indexed type

I am indeed an old user of Typescript, so asked this question. 我确实是 ts 的老用户,所以有此一问。 I think it is an important feature for annotations. 感觉还是比较重要的特性。

CppCXY commented 2 days ago

ts里面提供的类型体操要实现起来是比较麻烦的, 我看这一套体操, mapped type, indexed type, keyof type, extends type, 都要实现才能满足基本需求, luals在4.0有非常初步的泛型类支持, 那时候再讨论这些或许才有意义

Issues-translate-bot commented 2 days ago

Sumneko Lua translate bot


The type gymnastics provided in ts are more troublesome to implement. I think this set of gymnastics, mapped type, indexed type, keyof type, extends type, must be implemented to meet the basic needs. Luals has very preliminary generics in 4.0 Class support, it might make sense to discuss these at that time