Path-dependent argument type is not enforced (?)

杀马特。学长 韩版系。学妹 提交于 2020-01-06 15:48:10

问题


class Outter {
  type Inner = Either[Int, String]

  def f(x: this.Inner) = 1
}

val o = new Outter
val someLeft = Left(1)
o.f(someLeft)

tried at the REPL:

scala> :load Learn.scala
Loading Learn.scala...
defined class Outter
o: Outter = Outter@28037ced
someLeft: scala.util.Left[Int,Nothing] = Left(1)
res0: Int = 1

Which confuses the hell out of me as Outter.f() has an explicit path-dependent type argument (x: this.Inner) which means only those Lefts (or Rights) that are (a sub-type of) o.Inner are allowed and not just any Left[Int,_] or Right[Int,_]. I expect to see a type mismatch error along the lines :

Required o.Inner, Found Left[Int,Nothing].

But it just compiles fine! What am I (badly) missing here ?


回答1:


type Inner = Either[Int, String] is just a type alias. This is the expected behavior.

only those Lefts (or Rights) that are (a sub-type of) o.Inner are allowed and not just any Left[Int, _] or Right[Int, _].

There is only one Left and one Right class, respectively. There are no Lefts or Rights that are only a sub-type of o.Inner and nothing else. Creating a type alias does not create a new type that belongs only to class Outter. The only thing that is unique to Outter is the alias name Inner, but Either is the same old scala.util.Either.



来源:https://stackoverflow.com/questions/35557655/path-dependent-argument-type-is-not-enforced

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!