Implementing recursive lambda with MethodHandle
David.Moss at ubs.com
David.Moss at ubs.com
Tue Feb 23 01:24:23 PST 2010
Hi,
I don't think 'this' should refer to the lambda.
A lambda is a function belonging to it's enclosing scope (class), this
means that 'this' needs to refer to the enclosing scope in order to be
semantically correct. An additional syntax needs to be defined to refer
to the lambda itself.
A few options OTTOMH:
Public class C {
/*
* 1- Create a closure on the lambda variable.
*
* In this example, lambda1 is closed on at declaration
* time and remains a pointer to the function regardless
* of what the outer scope does to it's lambda1 variable.
* However, this approach potentially has consequences
* outside of the function scope, but better suited for
* the closures-dev list to discuss if such an approach
* were to be taken.
*/
#int(int) lambda1 = #int(int x) {
return x == 0 ? 1 : x * lambda1.(x - 1);
};
/*
* 2- Reference the variable.
*
* Similar to the above, but weaker, as the enclosing
* scope can change the value of 'lambda' making it
* point to a different function. Note that this may be
* desired behaviour and should be allowed, but should
* not be the only means of accessing the function.
*/
#int(int) lambda2 = #int(int x) {
return x == 0 ? 1 : x * this.lambda2.(x - 1);
};
/*
* 3- Force recursive function variables to be final.
*
* The final declaration could be implied, here it is
* explicit just for the sake of clarity.
*/
final #int(int) lambda3 = #int(int x) {
return x == 0 ? 1 : x * lambda3.(x - 1);
};
/*
* 4- Omit the variable name altogether.
*
* Create new syntax that behaves similarly to 'this'
* within function scope. The important thing about
* this approach is that it allows fully anonymous
* recursive functions, and as such is my preferred
* approach.
*/
int result1 = #int(int x) {
return x == 0 ? 1 : x * .(x - 1);
}.(10);
// Another example of the same, but using '#' as the
// reference to the function as I know a lot of people
// will be unhappy with the previous syntax (though it
// makes no difference):
int result2 = #int(int x) {
return x == 0 ? 1 : x * #.(x - 1);
}.(10);
}
Personally, I would allow for 2 and 4 (and potentially 1, if lexical
closures are a desired feature).
Kind Regards,
David.
--
EUR2 EG.1168 [1923 96757]
SST: DL-SST-DEV
"Where's the Kaboom? There was supposed to be an Earth-shattering
Kaboom!" - Marvin the Martian
"Don't worry about what anybody else is going to do. The best way to
predict the future is to invent it." - Alan Kay
"there is this amazingly powerful thing writhing around in there that
will basically do everything I could possibly ask of it if only I knew
how." - Ben Butler-Cole
Based on the present E-Mail exchange, and/or on the agreement reached
with you, respectively, UBS is entitled to contact you via insecure
E-Mail:
(a) E-Mails contain substantial risks such as lack of confidentiality,
manipulation of content and sender, misdirection, viruses etc. UBS
does not accept any liability for damages arising from use of
E-mail. Accordingly, UBS recommends to abstain from sending any
sensitive information via E-Mail, from forwarding the text received
when submitting reply E-Mails and recommends to manually capture the
E-Mail address in every instance. If you should wish to verify the
content of this message, please request a hard-copy version.
(b) In principle, UBS does not accept any (purchase) orders,
cancellation of orders or authorizations etc. via E-mail. If UBS
receives such E-Mails, UBS is not obliged to expressly decline them.
If you have received this E-Mail by mistake or do not wish to be
contacted by E-Mail in the future, you are kindly asked to inform UBS
accordingly. Any E-Mail received by mistake (including all its annexes)
needs to be destroyed and the content may not be forwarded nor disclosed
to any further persons.
c) This message is provided for informational purposes and should not be
construed as a solicitation or offer to buy or sell any securities or
related financial instruments.
UBS reserves the right to retain all messages. Messages are protected
and accessed only in legally justified cases.
More information about the lambda-dev
mailing list