Implementing recursive lambda with MethodHandle

Peter Levart peter.levart at gmail.com
Mon Feb 22 12:08:15 PST 2010


On Monday 22 February 2010 12:52:15 Rémi Forax wrote:
> 2) this refers to the current lambda
>      In that case you can use this.() to do the recursive call.
>      #int(int) a= #int(int x) {
>          return (x==0)?0:x + this.(x-1);
>       };
> 
> 3) allow a reference to a lambda to be used inside a lambda
>       #int(int) a= #int(int x) {
>          return (x==0)?0:x + a.(x-1);
>       };
> 
> How to implement solution 2 or 3 with method handles ?
> First, it's not simple because a method handle is an
> object that contains a function pointer to a method,
> so there is no dedicated class.
> 
> You have to first create the method handle and then inject itself to be 
> available
> in the body of the function referenced.
> Because injecting the method handle creates a new method handle
> 

If you're just invoking the function (either this.(...) or a.(...)) the compiler could translate 
that to simple invocation of a method (which is the target of the method handle). If you're 
passing the value to someone, then that's a different story...

Peter


More information about the lambda-dev mailing list