Open rhys-vdw opened 3 hours ago
Why do you want to write an extra annotation? Isn't it better to just use field = 5?
Why do you want to write an extra annotation? Isn't it better to just use field = 5?
I am just trying to make the process of exporting from C a bit easier.
An example:
It would be convenient to be able to just copy this out into a meta.lua
file directly.
That said, I can generate Lua too, and maybe it's more appropriate.
I suggest you use AI to convert it directly:
I don't want to run AI in CI, I will just write a program to do it.
Thanks for the reply.
It's okay to close this issue if you don't like it. :-)
In LuaLS @enum
actually works like an @alias
, which it is just a type of union values: https://luals.github.io/wiki/annotations/#enum, as described in here: https://github.com/LuaLS/lua-language-server/issues/2914#issuecomment-2439798833, https://github.com/LuaLS/lua-language-server/issues/2820#issuecomment-2310034789
---@enum T
T = {
a = 1,
b = 2,
}
--> the same as
---@alias T 1|2
So the type in @enum
is just union of values, not a class container. When you specify an enum
type in function param, the union values are checked against, instead of checking the object type. In this sense, @field
seems just not fit in the concept of @enum
in LuaLS. 🤔
With that said, there is another feature request to Add ability to define an enum without using a table: https://github.com/LuaLS/lua-language-server/issues/2721, maybe it suits you better for this case and you would like to +1
to it. 😄
@tomlau10 thanks for the detailed response! However I don't believe either of these notations are sufficient. e.g.
---@enum MyBool
MyBool {
---This is true.
myTrue = 1,
---This is not true.
myFalse = 0
}
Where do I define the name and comment for myTrue
and myFalse
?
---@alias MyBool 1|0
But it's okay, I think it's fine to define a table. I have actually coded up a generator: https://github.com/rhys-vdw/lua-doc-extractor
Request: Allow adding a field type to tables and enum definitions using the
@field
annotation.This is useful for generating meta files from C code where there is no Lua table literal to copy.
See current behaviour below:
@class
✅ Using
@field
✅ Using literal code
✅ Using type annotation
@enum
❌ Using field
Error: The field must be defined after the class.Lua Diagnostics.(doc-field-no-class)
✅ Using literal code
✅ Using type annotation
Untyped table
❌ Using field
Error: The field must be defined after the class.Lua Diagnostics.(doc-field-no-class)
✅ Using literal code
✅ Using type annotation