local a = {}
function a:test1(value)
print(value)
end
local b = {}
function b:test2(v1, v2)
v2(100);
end
b:test2(_, a.test1)
不起作用 值是零。我可以找到在匿名函数中进行封装的解决方案
b:test2(variable, function(value) a:test1(value) end)
但是我觉得这很糟糕
正确的语法是什么?
anotherObject:aFunction(variable, object.doStuff)
是正确的语法。
:
在函数中使用冒号只是将隐式self
参数作为第一个参数的调用或声明的语法糖。如果您想以更简洁的方式遵循示例中显示的模式,则可以使用辅助函数。
local function bind(t, k)
return function(...) return t[k](t, ...) end
end
然后,您可以像这样应用它。
anotherObject:aFunction(variable, bind(object, 'doStuff'))
编辑:我认为要解决您的问题,就需要在一定程度上进行绑定,而无需修改Lua解释器或使用代码翻译步骤。从根本上讲,这是因为Lua中的函数不携带有关其来源的任何信息。即,表并不固有地拥有它们存储的功能。
例如,以下是完全合法的Lua代码。
function Circle:area() -- function Circle.area(self)
-- ...
end
-- Evaluate the function in the "area" slot with Square as the self parameter.
Circle.area(Square)
当然,您可以尝试进行范式转换,但是如果您基于函数绑定到已被索引的表的想法构建整个应用程序,那可能为时已晚,如上所述。因此,我提出以下疯狂的解决方案。
local mt = {}
function mt:__index(k)
local v = self._slots[k]
if v == nil then
-- Ascend the inheritance tree.
-- This has to be done with rawget all the way up,
-- otherwise inherited functions would be repeatedly bound.
local p = self
repeat
p = rawget(p, '_parent')
if not p then break end
v = p._slots[k]
until v
end
if type(v) == 'function' then
-- Return a self-bound version of the function.
return function(...) return v(self, ...) end
end
return v
end
function mt:__newindex(k, v)
self._slots[k] = v
end
--- Demo & Tests ---
local function Object(parent)
local o = setmetatable({_slots = {}}, mt)
if parent then rawset(o, '_parent', parent) end
return o
end
local o1 = Object()
local o2 = Object(o1)
assert(o1.abc == nil, 'o1.abc should be nil')
o1.abc = 3
assert(o1.abc == 3, 'o1.abc should be 3')
assert(o2.abc == 3, 'o2.abc should be 3, inherited from o1')
o2.abc = 7
assert(o2.abc == 7, 'o2.abc should be 7, overriding o1')
assert(o1.abc == 3, 'o1.abc should be 3, unaffected by o2 setter')
function o1:test(bar)
return self.abc + bar
end
assert(type(o1.test) == 'function', 'o1.test should be a function')
assert(type(o2.test) == 'function', 'o2.test should be a function, inherited from o1')
assert(o1.test(5) == 8, 'o1.test(5) should return 3 + 5 = 8')
assert(o2.test(11) == 18, 'o2.test(11) should return 7 + 11 = 18')
function o2:test2(fn)
return self.abc + fn(7)
end
assert(o2.test2(o1.test) == 17, 'o2.test2(o1.test) should return 7 + (3 + 7) = 17')
o2.test3 = o1._slots.test -- proper function copying
assert(o2.test3(11) == 18, 'o2.test3(5) should return 7 + 11 = 18')
o2.abc = nil
assert(o2.abc == 3, 'o2.abc should be 3 again, inherited from o1 after clearing')
o2.abc = false
assert(o2.abc == false, 'o2.abc should be false, __index needs to differentiate between nil and false')
该元表将为您提供所需的内容,并带有继承的和绑定的功能来引导。您只需要确保要遵循此模式的所有表都遵循示例代码中显示的对象创建方法即可。
为了说明,以这种方式创建的每个表都将任何新的分配重定向到_slots
子表中,并且任何新的检索都要检查_parent
继承树。如果该值的类型为function
,则它将返回一个新的闭包,该闭包具有self
开始检查的原始绑定到找到的函数。
显然,使用:
冒号语法从这些对象之一调用函数将是一个愚蠢的主意,因为它的计算结果为o.fn(o, o)
,而这可能并不是您想要的。另一个警告是,将功能从这些对象复制到这些对象将无法按预期进行。o1.newfn = o2.fn
将放入一个o2
绑定函数o1
,然后将其重新绑定到o1
。最终结果将是类似o2.fn(o2, o1)
。您将不得不从_slots
表中复制函数。
结束语:即使这样做行得通,但从长远来看,我个人不建议这样做,因为这可能会使习惯于Lua如何使用表,索引和函数的任何人感到困惑,并且会产生开销。您可能能够通过废除一些它memoizing封盖,但我会离开这个决定取决于你。祝好运!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句