Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functional interfaces in Java 8

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?

like image 669
Josh Stone Avatar asked Apr 01 '14 19:04

Josh Stone


1 Answers

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.

like image 167
yshavit Avatar answered Oct 19 '22 17:10

yshavit