@coffius
With your example I would have done:
class GenericStringValidator private [Min <: Int, Max <: Int](minValue : Min, maxValue : Max) {
}
object GenericStringValidator {
def apply[Min <: Int with Singleton, Max <: Int with Singleton](min : Min, max : Max)(implicit r : Require[Min <= Max]) = new GenericStringValidator(min, max)
}
If you have further questions it's better to ask in the singleton-ops gitter
Map
or write an object ValidMap
with the appropriate helper functions.
RefineMacro
equivalent for Scala 3 right now. My current understanding is that it is not possible to have something similar in Scala 3. But I've not played a lot with Scala 3 yet, so maybe there is a way to have this functionality and I'm not aware of it.
Hi everyone. I am trying to make this string restrictions:
prefix: String Refined PrefixConstraints
type PrefixConstraints = NonEmpty And Not[Contains['.']] And Forall[LowerCase] And Head[Letter]
But i am getting errors with strings like "t-m" because of dash:
[error] java.lang.Error: ConvertFailure(CannotConvert("t-m",eu.timepit.refined.api.Refined[String,eu.timepit.refined.boolean.And[eu.timepit.refined.boolean.And[eu.timepit.refined.boolean.And[eu.timepit.refined.boolean.Not[eu.timepit.refined.collection.Empty],eu.timepit.refined.boolean.Not[eu.timepit.refined.boolean.Not[eu.timepit.refined.collection.Forall[eu.timepit.refined.boolean.Not[eu.timepit.refined.generic.Equal[Char('.')]]]]]],eu.timepit.refined.collection.Forall[eu.timepit.refined.char.LowerCase]],eu.timepit.refined.collection.Head[eu.timepit.refined.char.Letter]]],Left predicate of (((!isEmpty(t-m) && !!(!(t == .) && !(- == .) && !(m == .))) && (isLower('t') && isLower('-') && isLower('m'))) && isLetter('t')) failed: Right predicate of ((!isEmpty(t-m) && !!(!(t == .) && !(- == .) && !(m == .))) && (isLower('t') && isLower('-') && isLower('m'))) failed: Predicate failed: (isLower('t') && isLower('-') && isLower('m')).),
This is happens because of Forall[LowerCase]
. It seems that it is also restrict my string to only chars. Is there a workaround for this? I want to have numbers or dashes in my string with only lowercased chars.
Result[And[Result[P1], Result[P2]]]
before this whole Result
is translated to a String
but there is no API that exposes the Result
. A Result
is either Passed
or Failed
and would allow in your example to inspect which of the predicates P1
and P2
failed.
_ % 3
. Is there an alternative to using refineV[Interval.Closed[0,2]](x)
that doesn’t wraps the result in an Either (Knowing that since I do a modulo, all possible positive integers will satisfy the predicate ? Should I do something with singleton-ops ?
[error] 85 | val prefix: NonEmptyString = "u"
[error] | ^^^
[error] | Found: ("u" : String)
[error] | Required: eu.timepit.refined.types.all.NonEmptyString
[error] |
[error] | The following import might make progress towards fixing the problem:
[error] |
[error] | import eu.timepit.refined.auto.autoUnwrap
eu.timepit.refined.auto
are macro-based.
I was surprised to learn today that
val x: PosInt = 5 << 20
compiles fine during the compile
sbt task, but when compile:doc
runs, it blows up:
[error] /…code.scala: compile-time refinement only works with literals
[error] private val x: PosInt = 5 << 20
[error] ^
[info] Int(5242880) <: eu.timepit.refined.types.all.PosInt?
[info] false
[info] No documentation generated with unsuccessful compiler run
I'm curious why it's inconsistent. What is doc
doing differently that would cause this? (Scala 2.12.12, if it matters)
Hi there đź‘‹
I wonder if it is possible to validate a size of a string in compile time?
In my case I'd like to check currency codes and make sure that all code's values are strings with 3 chars in length.
I have defined the corresponding type alias as type _Code = String Refined Size[W.
3.T]
But when I try to use it like this
private val test: _Code = "123"
I get this compilation error
type mismatch;
found : String("123")
required: _Code
And it is unclear for me either it is impossible to validate this rule during compilation or I am missing some imports.
def getTrees(a: PlantID)(implicit di : DummyImplicit): List[Tree]
>
, !
, ==
, etc. that can be evaluated at compile-time but it would break down for more complex predicates like MatchesRegex
, Size
, etc.
compiletime.ops
since I was only looking at what functions can be called at compile-time on inline
values and not types.
refined-scalacheck
. I am trying to produce an Arbitrary instance for String Refined Size[Less[20]]
but it will not always succeed. I am puzzled by this because it can make one for Int Refined Less[20]
so supposedly collection.buildableSizeArbitrary
could use that instance to generate valid collection sizes, but I often get negative size values. Am I using the wrong implicits, or is this a bug in the library? import eu.timepit.refined.api.Refined
import eu.timepit.refined.collection.Size
import eu.timepit.refined.numeric._
import eu.timepit.refined.scalacheck.all.lessArbitrary
import eu.timepit.refined.scalacheck.string.stringSizeArbitrary
import org.scalacheck.Gen
object Example extends App {
type T1 = String Refined Size[Less[5]]
case class CC(value: T1)
println(implicitly(Gen.resultOf(CC.apply _)).sample)
}
Hi,
I'm using refined to refine a float value that I'm getting from db. I have a similar usage for int that works (tm)... But struggling with Integral[Float]
implicit not in scope.
Context
type ValidValue = Float Refined Divisible[0.25]
case class MyType(value: ValidValue)
implicit val myTypeGet: Get[MyType] = Get[Float].temap { raw =>
refineV[Divisible[0.25]](raw).map(MyType(_))
}
raises error on compile
could not find implicit value for parameter v: eu.timepit.refined.api.Validate[Float, Divisible[0.25]]
refineV[Divisible[0.25]](raw).map(MyType(_))
any pointers ?