我正在通过drmacvier博客了解Scala中的存在类型。阅读完后,我正在试验类型,我正在按照以下方式检查类型的相等性,如rnduja博客中所述。T的类型forSome {type T}
def implicitly[A](implicit a: A) = a
// To check equality of two types
class =:=[A, B]
implicit def equalTypeInstance[A] = new =:=[A, A]
def type_==[A, B](implicit ev: A =:= B = null) = ev != null
// To check subtype relation between two types
class <:<[-A, +B]
implicit def subTypeInstance[A] = new <:<[A, A]
def type_<[A, B](implicit ev: A <:< B = null) = ev != null
我检查的第一件事情是这样的:
type_==[Any, T forSome{type T}] // true
什么我不明白是T forSome{type T}
是任何类型的满意,但为什么它的类型Any
。假设,因为Any
是所有可能类型的共同祖先,所以它们是平等的。用我能够推理的类似方式。
type_==[Array[Any], Array[T forSome{type T}]] // true
type_==[List[Any], List[T forSome{type T}]] // true
我不能以相同的推理得到这个权利。
type_==[Array[Any], (Array[T] forSome{type T})] // false
type_==[List[Any], (List[T] forSome{type T})] // true
我在这里错过了什么?我的推理方式有缺陷吗?
大概是因为List是协变的而且数组是不变的? –