Enforce Bounded Nat?

后端 未结 1 1393
小鲜肉
小鲜肉 2021-01-26 11:59

How can I enforce a Nat that\'s <= N?

Example:

def lessThan5(x: NatLT5) = ???

where

lessThan5(Nat

相关标签:
1条回答
  • 2021-01-26 12:03

    You can use the type class LTEq (or LT if you want strictly less than).

    import shapeless.nat._
    import shapeless.ops.nat._
    
    def lessThan5[N <: Nat](n: N)(implicit ev: LTEq[N, _5]) = ???
    
    lessThan5(_4) // compiles
    lessThan5(_5) // compiles
    lessThan5(_6) // doesn't compile because LTEq[_6, _5] cannot be found
    
    0 讨论(0)
提交回复
热议问题