【发布时间】:2014-07-31 13:42:08
【问题描述】:
我试图理解 Shapeless,但我遇到了这个:
// Base trait for type level natural numbers.
trait Nat {
type N <: Nat
}
// Encoding of successor.
case class Succ[P <: Nat]() extends Nat {
type N = Succ[P]
}
// Encoding of zero.
class _0 extends Nat {
type N = _0
}
_0 是一个特殊且独特的情况,例如Nil 是List。 _0 没有前任。为什么它不是一个对象/案例对象(它是一个单例)? HLists 似乎是这样做的:
// `HList` ADT base trait.
sealed trait HList
// Non-empty `HList` element type.
final case class ::[+H, +T <: HList](head : H, tail : T) extends HList {
override def toString = head+" :: "+tail.toString
}
// Empty `HList` element type.
sealed trait HNil extends HList {
def ::[H](h : H) = shapeless.::(h, this)
override def toString = "HNil"
}
// Empty `HList` value.
case object HNil extends HNil
【问题讨论】:
标签: scala functional-programming shapeless hlist