Why is _0 Nat in Shapeless a class instead of an object?

I am trying to understand Shapeless, and I came across this:

// 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 is a special and unique case, for example Nil for a List . _0 has no predecessor. Why is this not an object object (which is single)? HList as follows:

 // `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 
+7
scala functional-programming shapeless hlist
source share
1 answer

(Just guessing, I don't know the original reason.)

It may be that the type _0 is available (pass it explicitly as in def fun[N <: Nat] = ???; fun[_0] , or it is easier to just define implications about this type).

If _0 were singleton, only the _0.type type would be available.

And the same for HNil ?

0
source share

All Articles