Lambdas not integrated with annotations
Remi Forax
forax at univ-mlv.fr
Wed Jul 30 18:18:04 UTC 2014
On 07/30/2014 07:47 PM, Jochen Theodorou wrote:
> Am 30.07.2014 16:09, schrieb Brian Goetz:
>>> why has it not been made possible to supply lambdas as annotation
>>> parameters? Would it have required too many changes to the language
>>> spec? Not deemed worthwhile?
>>
>> One significant impediment was: Annotation parameters are restricted
>> to the set of values that can be represented in classifies as
>> constants: integers, strings, classes, etc. This would have required
>> significant changes to the JVM spec.
>
> MethodHandles can be constant pool elements too - afaik.
yes, and a lambda inside an annotation will never capture local value
thus can be represented as a constant method handle.
But a constant lambda is not enough to reference a lambda, you need 4
informations (the constant method handle, the functional interface, the
single abstrat method descriptor and a reified version of this very same
descriptor).
So the question is how to store a constant method handle and 2 method
types (the method descriptors) as an annotation value
(the functional interface is stored as the type of the annotation
parameter).
> The spec problem is more I think that you normally don't do that
> directly in Java. Actually for Java as language a MethodHandle as such
> does not really exist besides in the API and what the compiler emits.
> But they are no constants in the sense of the Java language, since
> they can't even exist without making some kind of proxy out of them to
> obey the rules of a functional interface
We do store reference to an annotation as an annotation parameter in
Java, at runtime, it creates a reflection proxy.
A lambda at runtime creates another kind of proxy so it's not very
different.
So I think that neither the representation of a lambda as constant in
the bytecode nor at runtime are an issue.
The real issue is that adding a new type of annotation value will
require to update all tools that read annotations,
all the VMs, all the bytecode transformers, all the annotation processor,
so the question was does lambdas in annotation worth the trouble to
update all annotation tools.
the lambda EG answers 'no' to that question.
Note that instead of trying to represent a JUnit test as a lambda,
it's better to create a new tool for doing unit testing that works like
Ruby Rspec,
their way to describe an unit test is in my opinion better than what we
can do with JUnit.
>
> bye Jochen
>
cheers,
Rémi
More information about the lambda-dev
mailing list