-
Notifications
You must be signed in to change notification settings - Fork 152
Description
Lib version: "0.10.1"
There is no validator for types refined with Size[n] predicate. Which leads to an error:
could not find implicit value for parameter v: eu.timepit.refined.api.Validate[Long, T]
In "Practical FP in Scala" book there is this workaround (SizeValidator on the code), but it doesn't work for me anyway with error:
exception during macro expansion:
java.lang.ClassNotFoundException: Playground$Test2$
at scala.reflect.internal.util.AbstractFileClassLoader.findClass(AbstractFileClassLoader.scala:75)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at __wrapper$3$9ef546e2ad714e8c98288e38cb01ef12.__wrapper$3$9ef546e2ad714e8c98288e38cb01ef12$.wrapper(:31)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.$anonfun$compile$11(ToolBoxFactory.scala:291)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.eval(ToolBoxFactory.scala:460)
at scala.reflect.macros.contexts.Evals.eval(Evals.scala:32)
at scala.reflect.macros.contexts.Evals.eval$(Evals.scala:26)
at scala.reflect.macros.contexts.Context.eval(Context.scala:18)
at eu.timepit.refined.macros.MacroUtils.$anonfun$eval$1(MacroUtils.scala:22)
at scala.Option.getOrElse(Option.scala:201)
at eu.timepit.refined.macros.MacroUtils.tryN(MacroUtils.scala:26)
at eu.timepit.refined.macros.MacroUtils.tryN$(MacroUtils.scala:25)
at eu.timepit.refined.macros.RefineMacro.tryN(RefineMacro.scala:10)
at eu.timepit.refined.macros.MacroUtils.eval(MacroUtils.scala:22)
at eu.timepit.refined.macros.MacroUtils.eval$(MacroUtils.scala:15)
at eu.timepit.refined.macros.RefineMacro.eval(RefineMacro.scala:10)
at eu.timepit.refined.macros.RefineMacro.$anonfun$validateInstance$2(RefineMacro.scala:53)
at scala.Option.getOrElse(Option.scala:201)
at eu.timepit.refined.macros.RefineMacro.validateInstance(RefineMacro.scala:53)
at eu.timepit.refined.macros.RefineMacro.impl(RefineMacro.scala:25)
import Typ._
import eu.timepit.refined.api.{Refined, Validate}
import eu.timepit.refined.auto._
import eu.timepit.refined.collection.Size
trait SizeValidator {
implicit def validateSizeN[N <: Int, R](implicit
w: ValueOf[N]
): Validate.Plain[R, Size[N]] =
Validate.fromPredicate[R, Size[N]](
_.toString.length == w.value,
_ => s"Must have ${w.value} digits",
Size[N](w.value)
)
}
object Example extends App {
println("Hello")
println(Test2.x)
}
object Typ {
type SixP = Int Refined Size[6]
}
// won't work at all because validator not found
/*object Test1 {
val y: SixP = 123
val x: SixP = refineV[SixP](123).toOption.get
}*/
// will throw macros error
object Test2 extends SizeValidator {
val x: SixP = 123
}
Here is scastie runnable example: https://scastie.scala-lang.org/frNAMizRRpaTBaoeJjjrEQ