7
7
import scala .runtime .*;
8
8
9
9
import static junit .framework .Assert .assertEquals ;
10
- import static scala .runtime .java8 .JFunction .*;
11
10
import static scala .runtime .java8 .TestAPI .*;
12
11
13
12
import org .junit .Test ;
14
13
15
14
16
15
public class LambdaTest {
16
+ /*
17
+ // This version is for Scala 2.12.0-RC1 and is not compatible with 2.11. It's commented out to allow cross-building.
17
18
@Test
18
19
public void lambdaDemo() {
19
- // Not allowed with Scala 2.10 nor 2.11
20
- // "incompatible types: Function1 is not a functional interface"
21
- // scala.Function1<String, String> f = (String s) -> s;
22
-
23
- // Function1 is not a functional interface because it has abstract
24
- // methods in addition to apply, namely `compose` and `andThen`
25
- // (which are implemented in Scala-derived subclasses with mixin
26
- // inheritance), and the specialized variants of apply (also provided
27
- // by scalac.)
28
-
29
- // That's a pity, but we can get pretty close with this library!
30
-
31
- // We have to tell javac to use `JFunction1` as the functional interface.
32
- JFunction1 <String , String > f1 = (String s ) -> s ;
20
+ scala.Function1<String, String> f1 = (String s) -> s;
33
21
34
22
// That's more or less equivalent to the old, anonymous class syntax:
35
- new JFunction1 <String , String >() {
23
+ new scala.Function1 <String, String>() {
36
24
public String apply(String s) { return s; }
37
25
};
38
26
@@ -47,80 +35,73 @@ public void lambdaDemo() {
47
35
// F1 is a subclass of Function1:
48
36
scala.Function1<String, String> f2 = f1;
49
37
50
- // Factory methods in `JFunction` can reduce the verbosity a little:
51
- // `func` is actually just an identity method; it only exists to
52
- // trigger lambda creation using the `JFunction1` functional interface.
53
- scala .Function1 <String , String > f3 = func ((String s ) -> s );
54
-
55
- // Note that javac's type inference can infer the parameter type here,
56
- // based on the acribed type of `f4`.
57
- scala .Function1 <String , String > f4 = func (s -> s );
58
-
59
- // f1.apply("");
38
+ scala.Function1<String, String> f3 = (String s) -> s;
39
+ scala.Function1<String, String> f4 = s -> s;
60
40
61
41
// Specialized variants of the `apply` method are implenented in the
62
42
// functional interface
63
- JFunction1 <Integer , Integer > f5 = (i ) -> -i ;
43
+ scala.Function1 <Integer, Integer> f5 = (i -> -i) ;
64
44
assert(f5.apply(1) == -1);
65
45
assert(f5.apply$mcII$sp(1) == -1);
66
46
67
47
// as are `curried`, `tupled`, `compose`, `andThen`.
68
48
f3.compose(f3).andThen(f3).apply("");
69
- scala .Function2 <String , String , String > f6 = func ((s1 , s2 ) -> join (s1 , s2 ));
49
+ scala.Function2<String, String, String> f6 = ((s1, s2) -> join(s1, s2));
70
50
assert(f6.curried().apply("1").apply("2").equals("12"));
71
51
72
- // Functions returning unit must use the `JProcedure1`, ... functional interfaces
73
- // in order to convert a void lamdba return to Scala's Unit.
74
- //
75
- // The easiest way to do this is via `JFunction.proc`, ....
52
+ // Functions returning unit must return BoxedUnit.UNIT explicitly.
76
53
//
77
54
// Note that the lambda has a return type of `void` if the last
78
55
// statement is a call to a `void` returning method, or if it is
79
56
// a `return`.
80
- scala .Function1 <String , BoxedUnit > f7 = proc (s -> sideEffect ());
81
- scala .Function1 <String , BoxedUnit > f8 = proc (s -> {s .toUpperCase (); return ; });
57
+ scala.Function1<String, BoxedUnit> f7 = (s -> { sideEffect(); return scala.runtime.BoxedUnit.UNIT; } );
58
+ scala.Function1<String, BoxedUnit> f8 = (s -> { s.toUpperCase(); return scala.runtime.BoxedUnit.UNIT; });
82
59
83
60
// Function0 is also available
84
- scala .Function0 <String > f9 = func (() -> "42" );
61
+ scala.Function0<String> f9 = (() -> "42");
85
62
assert(f9.apply().equals("42"));
86
63
87
64
// You can go up to 22 (the highest arity function defined in the Scala standard library.)
88
- assert (acceptFunction1 (func (v1 -> v1 .toUpperCase ())).equals ("1" ));
89
- acceptFunction1Unit (proc (v1 -> sideEffect ()));
90
- acceptFunction1Unit (proc (v1 -> {v1 .toUpperCase (); return ;}));
65
+ assert(acceptFunction1((v1 -> v1.toUpperCase())).equals("1"));
66
+ acceptFunction1Unit((v1 -> { sideEffect(); return scala.runtime.BoxedUnit.UNIT;} ));
67
+ acceptFunction1Unit((v1 -> {v1.toUpperCase(); return scala.runtime.BoxedUnit.UNIT ;}));
91
68
92
- assert (acceptFunction2 (func ((v1 , v2 ) -> join (v1 , v2 ))).equals ("12" ));
93
- acceptFunction2Unit (proc ((v1 , v2 ) -> {v1 .toUpperCase (); return ;}));
69
+ assert(acceptFunction2(((v1, v2) -> join(v1, v2))).equals("12"));
70
+ acceptFunction2Unit(((v1, v2) -> {v1.toUpperCase(); return scala.runtime.BoxedUnit.UNIT ;}));
94
71
95
- assert (acceptFunction3 (func ((v1 , v2 , v3 ) -> join (v1 , v2 , v3 ))).equals ("123" ));
96
- acceptFunction3Unit (proc ((v1 , v2 , v3 ) -> {v1 .toUpperCase (); return ;}));
72
+ assert(acceptFunction3(((v1, v2, v3) -> join(v1, v2, v3))).equals("123"));
73
+ acceptFunction3Unit(((v1, v2, v3) -> {v1.toUpperCase(); return scala.runtime.BoxedUnit.UNIT ;}));
97
74
98
- assert (acceptFunction22 (func ((v1 , v2 , v3 , v4 , v5 , v6 , v7 , v8 , v9 , v10 , v11 , v12 , v13 , v14 , v15 , v16 , v17 , v18 , v19 , v20 , v21 , v22 ) -> join (v1 , v2 , v3 , v4 , v5 , v6 , v7 , v8 , v9 , v10 , v11 , v12 , v13 , v14 , v15 , v16 , v17 , v18 , v19 , v20 , v21 , v22 ))).equals ("12345678910111213141516171819202122" ));
99
- acceptFunction22Unit ( proc ((v1 , v2 , v3 , v4 , v5 , v6 , v7 , v8 , v9 , v10 , v11 , v12 , v13 , v14 , v15 , v16 , v17 , v18 , v19 , v20 , v21 , v22 ) -> {v1 .toUpperCase (); return ;}));
75
+ assert(acceptFunction22(((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) -> join(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22))).equals("12345678910111213141516171819202122"));
76
+ acceptFunction22Unit( ((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) -> {v1.toUpperCase(); return scala.runtime.BoxedUnit.UNIT ;}));
100
77
}
78
+ */
101
79
102
80
/*
103
81
// The JFunctions in 2.12.0-M4 are not Serializable anymore
104
82
@Test
105
83
public void isSerializable() {
106
- scala.runtime.java8.JFunction0 <String> f0 = () -> "foo";
84
+ scala.Function0 <String> f0 = () -> "foo";
107
85
assertEquals("foo", SerializationUtils.clone(f0).apply());
108
86
109
- scala.runtime.java8.JFunction1 <String, String> f1 = (a) -> a.toUpperCase();
87
+ scala.Function1 <String, String> f1 = (a) -> a.toUpperCase();
110
88
assertEquals("FOO", SerializationUtils.clone(f1).apply("foo"));
111
89
112
- scala.runtime.java8.JFunction2 <String, String, String> f2 = (a, b) -> a + b;
90
+ scala.Function2 <String, String, String> f2 = (a, b) -> a + b;
113
91
assertEquals("foobar", SerializationUtils.clone(f2).apply("foo", "bar"));
114
92
115
- scala.runtime.java8.JFunction3 <String, String, String, String> f3 = (a, b, c) -> a + b + c;
93
+ scala.Function3 <String, String, String, String> f3 = (a, b, c) -> a + b + c;
116
94
assertEquals("foobarbaz", SerializationUtils.clone(f3).apply("foo", "bar", "baz"));
117
95
}
118
96
*/
119
97
98
+ /*
99
+ // This version is for Scala 2.12.0-RC1 and is not compatible with 2.11. It's commented out to allow cross-building.
120
100
private static scala.concurrent.Future<Integer> futureExample(
121
101
scala.concurrent.Future<String> future, scala.concurrent.ExecutionContext ec) {
122
- return future .map (func ( s -> s .toUpperCase ()) , ec ).map (func ( s -> s .length () ), ec );
102
+ return future.map(s -> s.toUpperCase(), ec).map(s -> s.length(), ec);
123
103
}
104
+ */
124
105
125
106
private static void sideEffect () {
126
107
}
0 commit comments