-
-
Notifications
You must be signed in to change notification settings - Fork 336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Field and Method of the same name not working since 3.0 #1541
Comments
If you omit the Additionally, you can use ---@class Car
car = {}
--- Here is some documentation about the userdata.
---
---@type userdata
car.Tires = nil -- ignore the `nil`; it's just here to give us a valid lua expression
---Returns the number of Tires on the Car
---@return integer number of tires
function car.Tires() end
-- lua-language-server correctly infers that this is an integer
local num = car.Tires()
-- this is ambiguous, so you'll see type defs for both the userdata
-- and the function
local data = car.Tires
-- this gets you an explicit userdata def
---@cast data -function
print(data) |
It is showing as a union type because the field
The autocompletion shows how you can retrieve the field ( As for why setting |
It used to work that way. I put a similar issue back in 2021. #549 But with the update to 3.0, it seems to have reverted. In my mind, there should be some distinction between a field and a function, even if they have the same name and should not be shown together in the popup. Saying that a Field with the name "foo" that returns a string, and a function with the name "foo()" that returns an integer can return one of two values is not correct. The Field returns a string and the Function returns an integer. They are used differently and have different return values. Showing them together and saying that either can return one of two values is misleading. |
Have issues to get the workaround to work... ---@class Tire
---@field IsSlicks boolean
---@class Car
car = {}
--- Here is some documentation about the userdata.
---
---@type Tire
car.Tires = nil -- ignore the `nil`; it's just here to give us a valid lua expression
---Returns the number of Tires on the Car
---@return integer number of tires
function car.Tires() end The function assignment will throw a Attempts at union-ing the field makes a weird code hint. ---@class Tire
---@field IsSlicks boolean
---@class Car
---@field Tires Tire|fun():int|boolean This results in: Should be |
Field types have different precedences, with |
The use of them seems fine. |
Judging the precedence of global variables is a bit difficult, and I decided to maintain the behavior for now. |
I think you missunderstood. Using ---@field as shown above with union, the code hint seems to show the "userdata type" as part of the function return union which is not correct. Its almost like it either prioritizes fun type first, then adds the union field or it groups all the unions together at the end. Either way it looks very wrong. |
So what you want is |
Closing stale issue |
How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
Which OS are you using?
Windows
What is the issue affecting?
Type Checking, Completion
Expected Behaviour
I use an in-game Lua implementation where the C++ bindings (userdata) objects are returned on object fields and in-game data is returned by accessing methods on those fields. In some cases, the field name itself is also a method. So you have objects that look like this:
The code completion only shows suggestions for the Field (usermeta) and not the function.
Actual Behaviour
Reproduction steps
Additional Notes
No response
Log File
No response
The text was updated successfully, but these errors were encountered: