Tongfei Chen
Tongfei Chen

Reputation: 613

Scala self type with dependent typing

This does not compile:

trait FileSystem {
    type P <: Path[this.type]
}

trait Path[S <: FileSystem] { self: fileSystem.P =>
    val fileSystem: S
}

How can a self-type constraint be dependent on a value member in that trait?

Upvotes: 4

Views: 193

Answers (2)

Eduardo Pareja Tobes
Eduardo Pareja Tobes

Reputation: 3120

Well I wouldn't do it, but you can; just put Path inside FileSystem:

trait AnyPath[S <: FileSystem] { self: S#P =>
  val fileSystem: S
}

trait FileSystem { thisFileSystem =>
  type P <: Path
  trait Path extends AnyPath[thisFileSystem.type]{ self: thisFileSystem.P =>
    lazy val fileSystem = thisFileSystem
  }
}

case object SomeFileSystem extends FileSystem {
  type P = ReallyConcretePath
  case class ReallyConcretePath() extends Path
}

Upvotes: 1

cchantep
cchantep

Reputation: 9158

It cannot (and not sure what it would mean).

trait FileSystem {
    type P <: Path[this.type]
}

trait Path[S <: FileSystem] { self: S#P =>
    val fileSystem: S
}

Upvotes: 4

Related Questions