-
-
Notifications
You must be signed in to change notification settings - Fork 320
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
@operator add customization for first operand? #2777
Comments
From lua-users wiki, these kinds of mathematic operators will check metatables on both side (the left value first, then the right).
With a bit of digging in LuaLS codebase, I think this functionality can be added here: lua-language-server/script/vm/operator.lua Lines 302 to 306 in 26a7b69
local node = vm.runOperator(binaryMap[op], source[1], source[2])
if not node and op == '+' then
-- interchangeable for '+'
node = vm.runOperator(binaryMap[op], source[2], source[1])
end
if node then
vm.setNode(source, node)
return
end I tested this and it works 👀 ---@class Vector3
---@field x number
---@field y number
---@field z number
---@operator add(Vector3): Vector3 # Vector3 + Vector3
---@operator add(number): Vector3 # Vector3 + number | number + Vector3
Vector3 = {}
local a = 1 + Vector3
local b = 1.1 + Vector3
local c = (1*2+3) + Vector3
local x ---@type number
local d = x + Vector3
-- a, b, c, d are all inferred as `Vector3` Note that the above demo code I only check for |
This is an interesting find and I find it a good enough workaround for now while #1371 is still being worked on for the asymmetric operations. Added that with that preliminary code snippet would make the LSP in line with the actual behavior of lua code. |
whoops misclick i apologize |
I have never thought of an asymmetric |
Good day, yeah I couldn't think of a practical one either but that might be an edge case for the future. Technically you could do that with a C binding where it returns a different type when it detects if it was the left or right operand. But that might be too weird, after all I'm just trying to have parity with a library I'm interfacing which has support for scalars on the left operand and so far I can't find a case which breaks symmetry. |
feat: flip binary operator check if failed (#2777)
How are you using the lua-language-server?
NeoVim
Which OS are you using?
Linux
What is the issue affecting?
Annotations
Expected Behaviour
I expected it would work like this
Actual Behaviour
But instead it is like this
Reproduction steps
copy paste 'Expected Behaviour code'
displays " ')' expected "
Additional Notes
I am asking if the impossible case is actually possible and that it is only undocumented or something. I expected the binary operators to take two parameters instead of just one. Thank you.
Log File
No response
The text was updated successfully, but these errors were encountered: