Getting predicate from Refined

127 views Asked by At

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
1

There are 1 answers

0
Luis Miguel Mejía Suárez On BEST ANSWER

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.