SAM = Single Abstract Method. It's just another way of saying FunctionalInterface (technically the latter is more correct too because an abstract class with a single abstract method doesnt qualify for a SAM type). So in Java instead of having T -> ()
we have Consumer<T>
. But nothing prevents me from writing an equivalent type:
interface MyDifferentConsumer<T> {
void consume(T t);
}
This also represents T-> ()
, it just has a different name. I think the best place we can see how this affects inference is with method references. In java, both of these are perfectly valid:
Consumer<String> c1 = System.out::println;
MyDifferentConsumer c2 = System.out::println;