lua-users home
lua-l archive

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

Why hardcode it when you can emulate it?

function makeobjforclosure(c)
    local t = {
        __index = function(t, k)
            return function(...)
                return c(k, ...)
    setmetatable(t, t)
    return t

Untested, but should work.

On Tue, 21 Sep 2004 22:33:58 -0700, Mark Hamburg <> wrote:
> PiL finishes the chapter on object-oriented programming with the rather
> Scheme-like approach of implementing objects as function closures. Would it
> make sense to define obj:msg( ... ) as meaning obj( "msg", ... ) if obj were
> a function? This has the downside of meaning that the syntax is no longer
> simply sugar, but it had already ceased to be treated as such in the
> implementation.
> It might actually be nicer to have it mean obj( obj, "msg", ... ) but that
> may confuse parameter counts in the implementation since the case where obj
> isn't a function doesn't pass the message to the method.
> Mark