RFR: 8322810: Lambda expressions can implement classes [v4]
Maurizio Cimadamore
mcimadamore at openjdk.org
Thu Apr 10 13:53:35 UTC 2025
On Thu, 10 Apr 2025 03:00:24 GMT, Vicente Romero <vromero at openjdk.org> wrote:
>> Currently javac is accepting code like:
>>
>>
>> class Test {
>> void m() {
>> var r = (Test & Runnable) () -> System.out.println("Hello, World!");
>> }
>> }
>>
>>
>> according to: `4.9 Intersection Types` a notional interface can't be induced here as one of the components is a class different from `j.l.Object`
>>
>> With this fix this code pattern will be rejected with a compiler error
>>
>> TIA
>
> Vicente Romero has updated the pull request incrementally with one additional commit since the last revision:
>
> allow annotations as functional interfaces
Re annotations, I'm not sure.
The javadoc for `@FunctionalInterface` says:
* <p>If a type is annotated with this annotation type, compilers are
* required to generate an error message unless:
*
* <ul>
* <li> The type is an interface type and not an annotation type, enum, or class.
* <li> The annotated type satisfies the requirements of a functional interface.
* </ul>
```
So, annotations seem to be excluded there. I can't find something in the JLS though -- but it strikes me as an oversight. We fixed this here
https://bugs.openjdk.org/browse/JDK-8005299
After it was reported that one could create new annotation instances using lambda expressions. While this might be ok, I don't recall we made a deliberate decision to support this, which is why we disabled support for annotations. It seems that the JLS never got the memo.
I'd suggest to fix the issue in front of us, and maybe start a separate discussion on what is the fate of annotations and functional interfaces (to be fixed in another PR).
-------------
PR Comment: https://git.openjdk.org/jdk/pull/24548#issuecomment-2793252187
More information about the compiler-dev
mailing list