lua-users home
lua-l archive

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


> > So, is there some support for Mark Hamburg's closure sugar in Rio ;) ?
> 
> Yes. Here we would write  C(set, "values") for C:values  and define C as
> 
>   C = function (obj, met)
>         met = obj[met]
>         return function (...) return met(obj, ...) end
>       end

A proposal for Lua 5.2:

    Make ':' an operator which creates a function with a bound first
    parameter.  Like your definition of C above.
    That way, the colon operator is no longer syntactic sugar, it's a
    real operator which makes stuff like this possible:

	a=foo:bar
	a()

    Of course, if every method call would create such an object, perfor-
    mance would suffer badly.

    But: a simple direct method call (the only valid usage of the colon
    operator at the moment) can easily be detected in the code generator
    (or even in the parser) and the creation of the object can be avoided
    (BIND,CALL -> SELF,CALL).  That way, simple method calls stay as fast
    as they are now and won't create garbage for each call.

    Open details:

	a) Wrap the binding in a CFunction or make it a new low-level
           type (i.e. BClosure along CClosure and LClosure).

	   Wrapping it in a CFunction costs one redundant pointer per
           binding and suffers one call indirection.
           But implementation is easy.

	   A new low-level type requires more changes but needs less
	   space and requires no indirection when called.

	b) Early or late binding?  Iow: fetch the method when binding or
	   when calling.  Iow: is the tuple <function,object> or <object,
	   methodname>.

	   I would prefer early binding - faster (fetch method only once)
	   and ... hmm ... late binding is so unpredictable ;-)

Ciao, ET.