[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: object methods as first class values
- From: John Belmonte <jvb@...>
- Date: Thu, 28 Nov 2002 11:25:08 -0600
Ignacio Castaño wrote:
ok.onClick.add( app:exit )
that's not valid, but could be written as:
ok.onClick.add( app.exit, app )
the first sintax is more natural since you already do app:exit() to call the
method. I'm wondering if 'app:exit' could be expanded to 'app.exit, app'
when used as a function argument.
I don't think this sugar would make a good addition to Lua, as it is too
specialized. For example, what if the method you'd like to have called
takes a few arguments? What you want to do can be handled sufficiently
and in a more general way by some simple functor utils.
With the functor interface defined at
http://lua-users.org/wiki/LuaFunctors, your example would become:
ok.onClick.add( Bind( app.exit, app ) )
With this, the "add" function is simplified because it only ever has to
handle one argument which is function. You may not think that's a big
deal, but lets say someone else has written the widget library and you
aren't able to alter all such functions to take (fun, arg, arg ...)
instead of just (fun).
Here is how things might look if the method required another arg:
greet.onClick.add( Bind( app.set_status_bar, app, 'hello' ) )
I've been meaning to add the functor utils to the lua-users stdlib, but
there has not been any activity there for a few months to motivate me.
If someone would find it useful let me know.
http:// if . /