Lambda Conversion Considered Harmful?
Peter Levart
peter.levart at gmail.com
Fri Feb 19 15:32:13 PST 2010
On Friday 19 February 2010 20:52:17 Joshua Bloch wrote:
> As per my previous e-mail, I'm worried about the semantics of lambda
> conversions (both project lambda's and BGGA's) as well as the performance.
> I consider the lack of an accurate "this" to be a serious issue, as well
> as the inability to work on class types and access class members. Also
> it's confusing to have a widening reference conversion from one interface
> type to another when the latter ("wider") interface doesn't include a
> method present on the former (invoke).
>
> Josh
>
There is a way to support "conversion" of lambda expression to SAM class (and/or interface) and
at the same time have "this" refer to lambda instance of function (not SAM) type. This precludes
interfaces as the implementation mechanism of function types - the same object can both extend
SAM class and implement function type. If you want to access members of SAM type from within
lambda body, you simply cast "this" to SAM type.
There might be another "hacky" variant which uses special objects as function typed objects
(MethodHandle) and goes like this:
- lambda conversion is performed at compile time (like BGGA) and includes possible conversion to
SAM classes. Each such generated SAM sub-class contains a reference to a function object
(MethoHandle) constructed at SAM instance construction time and which delegates to SAM's method
(not the other way around). "this" inside lambda refers to the function object. "this" can be
cast only to SAM type (or supertype) to which lambda expression was converted and such cast
returns the SAM instance (not the function object). So we only have 2 instances at any time.
I call it "hacky" because the following expression inside lambda body would be false:
this == (SamType)this;
But the following would be true:
(SamType)this == (SamType)this;
Regards, Peter
More information about the lambda-dev
mailing list