1
votes

I try this short snippet of code, where I try to constrain an Integer type using the Positive type from the refined library (https://github.com/fthomas/refined).

package xxx

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 X = Integer Refined Positive
  def lala(x: Int): Unit = {
    val lala: X = refineV[X](x)
  }
}

When compiling this snippet, I get this error message:

Error:(13, 29) could not find implicit value for parameter v: eu.timepit.refined.api.Validate[Int,xxx.Lala.X] val lala: X = refineVX

Error:(13, 29) not enough arguments for method apply: (implicit v: eu.timepit.refined.api.Validate[Int,xxx.Lala.X])Either[String,eu.timepit.refined.api.Refined[Int,xxx.Lala.X]] in class RefinePartiallyApplied. Unspecified value parameter v. val lala: X = refineVX

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

1

1 Answers

4
votes

A quick look at the similar example in the Refined readme should help to solve your problems :

import eu.timepit.refined._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto._
import eu.timepit.refined.numeric._

// Int instead of Integer
type X = Int Refined Positive 

// refineV returns an Either[String, SomeType Refined Tag]
// (and uses a Validate[SomeType, Tag])
def foo(x: Int): Either[String, X] = refineV[Positive](x)

foo(5)  // Either[String,X] = Right(5)
foo(-1) // Either[String,X] = Left(Predicate failed: (-1 > 0).)