Enforce Bounded Nat?

后端 未结 1 799
甜味超标
甜味超标 2021-01-26 11:39

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

Example:

def lessThan5(x: NatLT5) = ???

where

lessThan5(Nat

1条回答
  •  臣服心动
    2021-01-26 12:26

    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)
提交回复
热议问题