Hi all!
I've learned BigDecimal support in fthomas/refined#345 & fthomas/refined#36
But I am unable to create RefinedTypeOps for BigDecimal with Closed predicate.
I would like to achieve something like
type Percent = Refined[BigDecimal, Closed[W.`0.0`.T, W.`100.0`.T]]
object Percent extends RefinedTypeOps[Percent, BigDecimal]
// What I've tried:
type Percent = Refined[BigDecimal, Closed[W.`BigDecimal(0.0)`.T, W.`BigDecimal(100.0)`.T]]
object Percent extends RefinedTypeOps[Percent, BigDecimal]
type Percent = Refined[BigDecimal, Closed[W.`BigDecimal("0.0")`.T, W.`BigDecimal("100.0")`.T]]
object Percent extends RefinedTypeOps[Percent, BigDecimal]
could not find implicit value for parameter rt: eu.timepit.refined.api.RefinedType.AuxT[com.xxx.Percent,BigDecimal]
[error] object Percent extends RefinedTypeOps[Percent, BigDecimal]
Hi there 🙋♂️
I wonder if it is possible to check that one int is less than or equal to another int.
Using refined + literal-based singleton types from SIP-23
Something like that:
trait <=[MIN, MAX] {} // Should be automagically derived during compilation
// Only if MIN <= MAX
class GenericStringValidator[MIN: ValueOf, MAX: ValueOf](implicit ev: MIN <= MAX) {
private val minValue = implicitly[ValueOf[MIN]].value
private val maxValue = implicitly[ValueOf[MAX]].value
}
P.S.: That piece of code in scastie
@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.