-
-
Notifications
You must be signed in to change notification settings - Fork 331
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
Modifying local reference of a table does not update the table in other files #3025
Comments
Worth mentioning similar issue happens with require("Ready").test = {a = "abc"} |
IIUC, luals doesn't run your actual code, it just tries its best to infer the types. Consider the following:
MyTable = {a = 1}
MyTable.a = nil
MyTable = {b = 2}
MyTable.b = nil
print(MyTable) --> what type of `MyTable` do you expect here?
-- only with field `a`?
-- only with field `b`?
-- no field at all? no, the result is
because the server just traces all By aliasing the global variable with a Proper annotationIf you want luals to know that you are injecting fields into the same type, I think you should add proper
---@class MyTable
MyTable = {zx = 1}
require "Ready"
---@class MyTable
local MyTable = MyTable
MyTable.test = {a = "abc"}
require "Set"
local MyTable = MyTable
-- now `MyTable` has the class type `MyTable`, has both fields `zx` and `a`
MyTable.b = 2 --> warning: Fields cannot be injected into the reference of `MyTable` for `b`. To do so, use `---@class` for `MyTable`.
-- i.e. unless you add the `---@class` like file 2, otherwise luals forbids you to inject field into an already typed variable
-- otherwise any operation you did here will not reflect to the type system PS: I just recently learnt that this is the C# partial class concept, where you can split the definition of a class in multiple files. Our team has been widely adopting this coding pattern before I even know there is a name for this concept 😂 |
Thank you for reply, it's so nice to have this working now. |
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?
Other
Expected Behaviour
See the changes to table in all files.
Actual Behaviour
The changes do not appear in other files when modifying the local reference.
Reproduction steps
File 1:
File 2:
File 3:
However if file 2 does not use a local reference then it works as expected.
Additional Notes
No response
Log File
No response
The text was updated successfully, but these errors were encountered: