我正在阅读斯卡拉之旅:抽象类型.什么时候使用抽象类型更好?
例如,
abstract class Buffer {
type T
val element: T
}
Run Code Online (Sandbox Code Playgroud)
而是那些泛型,例如,
abstract class Buffer[T] {
val element: T
}
Run Code Online (Sandbox Code Playgroud) 我很确定我在这里遗漏了一些东西,因为我对Shapeless很新,而且我正在学习,但是Aux技术何时需要呢?我看到它用于通过将type语句提升到另一个"伴侣" type定义的签名来公开语句.
trait F[A] { type R; def value: R }
object F { type Aux[A,RR] = F[A] { type R = RR } }
Run Code Online (Sandbox Code Playgroud)
但是这不等于将R放入F的类型签名中吗?
trait F[A,R] { def value: R }
implicit def fint = new F[Int,Long] { val value = 1L }
implicit def ffloat = new F[Float,Double] { val value = 2.0D }
def f[T,R](t:T)(implicit f: F[T,R]): R = f.value
f(100) // res4: Long = 1L
f(100.0f) // res5: Double = …Run Code Online (Sandbox Code Playgroud) 这个问题之前可能会被提出并回答,但我想通过一个例子来理解这个问题,我无法推断出Aux模式可能有用的地方!所以这是特征:
trait Foo[A] {
type B
def value: B
}
Run Code Online (Sandbox Code Playgroud)
为什么我有一个类型绑定到值函数的返回类型?我这样做了什么?特别是,我会在哪里使用这种模式?
我正在尝试将库从 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]
}
Run Code Online (Sandbox Code Playgroud)
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] …Run Code Online (Sandbox Code Playgroud) 在类型级别,我偶然发现了以下内容:
sealed abstract class StSource[A] {
type S
def init: S // create the initial state
def emit(s: S): (A, S) // emit a value, and update state
}
object StSource {
type Aux[A, S0] = StSource[A] {type S = S0}
def apply[A, S0](i: S0)(f: S0 => (A, S0)): Aux[A, S0] =
new StSource[A] {
type S = S0
def init = i
def emit(s: S0) = f(s)
}
}
Run Code Online (Sandbox Code Playgroud)
引起我兴趣的那条线是 type Aux[A, S0] = StSource[A] {type S = S0} …