Type Annotations and Lambda
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Wed Feb 6 01:54:17 PST 2013
On 05/02/13 23:54, Werner Dietl wrote:
> Hi Maurizio,
>
>> I am waiting on a specification clarification from Mike and/or Alex
>> about METHOD_REFERENCE_RECEIVER and CONSTRUCTOR_REFERENCE_RECEIVER.
>> To me these don't look like type uses, but instead only like scoping
>> mechanisms, for which we don't support type annotations.
>> It would be great to see a few examples of type uses for these and
>> what the interpretation of type annotations should be.
>>
>> There are several kinds of method references - the so-called unbound kind
>> has a type as a qualifier - i.e.
>>
>> Person::getName
> In which context can I use the unbounded kind? When I try
>
> static Runnable returnLambda1() {
> return Test::getName;
> }
>
> I get:
>
> Test.java:25: error: incompatible types: invalid method reference;
> non-static method getName() cannot be referenced from a static context
> return Test::getName;
> ^
> 1 error
The target type must be a SAM whose descriptor accepts an extra argument
with the receiver type i.e.
interface Bar {
void m(Person p)
}
Here's where the annotation might come in handy - i.e. you could define
pair of annotations, and allow an unbound method reference only if the
target has a matching annotation on the receiver parameter.
>
>
>> Constructor references also have a type as a qualifier:
>>
>> ArrayList::new
> Why don't I get a warning about raw types in this example:
>
> static Runnable returnLambda1() {
> return ArrayList::new;
> }
>
> Even when I compile with -Xlint:all I don't get a warning about the raw type.
> This is the reason why I assumed that this is not really a type use,
> but only the class name to select the constructor from that class -
> not from a particular instantiation of the class.
> Is the "raw type" warning missing or am I misunderstanding?
This is as specified. No raw types involved here. Inference is doing its
job and inferring ArrayList<Object>.
>
>
>> Finally, the spec says that type annotations in the signature or body
>> of a lambda expression should appear in the method that results from
>> translation.
>> I was hoping that this would work without any special effort. However,
>> no type annotations appear in a translated method.
>> Could somebody point me to the location in the code that translates a
>> lambda expression into a method?
>>
>> This is the place:
>>
>> http://hg.openjdk.java.net/jdk8/jdk8/langtools/file/tip/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java
> Thanks! I'll take a look and will get back with any questions.
>
> cu, WMD.
>
More information about the type-annotations-dev
mailing list