Path-dependent argument type is not enforced (?)

2019-08-29 07:09发布

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条回答
我想做一个坏孩纸
2楼-- · 2019-08-29 07:24

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.

查看更多
登录 后发表回答