Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing a path-dependent type coming from a singleton type

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?

like image 273
betehess Avatar asked Nov 13 '22 11:11

betehess


1 Answers

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

like image 95
Miles Sabin Avatar answered Dec 20 '22 00:12

Miles Sabin