Reputation: 21978
I have a method look like this.
static <R> R applyOthers(Some some, Function<List<Other>, R> function) {
List<Other> others = ...;
return function.appply(others);
}
Now when I try this,
withSome(some -> {
List<Other> others1 = applyOthers(some, v -> v); // works
List<Other> others2 = applyOthers(some, Function::identity); // doesn't
});
An error, I get.
incompatible types: unexpected static method <T>identity() found in unbound lookup
where T is a type variable:
T extends Object declared in method <T>identity()
Why ::identity
doesn't work?
Upvotes: 1
Views: 849
Reputation: 109
shouldn't we say that Function::identity is supplier of Function like -
Supplier<Function<?,?>> a = Function::identity;
and that's why its not working ? If we replace '?' with correct type a.get() works.
Upvotes: 1
Reputation: 1764
Function<Object, Object> f1 = v1 -> v1;
Supplier<Object> s1 = Function::identity;
Supplier<Object> s2 = () -> Function.identity();
See this piece of code, i guess you just misused the method reference here. When we say SomeObject::method
, this method reference should match with some functional interface. In the above example, Function::identity
is kind of supplier instance not java.util.function.Function
instance.
Upvotes: 2