77import scala .runtime .*;
88
99import static junit .framework .Assert .assertEquals ;
10- import static scala .runtime .java8 .JFunction .*;
1110import static scala .runtime .java8 .TestAPI .*;
1211
1312import org .junit .Test ;
1413
1514
1615public 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.
1718 @Test
1819 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;
3321
3422 // That's more or less equivalent to the old, anonymous class syntax:
35- new JFunction1 <String , String >() {
23+ new scala.Function1 <String, String>() {
3624 public String apply(String s) { return s; }
3725 };
3826
@@ -47,80 +35,73 @@ public void lambdaDemo() {
4735 // F1 is a subclass of Function1:
4836 scala.Function1<String, String> f2 = f1;
4937
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;
6040
6141 // Specialized variants of the `apply` method are implenented in the
6242 // functional interface
63- JFunction1 <Integer , Integer > f5 = (i ) -> -i ;
43+ scala.Function1 <Integer, Integer> f5 = (i -> -i) ;
6444 assert(f5.apply(1) == -1);
6545 assert(f5.apply$mcII$sp(1) == -1);
6646
6747 // as are `curried`, `tupled`, `compose`, `andThen`.
6848 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));
7050 assert(f6.curried().apply("1").apply("2").equals("12"));
7151
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.
7653 //
7754 // Note that the lambda has a return type of `void` if the last
7855 // statement is a call to a `void` returning method, or if it is
7956 // 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; });
8259
8360 // Function0 is also available
84- scala .Function0 <String > f9 = func (() -> "42" );
61+ scala.Function0<String> f9 = (() -> "42");
8562 assert(f9.apply().equals("42"));
8663
8764 // 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 ;}));
9168
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 ;}));
9471
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 ;}));
9774
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 ;}));
10077 }
78+ */
10179
10280 /*
10381 // The JFunctions in 2.12.0-M4 are not Serializable anymore
10482 @Test
10583 public void isSerializable() {
106- scala.runtime.java8.JFunction0 <String> f0 = () -> "foo";
84+ scala.Function0 <String> f0 = () -> "foo";
10785 assertEquals("foo", SerializationUtils.clone(f0).apply());
10886
109- scala.runtime.java8.JFunction1 <String, String> f1 = (a) -> a.toUpperCase();
87+ scala.Function1 <String, String> f1 = (a) -> a.toUpperCase();
11088 assertEquals("FOO", SerializationUtils.clone(f1).apply("foo"));
11189
112- scala.runtime.java8.JFunction2 <String, String, String> f2 = (a, b) -> a + b;
90+ scala.Function2 <String, String, String> f2 = (a, b) -> a + b;
11391 assertEquals("foobar", SerializationUtils.clone(f2).apply("foo", "bar"));
11492
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;
11694 assertEquals("foobarbaz", SerializationUtils.clone(f3).apply("foo", "bar", "baz"));
11795 }
11896 */
11997
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.
120100 private static scala.concurrent.Future<Integer> futureExample(
121101 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);
123103 }
104+ */
124105
125106 private static void sideEffect () {
126107 }
0 commit comments