<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>Functional Interfaces vs. Function Types in Java with Lambdas</article-title>
      </title-group>
      <contrib-group>
        <aff id="aff0">
          <label>0</label>
          <institution>Martin Plu ̈micke Baden-Wuerttemberg Cooperative State University Stuttgart Department of Computer Science Florianstraße 15</institution>
          ,
          <addr-line>D-72160 Horb</addr-line>
        </aff>
      </contrib-group>
      <fpage>146</fpage>
      <lpage>147</lpage>
      <abstract>
        <p>In several steps the Java type system is extended by features, which we know from functional programming languages. In Java 5.0 [GJSB05] generic types as well as a limited form of existential types (bounded wildcards) are introduced. In Java 8 the language will be expanded by lambda expressions, functional interfaces as target types, method and constructor references and default methods. In an earlier approach real function types were introduced as types of lambdas. The function types have been replaced by functional interfaces (interfaces with one method). Brian Goetz [Goe13] gave some reasons for this decision, why no function types have been introduced: the mix of structural and nominal types, the divergence of library styles - some libraries would continue to use callback interfaces, while others would use structural function types, the syntax could be unwieldy, there would not be a runtime representation for each distinct function type, caused by type erasure, e.g, it would not be possible (perhaps surprisingly) to overload methods m(T-&gt;U) and m(X-&gt;Y). This decision allows, however, to use lambda expressions as an abbreviation for anonymous inner classes. In this paper we will show some disadvantages and propose an idea to allow both function types and functional interfaces.</p>
      </abstract>
      <kwd-group>
        <kwd>- Extended Abstract -</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Introduction</title>
    </sec>
    <sec id="sec-2">
      <title>Functional interfaces vs. function types</title>
      <p>First we will present the idea of functional interfaces as target types of lambda expressions.
A lambda expression in Java 8 has no explicit type. The type is inferred by the compiler
from the context in which the expression appears. This means that one lambda expression
can have different types in different contexts.</p>
      <p>Callable&lt;String&gt; c = () -&gt; "done";</p>
      <p>PrivilegedAction&lt;String&gt; a = () -&gt; "done";
There is a canonical target type of a lambda expression (T1 a1, ..., TN aN ) -&gt; lbody
interface FunN &lt;R,T1 , ..., TN &gt; f R apply(T1 arg1 , ..., TN argN );g
where the type variables T1 , . . . , TN are instanced, respectively.</p>
      <p>Using functional interfaces as function types there are two disadvantages wrt. subtyping
and direct evaluation of lambda expressions:
Subtyping: While for real function types there is a subtyping relation
for functional interfaces this holds only by introducing wildcards
FunN&lt;T0; T10; : : : ; T N0 &gt; FunN&lt;? extends T00; ? super T1; : : : ; ? super TN &gt;; Ti Ti0:
Evaluation of lambda expressions: In the -calculus a lambda expression can be applied
directly to its arguments like: ((x1; ; : : : ; xN ) ! h(x1; ; : : : ; xN ))(a1: : : : ; aN ):
In Java 8 this is only possible by applying the method of the functional interface and
introducing a type-cast
((FunN&lt;T0; T1; : : : ; TN&gt; )(x1,..., xN)-&gt;h(x1,..., xN)).apply(a1....,aN);
For curried functions this becomes very unbeautiful:
((Fun1&lt;Fun1&lt;Fun1&lt;: : : Fun1&lt;T0; TN&gt;; : : :&gt;; T2&gt;; T1&gt; )
((x1) -&gt; (x2) -&gt;...-&gt; (xN) -&gt; h(x1,...,XN))).apply(a1).....apply(aN));
3</p>
    </sec>
    <sec id="sec-3">
      <title>Solution</title>
      <p>We propose to extend Java 8 by real function types, such that lambda expressions have
explicite types. Additionally we maintain the target typing of lambda expressions, which
means that a function type is then converted automatically to a functional interface. Then
Java would have the properties: lambda expressions as abbreviations for anonymous inner
classes, subtyping as expected for function types and the possibility to apply a lambda
expression to its arguments, directly.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [GJSB05]
          <string-name>
            <given-names>James</given-names>
            <surname>Gosling</surname>
          </string-name>
          , Bill Joy, Guy Steele, and
          <string-name>
            <given-names>Gilad</given-names>
            <surname>Bracha. The JavaT M Language</surname>
          </string-name>
          <article-title>Specification</article-title>
          .
          <source>The Java series. Addison-Wesley, 3rd edition</source>
          ,
          <year>2005</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [Goe13]
          <string-name>
            <given-names>Brian</given-names>
            <surname>Goetz</surname>
          </string-name>
          .
          <article-title>State of the Lambda</article-title>
          ,
          <year>September 2013</year>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>