-
- Notifications
You must be signed in to change notification settings - Fork 386
Open
Description
If an object's metatable implements the proper metamethods, LuaLS should be able to infer the @operator implementation.
Note that I may just be doing it wrong, as I'm using a class with a metatable of itself:
---@class Rule ---@field def fun(source: string, index: number): number, ... T.Rule = {__metatable = false} -- Prevent infinite recursion by not taking the shortcut of __index: table setmetatable(T.Rule, { __call = function(t, source, index) return t:parse(source, index) end, __index = function(t, k) if t ~= T.Rule then return T.Rule[k] end end, __newindex = function() error("cannot create fields on a value of type Rule") end, __metatable = false }) ---@param def fun(source: string, index: number): number, ... ---@return Rule ---@nodiscard function T.Rule.new(def) local o = {def = def} setmetatable(o, T.Rule) return o end ---@param other Rule ---@return Rule ---@nodiscard function T.Rule:__add(other) return T.Rule.new(function(source, index) local succ, i = pcall(self.def, source, index) if not succ then error(i, 2) end return other.def(source, i) end) endluiz00martins and Rathoz
Metadata
Metadata
Assignees
Labels
No labels