• Aucun résultat trouvé

Functional Interfaces vs. Function Types in Java with Lambdas

N/A
N/A
Protected

Academic year: 2022

Partager "Functional Interfaces vs. Function Types in Java with Lambdas"

Copied!
2
0
0

Texte intégral

(1)

Functional Interfaces vs. Function Types in Java with Lambdas

– Extended Abstract –

Martin Pl¨umicke

Baden-Wuerttemberg Cooperative State University Stuttgart Department of Computer Science

Florianstraße 15, D–72160 Horb pl@dhbw.de

1 Introduction

In several steps theJavatype system is extended by features, which we know from func- tional programming languages. InJava 5.0[GJSB05] generic types as well as a limited form of existential types (bounded wildcards) are introduced. InJava 8the language will be expanded by lambda expressions, functional interfaces as target types, method and constructor referencesanddefault 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 func- tion 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 methodsm(T->U)andm(X->Y).

This decision allows, however, to use lambda expressions as an abbreviation for anony- mous inner classes. In this paper we will show some disadvantages and propose an idea to allow both function types and functional interfaces.

2 Functional interfaces vs. function types

First we will present the idea of functional interfaces as target types of lambda expressions.

A lambda expression inJava 8has no explicit type. The type is inferred by the compiler

2014 for the individual papers by the papers’ authors. Copying permitted for private and academic pur-c poses.

146

(2)

from the context in which the expression appears. This means that one lambda expression can have different types in different contexts.

Callable<String> c = () -> "done";

PrivilegedAction<String> a = () -> "done";

There is a canonical target type of a lambda expression(T1 a1, ..., TNaN) -> lbody interface FunN<R,T1 , ..., TN> {R apply(T1 arg1 , ..., TN argN);}

where the type variablesT1, . . . ,TN are instanced, respectively.

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 T10×. . .×TN0 →T0 T1×. . .×TN →T00, TiTi0.

for functional interfaces this holds only by introducing wildcards

FunN<T0, T10, . . . , TN0 >≤ FunN<? extendsT00,? superT1, . . . ,? superTN>, TiTi0. 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<T0,T1, . . . ,TN> )(x1,..., xN)->h(x1,..., xN)).apply(a1....,aN);

For curried functions this becomes very unbeautiful:

((Fun1<Fun1<Fun1<. . .Fun1<T0,TN>, . . .>,T2>,T1> )

((x1) -> (x2) ->...-> (xN) -> h(x1,...,XN))).apply(a1)...apply(aN));

3 Solution

We propose to extendJava 8by 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 Javawould have the properties:lambda expressions as abbreviations for anonymous inner classes, subtyping as expected for function typesandthe possibility to apply a lambda expression to its arguments, directly.

References

[GJSB05] James Gosling, Bill Joy, Guy Steele, and Gilad Bracha. The JavaT M Language Specifi- cation. The Java series. Addison-Wesley, 3rd edition, 2005.

[Goe13] Brian Goetz. State of the Lambda, September 2013.

147

Références

Documents relatifs

cherche, s'ajouteront autour de cette structure pyrami- dale, trois groupes reliés au concept du pouvoir d'au- torité qui permettront d'identifier les types et les

Abstract Pseudopotential parameter sets for the elements from H to Kr using the relativistic, norm-conserving, sep- arable, dual-space Gaussian-type pseudopotentials of Goe-

Indefinite forms disappear from the narratives of group III; however, the choice of definite determiners in their narratives is always different from those used

Expressions of type int&amp;positive are obtained by an explicit check construct, analogous to cast, e.g., (positive) 2. In this way, the fact that the value of an expression

Due to a theorem by Dubins and Schwarz, by changing the time scale we can turn the local martingale into a Brownian motion and the problem of computing the pdfs of the spikes times

- Une variable d’instance (pas static) final est constante pour chaque instance ; mais elle peut avoir 2 valeurs différentes pour 2 instances. - Une variable d'instance final

This is done by giving an efficient inference algorithm for linear dependent types which, given a PCF term, produces in output both a linear dependent type and a cost expression for

Unit´e de recherche INRIA Rennes, IRISA, Campus universitaire de Beaulieu, 35042 RENNES Cedex Unit´e de recherche INRIA Rh ˆone-Alpes, 46 avenue F´elix Viallet, 38031 GRENOBLE Cedex