lua-users home
lua-l archive

Re: More on environments and objects

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 26-Aug-05, at 9:35 AM, Chris Marrin wrote:
It would not be hard to add something to CallInfo to indicate that this method was called with the SELF instruction. That would no longer allow this:
 instance.g(instance, instance2)
in place of this:
 instance:g(instance2)
but it would still be compatible because I'm making the ':' syntax a requirement in my object system. The rest of Lua works as it always has.
Anyway, does this seem like a reasonable approach?
The problem with this is that sometimes you need to call an instance method supplying the actual instance. This shows up at odd moments; the simplest example I can think of is the following: table.foreach calls a function for every k,v in a table. It could be written:
function table.foreach(tab, func)
 for k, v in pairs(tab) do
 local rv = func(k, v)
 if rv then return rv end
 end
end
Now, suppose I actually want to invoke an object method, instead of calling a function? I could do that with an explicit wrapper:
 table.foreach(tab, function(k, v) return obj:method(k, v) end)
but that would get tedious, so I would want to refactor it:
function table.foreachmeth(obj, tab, method)
 for k, v in pairs(tab) do
 local rv = obj[method](obj, k, v)
 if rv then return rv end
 end
end
table.foreachmeth(obj, t, "send")
I could even introduce that into the object:
obj.foreach = foreachmeth
obj:foreach(tab, "send")
How would I write this if the method refused to execute unless self-called? The best I can come up with is filling the method name into a template and then compiling the template, which seems yukky.

AltStyle によって変換されたページ (->オリジナル) /