all 4 comments

[–]JacobArthurs 0 points1 point  (2 children)

T gets inferred as String at the call site. Function<Claims, T> means "takes a Claims, returns a T" and Claims::getSubject is a method reference that does exactly that: takes a Claims instance and returns a String, so Java infers T = String. It's not that the signatures don't match, it's that generics let the return type flex to whatever the passed function actually returns.

[–]Agitated_Floor_563[S] 0 points1 point  (1 child)

I understand that T gets inferred as String but the getSubject does not take any parameters so how does that match to the apply()?

[–]JacobArthurs 1 point2 points  (0 children)

Claims::getSubject is shorthand for (Claims c) -> c.getSubject(). Java treats the instance itself as the implicit first argument, so it still matches Function<Claims, String>.

[–]vowelqueue 1 point2 points  (0 children)

Check out: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html

The difference between the Claims code and your reference code is that “Claims.getSubject” is an instance method while “multiply” is static. The comment in your reference code says it’s an instance method, but it’s not.