简体   繁体   English

Scala中依赖于路径的内部类型之间的关系

[英]relationship between path-dependent inner types in Scala

Warning: I'm cross-posting from #scala 警告:我是从#scala交叉发帖的

The book Programming in Scala states that path-dependent types are different depending on the exact instance of the path in question. Scala中的Programming一书指出,路径依赖类型根据所讨论路径的确切实例而有所不同。 If so, I don't understand why all the following predicates return true: 如果是这样,我不明白为什么以下所有谓词都返回true:

class Outer {
  val in = new Inner
  class Inner
}

val o1 = new Outer
val o2 = new Outer

o1.in.isInstanceOf[Outer#Inner] //makes perfect sense
o1.in.isInstanceOf[o1.Inner]    //still makes sense, the path-dependent type is o1's own
o1.in.isInstanceOf[o2.Inner]    //why is this true? PiS p.423 says the path-dependent types are different, they only share a common supertype Outer#Inner

o1.Inner and o2.Inner are different types, but their erasures are the same: o1.Innero2.Inner是不同的类型,但它们的删除是相同的:

scala> class Outer {
     |   val in = new Inner
     |   class Inner
     | }
defined class Outer

scala> val o1 = new Outer
o1: Outer = Outer@1d16ecf

scala> val m1 = implicitly[Manifest[o1.Inner]]
m1: Manifest[o1.Inner] = Outer@1d16ecf.type#Outer$Inner

scala> m1.erasure
res0: java.lang.Class[_] = class Outer$Inner

scala> val o2 = new Outer
o2: Outer = Outer@138ef1d

scala> val m2 = implicitly[Manifest[o2.Inner]]
m2: Manifest[o2.Inner] = Outer@138ef1d.type#Outer$Inner

scala> m2.erasure
res1: java.lang.Class[_] = class Outer$Inner

and o1.in.isInstanceOf[o2.Inner] can only check that o1.in is an instance of the erasure of o2.Inner . o1.in.isInstanceOf[o2.Inner]只能检查o1.in是擦除的实例o2.Inner

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM