如何在shapeless中为3个或3个以上的类型提供类型连接?举个例子。
import shapeless._
object Tst extends App {
sealed trait Base
final case class A() extends Base
final case class B() extends Base
final case class C() extends Base
final case class D() extends Base
def AorB[T: (A |∨| B)#λ](t: T): Unit =
t match {
case _: A => println("A")
case _: B => println("B")
}
AorB(A()) //Ok
AorB(B()) //Ok
def AorBorC[T: (A |∨| B |∨| C)#λ](t: T): Unit =
t match {
case _: A => println("A")
case _: B => println("B")
case _: C => println("C")
}
AorBorC(A()) //compile-error
AorBorC(B()) //compile-error
AorBorC(C()) //Ok
}
正如我们所看到的,对于2个类型的分离,它的工作完全正常。但是对于3个类型的异构,它不能像预期的那样工作。
编译错误是
Error:(28, 10) Cannot prove that (Tst.A => Nothing) => Nothing <:< Object{type λ[X] = (X => Nothing) => Nothing <:< Tst.A => Nothing with Tst.B => Nothing => Nothing} => Nothing with Tst.C => Nothing => Nothing.
AorBorC(A())
和
Error:(29, 10) Cannot prove that (Tst.B => Nothing) => Nothing <:< Object{type λ[X] = (X => Nothing) => Nothing <:< Tst.A => Nothing with Tst.B => Nothing => Nothing} => Nothing with Tst.C => Nothing => Nothing.
AorBorC(B())
shapeless.|∨|
对于超过2个类型就不能用了。
http:/milessabin.comlog20110609scala-union-types-curry-howard
对于2种以上的类型,编码变得更加复杂。
一种编码是针对2, 4, 8 ... 类型的
type ¬¬¬¬[T] = ¬¬[¬¬[T]]
type |∨∨|[T, U] = {
type λ[X] = ¬¬¬¬[X] <:< (T ∨ U)
}
def AorBorC[T: ((A ∨ B) |∨∨| (C ∨ C))#λ](t: T): Unit =
t match {
case _: A => println("A")
case _: B => println("B")
case _: C => println("C")
}
AorBorC(A()) //Ok
AorBorC(B()) //Ok
AorBorC(C()) //Ok
另一种是对于任意数量的类型
trait Disj[T] {
type or[S] = Disj[T with ¬[S]]
type apply = ¬[T]
}
type ∨∨∨[T1, T2, T3] = Disj[¬[T1]]#or[T2]#or[T3]#apply
type |∨∨∨|[T1, T2, T3] = {
type λ[X] = ¬¬[X] <:< ∨∨∨[T1, T2, T3]
}
def AorBorC[T: |∨∨∨|[A, B, C]#λ](t: T): Unit =
t match {
case _: A => println("A")
case _: B => println("B")
case _: C => println("C")
}
AorBorC(A()) //Ok
AorBorC(B()) //Ok
AorBorC(C()) //Ok