#1869 Method callback syntax

Jens Wed 4 Apr 2012

Hi,

Is this the shortest way to write a callback?

onEvent.add { this.handleEvent( it ) }

The disadvantage is that it only works for one arg methods.

If there are more args you seem to have to do this:

onTwoArgEvent.add( |a1, a2| { this.handleTwoArgEvent( a1, a2 ) } )

Which is a little noisy. But I would expect there to be a way to write something like this:

onTwoArgEvent.add( this.handleTwoArgEvent )

Also, this doesn't seem to be compiling:

onEvent.add( { this.handleEvent( it ) } )

(Notice the added parentheses  around the argument to .add)

Why is that?

BR, Jens

andy Wed 4 Apr 2012

Hey Jens. If the last argument to your method is a closure, it can be defined outside the parens:

onEvent.add |a1,a2| { this.handleTwoArgEvent(a1, a2) }

Akcelisto Thu 5 Apr 2012

Why not add more argument placeholders?

it  - 0th arg
it1 - 1th arg
it2 - 2th arg
...
it9 - 9th arg

This give us:

onEvent.add { this.handleTwoArgEvent(it, it1) }
onEvent.add { this.handleThreeArgEvent(it, it1, it2) }

dobesv Thu 5 Apr 2012

I think you can do this to create a "bound method":

onTwoArgEvent.add( #handleTwoArgEvent.func.bind(this) )

I don't think there's any shorter syntax for it, since just saying this.handleTwoArgEvent is ambiguous about whether it's a call with no args or a method binding.

Perhaps an alternate syntax could be invented for this but I'm not sure it's common enough usage to justify changing the language. Adding a proxy in Method to Func.bind might be friendlier ( #handleTwoArgEvent.func.bind(this) ), or a method on Obj to return a bound method ( this.bound(#handleTwoArgEvent) ).

Jens Thu 5 Apr 2012

dobesv wrote:

I think you can do this to create a "bound method":

That works fine (if you replace this by [this]). It's still a bit wordy, but you don't have to repeat the arguments.

dobesv wrote:

I'm not sure it's common enough usage to justify changing the language

It's pretty common to have method callbacks. But the current alternatives aren't so bad anyway.

Akcelisto wrote:

Why not add more argument placeholders?

If you do this you'll probably want to name them something like this:

it  - for one arg funcs only
it1 - 1th arg for multi arg funcs only
it2 - 2th arg for multi arg funcs only
...
it9 - 9th arg for multi arg funcs only

Could be useful.

Jens Thu 5 Apr 2012

dobesv wrote:

Perhaps an alternate syntax could be invented for this but I'm not sure it's common enough usage to justify changing the language.

Oh, and most importantly, it would make declarative GUI-building with fwt more beautiful! There method callbacks are very common, and reading one of the above alternatives is a little disturbing.

ikhwanhayat Thu 5 Apr 2012

Perhaps an alternate syntax could be invented for this but I'm not sure it's common enough usage to justify changing the language. Adding a proxy in Method to Func.bind might be friendlier ( #handleTwoArgEvent.func.bind(this) ), or a method on Obj to return a bound method ( this.bound(#handleTwoArgEvent) ).

Scourging old threads, I found out that there was some kind of syntax to do this http://fantom.org/sidewalk/topic/697.

I wonder what people's thought about delegates? Like what they have in C#.

Login or Signup to reply.