[haXe] blocks and yield?

blackdog blackdog at ipowerhouse.com
Sat Apr 7 17:09:37 CEST 2007


On Sat, 07 Apr 2007 16:59:18 +0200
"Janko M." <janko.m at itmmetelko.com> wrote:

> blackdog wrote:
> 
> >On Sat, 07 Apr 2007 10:10:25 +0200
> >Nicolas Cannasse <ncannasse at motion-twin.com> wrote:
> >
> >  
> >
> >>>public method(int x,Closure c)  {
> >>>// snip
> >>> 	c.call(myparam);
> >>>// snip
> >>>}
> >>>
> >>>called with
> >>>
> >>>obj.method(1) { p ->
> >>>	println p
> >>>}
> >>>
> >>>OR
> >>>
> >>>obj.method(1,{p-> println p})
> >>>      
> >>>
> >>This is only a "local" yield, so less powerful, since you can't
> >>escape the scope of the Closure.
> >>
> >>It can be done currently in haXe :
> >>
> >>public function method( x, f ) {
> >>// snip
> >>    f(456);
> >>// snip
> >>}
> >>
> >>obj.method(1,function(p) { trace(p); });
> >>
> 
> >>Anyone else got any comments on this ?
> >>    
> >>
> I must confess I couldn't uderstand your code p -> println p in all 
> emails I got througth mailing list until now and I tried some.
> basically now I see
> 
> {p-> trace(p) } == function(p) { trace(p); }
> 
> ok, now that I know it's ok, but I maybe like it more in the more
> obvious way as it is now...
> 
>  because function(p) {} is familiar (to me) with everything else in
> the haxe language and even if I don't know what it does I can look at
> it and come to a conclusion pretty quickly. While p -> trace(p) is
> more alien and I se no relation to other constructs in the lang and
> it confused me.
> 
> ----
> 
> I don't know closures yet,... I have just heard of them... so I don't
> know if yield is related to them... or do you mean yield like python
> generators have it?
> 
> Janko
> 
> 
> 


hi Janko

{p-> trace(p) } == function(p) { trace(p); }

I used this syntax because groovy has it, but maybe its more obvious to
more people if the ruby syntax is used

{ |p| trace(p) } == function(p) { trace(p); }

i used the groovy syntax becuase in  groovy they decided against || due
to conflict with the or operator - and I reasoned that another C like
languge would have the same issue, hence ->. actually for haxe, i think
-> is a good choice cos  you only see -> in function definitions, and
this is a functional construct.

below y is a closure, it has reference to it's enclosing scope stored
in self. therefore y can be passed to other contexts but still execute
relative to it's originating object.

function x() {

	var y = function() {
		var self = this
	}
	
	return y
}

i'm not sure if it's the same as python generators or not exactly.

bd



More information about the Haxe mailing list