[haXe] Always surrounding a method call by other method calls

Justin Donaldson jjdonald at indiana.edu
Wed Jun 17 02:12:28 CEST 2009


I'll give this a shot...

"callback" creates a wrapper function around another function, automatically
inserting arguments into the original function that will persist every time
the wrapper function is called.
It can be used like :

public function foo(x:Int, y:Int){ return x+y;}
public static function main(){
    var foo_cb = callback(foo,4); // creates a copy of foo() that
automatically makes the first argument a 4
    trace(foo_cb(5)); // traces 9
}

The question that keeps coming up is... why would anyone want to do this?
It seems similar to what you can accomplish through some of the methods in
Reflect.callMethod() or Reflect.makeVarArgs().
Callback also seems more limited than any of the Reflect methods. You can
only insert arguments in order (i.e. you must specify the first argument
before you can specify the second argument, and so forth).  So, you can't
really use it to only specify the *final* argument of a method.

The big feature of using callback is that it is a fully typed call, for both
arguments and arity, so you get type-safe code (vs. Reflect).  The main
benefit of callback (for me anyways) comes inside of functional routine
arguments, such as the Lambda methods.  The Lambda method arguments
typically require  the form f: A->B, so they accept only a single argument.
With callback, you can quickly create wrapper functions that save you the
trouble of writing out one-off function blocks.  Consider the following two
snippets:

// calculate the following powers of 3

trace(Lambda.map( [10,2,36,4,5], callback(Math.pow, 3)));

vs.

trace(Lambda.map( [10,2,36,4,5], function(x) { return Math.pow(3,x);}));

So, it saves a few keystrokes, is type-safe, and isn't horribly difficult to
parse (visually).  For some reason, I really dislike inserting function
blocks into method arguments.  Callback gets rid of several of these
instances, so I use it. But other than that it's a pretty minor tool.

On a side note, the "using" keyword looks like it'll be even more useful for
this sort of thing:

using Math; using Lambda;
trace(
[10,2,36,4,5].map(3.pow)
);

A bit tricky to read at first, but once you get the hang of it, it makes the
types very, very expressive and concise.

Best,
-Justin

P.S. let me know if anyone has any other uses for callback, I'm curious











On Tue, Jun 16, 2009 at 4:54 AM, Lee McColl Sylvester <lee at designrealm.co.uk
> wrote:

> Ahh, the callback method. Nah, you got me, too. I dunno, either ;-)  Still,
> with local method sigs, who needs it? ;-)
>
> Lee
>
>
>
> Ian Liu wrote:
>
>> Lee, I'm afraid you are wrong. Take a look in this thread:
>> http://lists.motion-twin.com/pipermail/haxe/2007-May/008966.html
>>
>> I've never understood really well the purpose of callback.
>>
>> Ian L.
>>
>> On Tue, Jun 16, 2009 at 7:26 AM, Lee McColl Sylvester <
>> lee at designrealm.co.uk <mailto:lee at designrealm.co.uk>> wrote:
>>
>>    Nope, that's not a callback at all. Callbacks in haXe are
>>    statically typed method signatures. You specify the parameters the
>>    callback accepts and the return value. Thus, a method that accepts
>>    two ints and returns a string would be
>>
>>    Int -> Int -> String
>>
>>    A method that accepts a string and returns nothing is
>>
>>    String -> Void
>>
>>    A method that accepts nothing and returns nothing is
>>
>>    Void -> Void
>>
>>    You can use it like this
>>
>>
>>    var callback : Int -> Int -> Int;
>>
>>    public static function main()
>>    {
>>      callback = function( a : Int, b : Int ) { return a + b; };
>>      trace( callback( 1 + 2 ) );
>>    }
>>
>>    Lee
>>
>>
>>
>>
>>
>>
>>
>>    Thomas wrote:
>>
>>        Hi,
>>
>>        what's a callback in haxe? Is it a special keyword? I couldn't
>>        find
>>        any info about that. I saw it in the haxevideo samples code.
>>
>>
>>
>>
>>    --    haXe - an open source web programming language
>>    http://haxe.org
>>
>>
>>
>
> --
> haXe - an open source web programming language
> http://haxe.org
>



-- 
Justin Donaldson
PhD Candidate, Informatics
Indiana University
http://www.scwn.net
aim: iujjd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.motion-twin.com/pipermail/haxe/attachments/20090616/2d350651/attachment-0001.htm


More information about the Haxe mailing list