Optional.orElse does not compile with anonymous types
Asked Answered
G

3

8

I encountered a weird problem using Optionals and anonymous classes:

public class Foo {
    interface Bar {
    }

    void doesNotCompile() {
        Optional.of(new Bar() {
        }).orElse(new Bar() {
        });
    }

    void doesNotCompile2() {
        final Bar bar = new Bar() {
        };
        Optional.of(new Bar() {
        }).orElse(bar);
    }

    void compiles1() {
        final Bar bar = new Bar() {
        };
        Optional.of(bar).orElse(new Bar() {
        });
    }
}

The first two methods do not compile with the error

java: incompatible types: <anonymous test.Foo.Bar> cannot be converted to <anonymous test.Foo.Bar>

I'd expected that, since both implement the interface Bar all three approaches work. I also cannot figure out why the third option fixes the problem. Can anyone explain this please?

Gwenngwenneth answered 3/12, 2019 at 13:53 Comment(1)
The third one fixes the problem, because there the type of Optional.of is fixed to Optional<Bar>. In all the other cases, it is Optional<SubAnonymousSubclassOfBar>. I would have expected the other two to type-infer the appropriate common upper-bound of Bar as well, though. But apparently Optional<SomeSubclassOfBar>(bar).orElse(someOtherSubclassOfBar) needs some hand-holding.Insolvable
S
8

You can supplement the type on the first two using a type witness:

Optional.<Bar>of(new Bar(){}).orElse(new Bar(){});

This allows the compiler to see that you are expecting a return of Optional<Bar>, which #orElse can then inferr to accepting any Bar

Spineless answered 3/12, 2019 at 14:6 Comment(0)
E
5

You'd need to tell the Optional that you want a Bar for that interface.

Bar bar = new Bar();
Optional<Bar> o = Optional.of(new Bar() {}).orElse(bar);
Elwood answered 3/12, 2019 at 13:59 Comment(3)
I know what generics are for. I just thought javac will infer the types as @Insolvable said.Gwenngwenneth
Yeah, coming from Scala, having to spell this out is kind of lame. On the other hand, javac compiles so much faster, so ...Insolvable
@Gwenngwenneth it infers 'anon type extending Bar'. If that's not what you want, you have to state your intent.Elwood
P
5

Case compiles1

Your Optional has the generic type Bar, because the variable bar has type Bar.

The anonymous class of type Foo$1 you create has Bar as a super type, thus the method compiles.

Case doesNotCompile

Here, Optional has the generic type Foo$1 and you are trying to pass an object of type Foo$2 into orElse which does not have Foo$1 as a super type. Hence the compile error.

Case doesNotCompile2

Similar to doesNotCompile, Optional has the generic type Foo$1 and you are trying to pass bar, a variable of type Bar into orElse which again does not have Foo$1 as a super type.


Avoiding these errors

Add a type witness to your call of Optional::of. This gives your Optional the generic type Bar:

public class Foo {

    interface Bar {
    }

    void doesNotCompile() {
        Optional.<Bar>of(new Bar() {
        }).orElse(new Bar() {
        });
    }

    void doesNotCompile2() {
        final Bar bar = new Bar() {
        };
        Optional.<Bar>of(new Bar() {
        }).orElse(bar);
    }

    void compiles1() {
        final Bar bar = new Bar() {
        };
        Optional.of(bar).orElse(new Bar() {
        });
    }
}
Parkland answered 3/12, 2019 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.