Exposing a path-dependent type coming from a singl

2019-04-09 04:01发布

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条回答
Emotional °昔
2楼-- · 2019-04-09 04:19

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

查看更多
登录 后发表回答