I'm having a hard time sorting out why lambda expressions are assignable to some functional interfaces, but not others. An example, using some functional interfaces from the Metrics library:
Gauge<Double> foo = () -> { return null; };
RatioGauge bar = () -> { return null; };
The second statement has a compile error (in Eclipse):
The target type of this expression must be a functional interface
As far as I can tell, RatioGauge is a functional interface. Am I missing something?
An abstract class (even if it only has one abstract method) is not a functional interface. Only an interface can be one.
From JLS 9.8:
A functional interface is an interface that has just one abstract method (aside from the methods of Object)... (emphasis added)
The original idea was to let abstact classes be expressed as a lambda; they were called "SAM types," which stood for "single abstract method." That turned out to be a difficult problem to solve efficiently. This thread talks a bit about why; basically, the base class's constructor made it difficult.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With