Vit*_*uel 3 scala dependent-type shapeless
据我了解,依赖类型允许您不指定输出类型:
例如,如果您有一个类型类:
trait Last[In] {
type Out
}
Run Code Online (Sandbox Code Playgroud)
那么你可以在不指定输出类型的情况下召唤一个实例:
implicitly(Last[String :: Int :: HNil]) // output type calculated as Int
Run Code Online (Sandbox Code Playgroud)
Aux 模式允许您再次指定输出类型:
implicitly(Last.Aux[String :: Int :: HNil, Int])
Run Code Online (Sandbox Code Playgroud)
您需要在隐式参数列表中使用它,以便对输出类型做一些有用的事情(解决 Scala 对依赖类型的限制)。
但是如果你总是需要指定(或分配一个类型参数)输出类型,为什么首先要使用依赖类型(然后是 Aux)呢?
我尝试Last从 Shapeless 的 src复制类型类,替换type Out为特征中的附加类型参数并删除 Aux。它仍然有效。
当我真正需要它们时是什么情况?
我知道这
Sum[A, B]与Sum[A, B] { type Out = C }or 不同Sum.Aux[A, B, C]。我在问为什么我需要 typeOut而不仅仅是Sum[A, B, C].
区别在于部分应用。因为trait MyTrait { type A; type B; type C }您可以指定某些类型而不指定其他类型(期望编译器推断它们)。但是因为trait MyTrait[A, B, C]您只能指定所有这些或不指定任何一个。因为Sum[A, B] { type Out }您更愿意指定A,B而不是指定Out(期望编译器根据作用域中存在的隐式推断其值)。同样,trait Last[In] { type Out }您更愿意指定In而不是指定Out(期望编译器推断其值)。所以类型参数更像是输入,类型成员更像是输出。
https://www.youtube.com/watch?v=R8GksuRw3VI
抽象类型与类型参数和链接问题
但究竟什么时候,我更愿意指定
In而不是指定Out?
让我们考虑以下示例。这是一个用于添加自然数的类型类:
sealed trait Nat
case object Zero extends Nat
type Zero = Zero.type
case class Succ[N <: Nat](n: N) extends Nat
type One = Succ[Zero]
type Two = Succ[One]
type Three = Succ[Two]
type Four = Succ[Three]
type Five = Succ[Four]
val one: One = Succ(Zero)
val two: Two = Succ(one)
val three: Three = Succ(two)
val four: Four = Succ(three)
val five: Five = Succ(four)
trait Add[N <: Nat, M <: Nat] {
type Out <: Nat
def apply(n: N, m: M): Out
}
object Add {
type Aux[N <: Nat, M <: Nat, Out0 <: Nat] = Add[N, M] { type Out = Out0 }
def instance[N <: Nat, M <: Nat, Out0 <: Nat](f: (N, M) => Out0): Aux[N, M, Out0] = new Add[N, M] {
override type Out = Out0
override def apply(n: N, m: M): Out = f(n, m)
}
implicit def zeroAdd[M <: Nat]: Aux[Zero, M, M] = instance((_, m) => m)
implicit def succAdd[N <: Nat, M <: Nat, N_addM <: Nat](implicit add: Aux[N, M, N_addM]): Aux[Succ[N], M, Succ[N_addM]] =
instance((succN, m) => Succ(add(succN.n, m)))
}
Run Code Online (Sandbox Code Playgroud)
这个类型类在类型级别上都有效
implicitly[Add.Aux[Two, Three, Five]]
Run Code Online (Sandbox Code Playgroud)
和价值水平
println(implicitly[Add[Two, Three]].apply(two, three))//Succ(Succ(Succ(Succ(Succ(Zero)))))
assert(implicitly[Add[Two, Three]].apply(two, three) == five)//ok
Run Code Online (Sandbox Code Playgroud)
现在让我们用类型参数而不是类型成员重写它:
trait Add[N <: Nat, M <: Nat, Out <: Nat] {
def apply(n: N, m: M): Out
}
object Add {
implicit def zeroAdd[M <: Nat]: Add[Zero, M, M] = (_, m) => m
implicit def succAdd[N <: Nat, M <: Nat, N_addM <: Nat](implicit add: Add[N, M, N_addM]): Add[Succ[N], M, Succ[N_addM]] =
(succN, m) => Succ(add(succN.n, m))
}
Run Code Online (Sandbox Code Playgroud)
在类型级别它的工作方式类似
implicitly[Add[Two, Three, Five]]
Run Code Online (Sandbox Code Playgroud)
但是在值级别,现在您必须指定类型,Five而在前一种情况下,它是由编译器推断的。
println(implicitly[Add[Two, Three, Five]].apply(two, three))//Succ(Succ(Succ(Succ(Succ(Zero)))))
assert(implicitly[Add[Two, Three, Five]].apply(two, three) == five)//ok
Run Code Online (Sandbox Code Playgroud)
所以区别在于部分应用。
但是,如果您
+像往常一样添加语法糖以使其实用(shapeless 也适用于所有情况),则依赖类型似乎无关紧要
语法并不总是有帮助。例如,让我们考虑一个类型类,它接受一个类型(但不是这个类型的值)并产生一个这个类型的类型和值:
trait MyTrait {
type T
}
object Object1 extends MyTrait
object Object2 extends MyTrait
trait TypeClass[In] {
type Out
def apply(): Out
}
object TypeClass {
type Aux[In, Out0] = TypeClass[In] { type Out = Out0 }
def instance[In, Out0](x: Out0): Aux[In, Out0] = new TypeClass[In] {
override type Out = Out0
override def apply(): Out = x
}
def apply[In](implicit tc: TypeClass[In]): Aux[In, tc.Out] = tc
implicit val makeInstance1: Aux[Object1.T, Int] = instance(1)
implicit val makeInstance2: Aux[Object2.T, String] = instance("a")
}
println(TypeClass[Object1.T].apply())//1
println(TypeClass[Object2.T].apply())//a
Run Code Online (Sandbox Code Playgroud)
但是如果我们创建Out一个类型参数,那么在调用时我们将不得不指定Out并且无法定义扩展方法并In从元素类型推断类型参数,因为没有类型的元素Object1.T, Object2.T。