Static methods of generic classes are treated differently

Ethan McCue ethan at mccue.dev
Fri Jun 9 16:58:43 UTC 2023


Following up on this just to see if there is an explanation of some sort I
am missing.

On Sun, Apr 30, 2023 at 8:40 AM Ethan McCue <ethan at mccue.dev> wrote:

> I've run into either a corner case in type resolution or a compiler bug
> and I'm not sure which.
>
> In an expression context, if a static method is referenced from a generic
> class then it won't properly target overloads that require primitive
> wrapper unboxing.
>
> package org.example;
>
> import java.time.Month;
> import java.util.function.Supplier;
>
>
> interface A {
>     static int f() {
>         return 1;
>     }
> }
>
> interface B<T> {
>     static int f() {
>         return 2;
>     }
> }
>
> final class C {
>     static int f() {
>         return 3;
>     }
> }
>
> final class D<T> {
>     static int f() {
>         return 4;
>     }
> }
>
>
> public class Main {
>     static void h(Month b) {}
>     static void h(int b) {}
>
>     static <T> T g(Supplier<? extends T> d) {
>         return d.get();
>     }
>
>
>     public static void main(String[] args) {
>         // Compiles
>         h(
>                 g(A::f)
>         );
>
>         // Doesn't compile
>         h(
>                 g(B::f)
>         );
>
>         // Compiles
>         var r1 = g(B::f);
>         h(r1);
>
>         // Compiles
>         h(
>                 g(C::f)
>         );
>
>         // Doesn't compile
>         h(
>                 g(D::f)
>         );
>
>         // Compiles
>         var r2 = g(D::f);
>         h(r2);
>     }
> }
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20230609/e5c1a42a/attachment.htm>


More information about the compiler-dev mailing list