Scala F-bounded polymorphism on object

谁说胖子不能爱 提交于 2019-12-03 15:04:34

It really seems like you ought to be able to write,

trait X[T <: X[T]]
object Y extends X[Y.type]

however, if you try that the compiler will give you an unhelpful (and I think spurious) error,

scala> object Y extends X[Y.type]
<console>:16: error: illegal cyclic reference involving object Y
       object Y extends X[Y.type]

I say "spurious" because we can construct an equivalent object with a little bit of additional infrastructure,

trait X[T <: X[T]]

trait Fix { type Ytype >: Y.type <: Y.type; object Y extends X[Ytype] }
object Fix extends Fix { type Ytype = Y.type }
import Fix.Y

If you wanted to experiment with this in real code, using a package object in place of object Fix would make this idiom a little more usable.

Change it to :

  trait Y extends X[Y]

object is not a type in Scala, but the so called companion object. By defining object Y, you can't express that it should extend trait T[Y], since that second Y refers to a type Y that hasn't been defined. You can however do the following:

trait Y extends X[Y]          //If you try this on the REPL, do :paste before
object Y extends X[Y]

In this case the object Y extends X[Y] where the second Y is the trait you just define, make sure you keep this in mind.

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