2017-12-16 26 views
0

나는 이런 식으로 보입니다.메소드 참조가이 기능과 작동하지 않는 이유는 무엇입니까?

static <R> R applyOthers(Some some, Function<List<Other>, R> function) { 
    List<Other> others = ...; 
    return function.appply(others); 
} 

지금 내가이 때,

withSome(some -> { 
    List<Other> others1 = applyOthers(some, v -> v); // works 
    List<Other> others2 = applyOthers(some, Function::identity); // doesn't 
}); 

오류, 내가 얻을.

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() 

::identity이 작동하지 않습니까?

답변

2
Function<Object, Object> f1 = v1 -> v1; 
Supplier<Object> s1 = Function::identity; 
Supplier<Object> s2 =() -> Function.identity(); 

이 코드 조각을 참조하십시오. 방금 여기에서 메소드 참조를 잘못 사용한 것 같습니다. SomeObject::method이라고 말하면이 메소드 참조는 일부 기능 인터페이스와 일치해야합니다. 위의 예에서 Function::identity은 공급자 인스턴스가 아니며 java.util.function.Function 인스턴스입니다.

+0

그게 다예요! 'Function :: identity'가 아닌'Function.identity()'이어야합니다. 감사. –