Scala self type with dependent typing

This will not compile:

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

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

How does a self-type constraint depend on a value element in this trait?

+4
source share
2 answers

He cannot (and not sure what that will mean).

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

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

Well, I would not do that, but you can; just type Pathinside 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
}
+1
source

Source: https://habr.com/ru/post/1661117/


All Articles