如果您想知道 scalac 对代码的作用,请询问它。;)
这可以通过scalac -Xprint:typer <file>
2.10 中的任一或新的反射 API 实现:
scala> import reflect.runtime.universe._
import reflect.runtime.universe._
scala> reify{List(1,2,3).map(_+1)}
res0: reflect.runtime.universe.Expr[List[Int]] = Expr[List[Int]](immutable.this.List.apply(1, 2, 3).map(((x$1) => x$1.$plus(1)))(immutable.this.List.canBuildFrom))
因此,map
用这个打电话,CanBuildFrom
一切正常。真的吗?不,它没有!问题是 Java 编译器愚蠢地推断map
. 那么该怎么办?我相信唯一的方法是创造所需的价值,然后他们将它们扔掉。最后混合一些SuppressWarnings-Annotations
,代码应该可以正常工作。;)
这就是我想出的:
import scala.Function1;
import scala.collection.generic.CanBuildFrom;
import scala.collection.immutable.List;
import scala.collection.immutable.List$;
import scala.runtime.AbstractFunction1;
public class JTest {
@SuppressWarnings({"unchecked", "rawtypes"})
public static void main(final String... args) {
final List<Integer> xxx = (List) List$.MODULE$.apply(Predef.wrapIntArray(new int[] {1,2,3}));
System.out.println(xxx);
System.out.println(Test.sum(1, 2));
final Abc abc = new Abc();
System.out.println(abc.hello("simon"));
final List<Integer> xs = (List) Test.xs();
final Function1<Integer, String> mapper = new AbstractFunction1<Integer, String>() {
@Override
public String apply(final Integer i) {
return String.valueOf(i);
}
};
final CanBuildFrom<List<Integer>, String, List<String>> cbf =
(CanBuildFrom) List.<Integer>canBuildFrom();
final List<String> ys = xs.<String, List<String>>map(mapper, cbf);
System.out.println(ys);
}
}
名单:
object Test {
def xs = List(1,2,3)
}
我相信,最好的办法是不要使用 Java 中的 Scala 代码。它看起来很丑。至少,将它包装在一些帮助类中,这样看到这段代码的人就不会想到对他的视网膜进行酸攻击。
顺便说一句,有时您必须查看字节码才能了解 scalac 如何创建值。如果要创建 Scala List is Java,则必须使用以下命令反编译代码javap -c -s -l -verbose -private <classfile>
:
0: aload_0
1: invokespecial #20; //Method java/lang/Object."<init>":()V
4: aload_0
5: putstatic #22; //Field MODULE$:LX$;
8: aload_0
9: getstatic #27; //Field scala/collection/immutable/List$.MODULE$:Lscala/collection/immutable/List$;
12: getstatic #32; //Field scala/Predef$.MODULE$:Lscala/Predef$;
15: iconst_3
16: newarray int
18: dup
19: iconst_0
20: iconst_1
21: iastore
22: dup
23: iconst_1
24: iconst_2
25: iastore
26: dup
27: iconst_2
28: iconst_3
29: iastore
30: invokevirtual #38; //Method scala/LowPriorityImplicits.wrapIntArray:([I)Lscala/collection/mutable/WrappedArray;
33: invokevirtual #42; //Method scala/collection/immutable/List$.apply:(Lscala/collection/Seq;)Lscala/collection/immutable/List;
36: new #44; //class X$$anonfun$1
或者在更易读的 Java 代码中:
@SuppressWarnings({"unchecked", "rawtypes"})
final List<Integer> xs = (List) List$.MODULE$.apply(Predef.wrapIntArray(new int[] {1,2,3}));