Boaz Nahum
Boaz Nahum

Reputation: 1097

A functional interface that extends other can't - can't assign constructor reference to it - Is it compiler bug?

In the simple code below i can see to 'Factory' interfaces, the only different between them is that Factory0Bad extends Supplier:

@FunctionalInterface
public static interface Factory0Bad<T> extends Supplier<T> {
    T get();
}

@FunctionalInterface
public static interface Factory0Good<T>  {
    T get();
}

public static class Handler {}
public static class HandlerA extends Handler {}

public static void main(String[] args) {
    // this line compiles
    Factory0Good<? extends Handler> good = HandlerA::new;

    // this does not!
    Factory0Bad<? extends Handler> bad = HandlerA::new;
}

So the line

Factory0Good<? extends Handler> good = HandlerA::new;

is accepted by the compiler (1.8.0u05), but this line

Factory0Bad<? extends Handler> bad = HandlerA::new;

is not.

Why is this? What am I missing here? Is it a bug in javac?

Upvotes: 2

Views: 117

Answers (1)

Boaz Nahum
Boaz Nahum

Reputation: 1097

 // this does not!
        // 1.8.0_05 - fails
        // 1.8.0_40 - succeeds
        // 1.8.0_45 - succeeds
        Factory0Bad<? extends Handler> bad = HandlerA::new;

Upvotes: 1

Related Questions