Exposing a path-dependent type coming from a singleton type

放肆的年华 提交于 2019-12-06 02:40:55

问题


I'm trying to make Scala find the right type for a path-dependent type coming from a singleton type.

First, here is the type container for the example, and one instance:

trait Container {
  type X
  def get(): X
}

val container = new Container {
  type X = String
  def get(): X = ""
}

I can see the String in this first attempt (so I already have a working scenario):

class WithTypeParam[C <: Container](val c: C) {
  def getFromContainer(): c.X = c.get()
}

val withTypeParam = new WithTypeParam[container.type](container)

// good, I see the String!
val foo: String = withTypeParam.getFromContainer()

But when there is no type parameter, this does not work anymore.

class NoTypeParam(val c: Container) {
  def getFromContainer(): c.X = c.get()
}

val noTypeParam = new NoTypeParam(container)

// this does *not* compile
val bar: String = noTypeParam.getFromContainer()

Does anybody know why the type parameter is needed?


回答1:


See this thread on scala-internals, in particular, Adriaan's explanation.



来源:https://stackoverflow.com/questions/9065343/exposing-a-path-dependent-type-coming-from-a-singleton-type

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