PLI
PLI

Reputation: 135

Compile error using the refined constraint Interval.Closed

I posted a similar question earlier, that was fixed. To ask this question, however, I simplified my initial problem. But, the solution to this simpler question does not seem to solve my initial problem

When I try this short snippet of code, where I try to constraint an Double type using the Interval.Closed type from the refined library (https://github.com/fthomas/refined), a compile error is produced.

import eu.timepit.refined._
import eu.timepit.refined.api.{Refined, Validate}
import eu.timepit.refined.auto._
import eu.timepit.refined.numeric._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.numeric.Interval

object Lala {
  type UnitReal = Double Refined Interval.Closed[W.`0.0`.T, W.`1.0`.T]

  def foo(x: Double): Either[String, UnitReal] = refineV[UnitReal](x)
}

This compile error is shown:

Error:(13, 67) could not find implicit value for parameter v: eu.timepit.refined.api.Validate[Double,xxx.Lala.UnitReal] def foo(x: Double): Either[String, UnitReal] = refineVUnitReal

From which it seems that a Validate implementation for the Interval.Closed type is missing. I was wondering whether someone could help me find an instance of the Validate trait for the Interval.Closed type? Or should I provide such an instance myself?

Upvotes: 2

Views: 380

Answers (1)

PLI
PLI

Reputation: 135

I was able to fix the problem by separating the constraint and the constrained type, and using the constraint to refine the value (i.e. refineVOneToZero):

object Lala {
  type OneToZero = Interval.Closed[W.`0.0`.T, W.`1.0`.T]
  type UnitReal = Double Refined OneToZero

  def foo(x: Double): Either[String, UnitReal] = refineV[OneToZero](x)
}

Upvotes: 2

Related Questions