Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
  • May 27 13:36
    gosterx starred fthomas/refined
  • May 26 21:25
    DeamonDev starred fthomas/refined
  • May 23 10:17
    scala-impala starred fthomas/refined
  • May 22 04:03
    adampauls opened #1090
  • May 19 15:41
    jackyevo938 starred fthomas/refined
  • May 19 10:18
    jelmerk starred fthomas/refined
  • May 18 07:38
    kylefeng starred fthomas/refined
  • May 18 05:54
    daofaziran1 starred fthomas/refined
  • May 17 22:48
    armanbilge commented #932
  • May 17 22:08
    note commented #932
  • May 17 22:08
    note commented #932
  • May 17 22:07
    note commented #932
  • May 17 22:05
    note commented #932
  • May 17 22:02
    note commented #932
  • May 17 17:40
    adamthompson93 starred fthomas/refined
  • May 17 02:38
    Kensuke-Hinata starred fthomas/refined
  • May 17 02:36
    uazw starred fthomas/refined
  • May 16 15:15
    armanbilge commented #932
  • May 16 14:47
    gcnyin starred fthomas/refined
  • May 16 13:30
    armanbilge commented #932
Nicolas Francois
Apparently it could be implemented like this in RefinedTypeOps
class RefinedTypeOps[FTP, T](implicit rt: RefinedType.AuxT[FTP, T]) extends Serializable {

  def apply[F[_, _], P](t: T)(implicit
      ev: F[T, P] =:= FTP,
      rt: RefType[F],
      v: Validate[T, P]
  ): FTP =
    macro RefineMacro.implApplyRef[FTP, F, T, P]

  def from(t: T): Either[String, FTP] =

  def unapply(t: T): Option[FTP] =

  def unsafeFrom(t: T): FTP =

  def deriveOrdering(implicit ordering: Ordering[T]): Ordering[FTP] = Ordering
    .fromLessThan[FTP]((x, y) =>
      ordering.lt(rt.refType.unwrap(rt.dealias(x)), rt.refType.unwrap(rt.dealias(y)))
Maybe should I directly open a pull request ?
Raúl Piaggio
hi there, I'm just starting using the lib. I see similar questions asked before but I can't find the answer. How can I get an AuxT[String Refined UpperCase, String] so that I can define RefinedTypeOps[String Refined UpperCase, String] ?
can it be derived or do I have to define it manually?
Frank Thomas
@rpiaggio UpperCase is a predicate for Char. If you want a String that only consists of upper-case letters, use String Refined Forall[UpperCase]. The required implicits for RefinedTypeOps can then be derived automatically
Raúl Piaggio
@fthomas Thank you very much. Thank explains it and it worked like a charm. However, now I can't get it to work for a NonEmptyString Refined Forall[UpperCase]
ok, it seems to work with type UpperNEString = String Refined And[NonEmpty, Forall[UpperCase]]. Thank you very much!

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]
And, of course, I am getting
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]
Leif Warner
Anyone have an opinion on which would be better (lower runtime cost, hopefully) between refined Size vs shapeless Sized?
Just thinking to assert e.g. a byte array has 8 elements (so I know it can represent a Long).
Dmytro Mitin
@LeifW I answered in shapeless.
Aleksei Shamenev

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

Aleksei Shamenev
Accidently scrolled up to the message and found the answer. Thanks, chat 😊
Oron Port
@coffius just a tip. From my experience it's better to have classes unconstrained with private construcor and constrained with a public definition in a companion.
Aleksei Shamenev
@soronpo, Hmm... Interesting idea. But maybe you can elaborate it a little bit more. I am not sure if I understand it good enough.
Oron Port

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

Aleksei Shamenev
@soronpo, Ok, now I got it. No further questions. Thanks 🙇‍♂️
@jarek_rozanski:matrix.org I was wondering if it possible to apply Refined types to key and value of a Map
Jarek Rozanski
Apologies for clunky Matrix bot earlier. In essence I am trying to apply Refined to a generic Map, and defining refined type to Keys and Values. Imagine something along the lines of:
type ValidMap = Map[String Refined NonEmpty, Long Refined Closed[W.`0`.T, W.`100`,T]
Jens Grassel
@jrozanski It should work, I've something like this:
type FormField = String Refined (Trimmed And NonEmpty)
type ErrorMessage = String Refined NonEmpty
type FormErrors = Map[FormField, List[ErrorMessage]]
However you'll need to construct our instances via Map or write an object ValidMap with the appropriate helper functions.
David Strawn
Is there current work underway for supporting something analogous to RefineMacro in Scala 3.x.x?
@isomarcte: I'm not working on a 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.
Denis Novac

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.

Zhenhao Li
hi, is there a version of applyRef that gives a ValidatedNel instead of Either
Zhenhao Li
so for type A = String Refined P1 AND P2 applyRef will give Invalid(NonEmptyList(errorOnP1, errorOnP2)) instead of Left(combinedError)
Damien Favre
Hi, I was wondering if someone had already developed something around typeclass derivation for refined types. e.g. given I have an Ordering[Int] in scope derive automatically an Ordering[Int Refined Positive] ?
Frank Thomas
@Zhen-hao internally refined creates a 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.
Zhenhao Li
thanks! I will give it a try when I get more time
Damien Favre
Say I have positive integers on which I map _ % 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 ?
Damien Favre
for the moment I ended up using unsafeFrom(_ % 3) but I assume kind of an anti pattern
Piyush Purang
A very general question here - does any one know the limitations of refined under scala 3? Seems like val u : NonEmptyString = "u" doesn't work as intended and in this case what would be an okish work around?
[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
^that import is enabled
And implicit conversions in eu.timepit.refined.auto are macro-based.
Dominic Egger
Hey. I was wondering if it's possible to summon a runtime representation of a Predicate namely for logging puproses, I can't log the values because of sensitivity concerns but want to log the predicate it fails. But I can't find anything in the code
Dominic Egger
Right now I just have my own encoding using refineDescribed[V, P](implicit validate:Validate[V,P], description: DescribeRefinement[P]): Either[RefinementError[P], V Refined P] but I want to avoid reinventing the wheel
Brian P. Holt

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)

Brian P. Holt
Is it possible to use capture groups in a MatchesRegex predicate, and then extract the captured value(s) somehow?
Aleksei Shamenev

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.

Aleksei Shamenev
Alan Devlin
Hi everyone? Wondering if there is an idiomatic solution for the double definition error with refined types as parameters?
` ``
def getTrees(a: TreeID): List[Tree]
def getTrees(a: PlantID): List[Tree]
double definition:
have same type after erasure: (a: String):
Oron Port
@spf3000 have you tried adding a dummy implicit argument?
def getTrees(a: PlantID)(implicit di : DummyImplicit): List[Tree]
Alan Devlin
@soronpo I didn't know about that trick - it's very neat - thanks :)
Jakub Kozłowski
hey, are there plans to support refined macros in dotty?
Frank Thomas
@kubukoz:matrix.org I'm not sure yet if they can be ported to Scala 3. The Scala 2 macros use eval which is not available in Scala 3. Maybe if all Validate instances are declared as inline, it would be possible to do the validations at compile-time