Using Peter Neyens help answer , I tried to create a class Xthat consists only of Natless than or equal to 2.
import shapeless._
import shapeless.ops.nat._
import shapeless.nat._
case class X[A <: Nat](values: List[A])(implicit ev: LTEq[A, _2])
The following works:
scala> X( List(_1, _1, _1) )
res6: X[shapeless.nat._1] = X(List(Succ(), Succ(), Succ()))
But when I used different ones Nat, i.e. _1and _2, I got a compile time error:
scala> X( List(_1, _2) )
<console>:23: error: could not find implicit value for parameter ev:
shapeless.ops.nat.LTEq[shapeless.Succ[_ >: shapeless.Succ[shapeless._0]
with shapeless._0 <: Serializable with shapeless.Nat],shapeless.nat._2]
X( List(_1, _2) )
^
Then I tried:
scala> case class X(values: List[_ <: Nat])
defined class X
which works for sub-classesof Natbut it is not limited <= 2:
scala> X(List(_1, _2, _3))
res8: X = X(List(Succ(), Succ(), Succ()))
How can I write mine above Xwhich has a list Natthat fall in a range 0 to 2?
My immediate thought is to use the Algebraic data type for MyZero, MyOneand MyTwo, but not sure, the cleanest way.
, , X . X?