例如,假设我们有:
object Types {
type ObjectMap = collection.Map[String, Any]
}
class X {
def toObjectMap(x:Any): ObjectMap = x.asInstanceOf[Types.ObjectMap]
}
与以下相比,这是否会产生额外的运行时间惩罚:
class X {
def toObjectMap(x:Any): collection.Map[String, Any]= x.asInstanceOf[collection.Map[String, Any]]
}
答案 0 :(得分:4)
我不指望它,但你知道它就像,很容易尝试。
scala> :javap -prv X
public scala.collection.Map<java.lang.String, java.lang.Object> toObjectMap(java.lang.Object);
flags: ACC_PUBLIC
Code:
stack=1, locals=2, args_size=2
0: aload_1
1: checkcast #9 // class scala/collection/Map
4: areturn
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this L$line9/$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$X;
0 5 1 x Ljava/lang/Object;
LineNumberTable:
line 53: 0
Signature: #75 // (Ljava/lang/Object;)Lscala/collection/Map<Ljava/lang/String;Ljava/lang/Object;>;
public scala.collection.Map<java.lang.String, java.lang.Object> toObjectMap2(java.lang.Object);
flags: ACC_PUBLIC
Code:
stack=1, locals=2, args_size=2
0: aload_1
1: checkcast #9 // class scala/collection/Map
4: areturn
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this L$line9/$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$X;
0 5 1 x Ljava/lang/Object;
LineNumberTable:
line 54: 0
Signature: #75 // (Ljava/lang/Object;)Lscala/collection/Map<Ljava/lang/String;Ljava/lang/Object;>;
答案 1 :(得分:0)
类型别名只是简写。编译器扩展了别名,从那时起,就像你自己编写了类型一样。 (正如Som的回答所示,至少对你的特定例子而言。)