I'm trying to migrate a library from Scala 2.13 to Scala 3, but the existing code does not compile.
Here is a snippet
trait Identity
trait Authenticator
trait Env {
type I <: Identity
type A <: Authenticator
}
trait IdentityService[T <: Identity]
trait AuthenticatorService[T <: Authenticator]
trait Environment[E <: Env] {
def identityService[T <: Identity]: IdentityService[E#I]
def authenticatorService: AuthenticatorService[E#A]
}
The Scala 3 compiler fails with:
error] 14 | def identityService[T <: Identity]: IdentityService[E#I]
[error] | ^
[error] | E is not a legal path
[error] | since it is not a concrete type
[error] -- Error:
[error] 15 | def authenticatorService: AuthenticatorService[E#A]
[error] | ^
[error] | E is not a legal path
[error] | since it is not a concrete type
[error] two errors found
You can try directly at https://scastie.scala-lang.org/GuqSqC9yQS6uMiw9wyKdQg
You can use match types (and Aux-pattern for technical reasons, namely refined types
Env { type I = i }
can't be type patterns)What does Dotty offer to replace type projections?
In Scala 3, how to replace General Type Projection that has been dropped?
https://users.scala-lang.org/t/converting-code-using-simple-type-projections-to-dotty/6516
Alternatively you can use type classes.