F-begrenzte Quantifizierung durch Typelement anstelle von Typparameter?
Ich möchte einen Typparameter in ein Typparameter verschieben.
Dies ist der Ausgangspunkt, der funktioniert:
trait Sys[S <: Sys[S]] {
type Tx
type Id <: Identifier[S#Tx]
}
trait Identifier[Tx] {
def dispose()(implicit tx: Tx): Unit
}
trait Test[S <: Sys[S]] {
def id: S#Id
def dispose()(implicit tx: S#Tx) {
id.dispose()
}
}
Was mich nervt ist, dass ich einen Typparameter mit mir herumtrage[S <: Sys[S]]
in meinen gesamten Bibliotheken. Also, was ich dachte, ist das:
trait Sys {
type S = this.type // ?
type Tx
type Id <: Identifier[S#Tx]
}
trait Identifier[Tx] {
def dispose()(implicit tx: Tx): Unit
}
trait Test[S <: Sys] {
def id: S#Id
def dispose()(implicit tx: S#Tx) {
id.dispose()
}
}
Welches scheitert ...S#Tx
undS#Id
wurde irgendwie losgelöst:
error: could not find implicit value for parameter tx: _9.Tx
id.dispose()
^
Irgendwelche Tricks oder Änderungen, die es funktionieren lassen?
BEARBEITEN : Zur Verdeutlichung hoffe ich in erster Linie, den Typ zu behebenS
imSys
damit es funktioniert. In meinem Fall gibt es zahlreiche Probleme bei der Verwendung von pfadabhängigen Typen. Um nur ein Beispiel zu nennen, das die Antworten von Pedrofuria und Owen widerspiegelt:
trait Foo[S <: Sys] {
val s: S
def id: s.Id
def dispose()(implicit tx: s.Tx) {
id.dispose()
}
}
trait Bar[S <: Sys] {
val s: S
def id: s.Id
def foo: Foo[S]
def dispose()(implicit tx: s.Tx) {
foo.dispose()
id.dispose()
}
}
<console>:27: error: could not find implicit value for parameter tx: _106.s.Tx
foo.dispose()
^
Versuchen Sie das zu machendef foo: Foo[s.type]
um Ihnen eine Vorstellung davon zu geben, dass dies nirgendwo hin führt.