使用Scala中的隐式返回类型的Stange

时间:2015-11-05 15:35:47

标签: scala

我无法理解。为什么每个函数使用类型约束都有不同的返回类型。

首先我确实如下所示创建了函数User,它的返回类型是Int

if( /*arrayList contains only Person and User*/ ) {
  return null;
}
else {
  //arrayList contains other objects besides Person and User
  return arrayList;
}

然后我确实使用下面的类型约束来创建函数inv,它的'返回类型是Function1

implicit val x = 10
def inv[A](a: A)(implicit ev: Int) = ev
inv(1) // it return 10

// res0: Int = 10

1 个答案:

答案 0 :(得分:4)

android:configChanges的返回类型推断为inv2 - 正如所料。

但是,返回值的运行时类型为<:<[Int,Int]。那是因为Int => Int 是一个函数。

<:<

在函数上调用sealed abstract class <:<[-From, +To] extends (From => To) with Serializable 会导致toString被打印。