我尝试将一个库从Scala 2.13迁移到Scala 3,但现有代码无法编译。
这里有一个片段
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]
}
字符串
Scala 3编译器失败:
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
型
您可以直接在https://scastie.scala-lang.org/GuqSqC9yQS6uMiw9wyKdQg尝试
2条答案
按热度按时间csbfibhn1#
您可以使用match types(出于技术原因,也可以使用Aux-pattern,即细化类型
Env { type I = i }
不能是类型模式)字符串
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的
或者,您可以使用类型类。
nmpmafwu2#
下面是一个使用路径依赖类型的解决方案:
字符串