Giter VIP home page Giter VIP logo

Comments (4)

tomlau10 avatar tomlau10 commented on August 14, 2024

I would expect that inject-field error only show up if exact is provided the class annotation

Nope. This is the normal behavior.
Basically You can declare class fields in 2 ways:

  • directly use ---@field
  • add fields (or methods) to the variable annotated by ---@class
  • or use combination of the above

  • In your example local Foo is annotated by ---@class, so you can add fields/methods to this variable and LuaLS will merges added fields into the class Foo:
---@class Foo
---@field a string  -- method1: you declared a string field `a` by `---@field`
local Foo = {}

---@return Foo
function Foo.new()  -- method2: you are adding a `new()` function field to the class `Foo`
    return {a = 'abc'} -- Pretend this make a proper new object or smt
end
  • On the other hand, ---@class (exact) means that you can only use method 1 (---@field) to declare fields/function/methods of a class. That is if you write ---@class (exact) Foo, your function Foo.new will already throw this inject-field warning.

A few ways to tackle this warning:

  • declare the bar field in ---@class Foo
  • Add ---@class for foo (as stated by the warning)
local function doSomething()
    ---@class Foo
    local foo = Foo.new()
    ...
end
  • disable the inject-field warning

The last one is probably not a good idea, because in this case, LuaLS actually DOES NOT KNOW that Foo class has the field bar, and you can't do those goto defintion thing for this field.

from lua-language-server.

Rathoz avatar Rathoz commented on August 14, 2024

Then the docs probably needs to be updated as they state

Marking the class as (exact) means fields cannot be injected after the definition.

With the example used as

---@class (exact) Point
---@field x number
---@field y number
local Point = {}
Point.x = 1 -- OK
Point.y = 2 -- OK
Point.z = 3 -- Warning

from lua-language-server.

tomlau10 avatar tomlau10 commented on August 14, 2024

I think the example that you quoted from wiki is perfectly fine?
It strictly follows the rule "fields cannot be injected after the definition"

  • there exist x and y fields by using the ---@field annotation
  • therefore the following Point.x and Point.y => OK
  • but the last one Point.z triggers the inject-field warning

In my own opinion, there is indeed a difference between a class and an instance.

  • when you annotate the variable using ---@class
    => this is a class variable and newly injected fields should be merged to class definition
  • while in your first example local foo = Foo.new()
    => this just returns an instance of the Foo class, and generally no extra fields should be allowed to inject into an instance of Foo class, which will make this variable different from the definition of exiting Foo class. Unless you explicitly specify that the new variable acts as a class variable. 🤔

from lua-language-server.

Rathoz avatar Rathoz commented on August 14, 2024

Alright, fair.

I still think the documentation should be clarified

from lua-language-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.