Home > front end >  Getting predicate from Refined
Getting predicate from Refined

Time:03-19

Is it possible to extract the bounds from the predicate/witness of a Refined variable at runtime? Something like the following.

// Should return L as a Double
def getLowerBound[L, H](v: Refined[Double, Interval.Closed[L, H]]): Double = ???

val v: Refined[Double, Interval.Closed[0.5, 1.0]] = 0.94
val lowerBound = getLowerBound(v)
lowerBound shouldBe 0.5

CodePudding user response:

You can use a type bound to tell the compiler L will be a Double and then use the ValueOf typeclass to extract the value of the literal type; like this:

def getLowerBound[L <: Double, H](v: Refined[Double, Interval.Closed[L, H]])
                                 (implicit ev: ValueOf[L]): Double =
  ev.value

You can see the code running here.

  • Related