Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 05:44
    nhantran starred fthomas/refined
  • Apr 12 17:29
    fthomas updated the wiki
  • Apr 12 17:25

    fthomas on master

    Add contentsquare as adopter Merge pull request #943 from Be… (compare)

  • Apr 12 17:25
    fthomas closed #943
  • Apr 12 17:24
    fthomas commented #943
  • Apr 12 13:08
    codecov[bot] commented #943
  • Apr 12 13:07
    codecov[bot] commented #943
  • Apr 12 13:02
    Bertrand31 opened #943
  • Apr 12 12:19
    fthomas commented #941
  • Apr 12 12:16
    jonathanfrawley commented #941
  • Apr 12 05:58
    fthomas commented #941
  • Apr 09 19:42
    fthomas closed #168
  • Apr 09 19:42
    fthomas closed #169
  • Apr 09 19:40
    fthomas closed #308
  • Apr 09 19:40
    fthomas commented #308
  • Apr 09 19:40
    fthomas closed #341
  • Apr 09 19:40
    fthomas commented #341
  • Apr 09 19:40
    fthomas closed #389
  • Apr 09 19:40
    fthomas commented #389
  • Apr 09 19:39
    fthomas closed #731
Jens Grassel
@jan0sch
However you'll need to construct our instances via Map or write an object ValidMap with the appropriate helper functions.
David Strawn
@isomarcte
Is there current work underway for supporting something analogous to RefineMacro in Scala 3.x.x?
fthomas
@fthomas:matrix.org
[m]
@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
@DenisNovac

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
@Zhen-hao
hi, is there a version of applyRef that gives a ValidatedNel instead of Either
Zhenhao Li
@Zhen-hao
so for type A = String Refined P1 AND P2 applyRef will give Invalid(NonEmptyList(errorOnP1, errorOnP2)) instead of Left(combinedError)
Damien Favre
@benderpremier
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 S. Thomas
@fthomas
@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
@Zhen-hao
thanks! I will give it a try when I get more time
Damien Favre
@benderpremier
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
@benderpremier
for the moment I ended up using unsafeFrom(_ % 3) but I assume kind of an anti pattern
Piyush Purang
@ppurang
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
@GrafBlutwurst
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
@GrafBlutwurst
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
@bpholt

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
@bpholt
Is it possible to use capture groups in a MatchesRegex predicate, and then extract the captured value(s) somehow?
Aleksei Shamenev
@coffius

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
@coffius
Alan Devlin
@spf3000
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
@soronpo
@spf3000 have you tried adding a dummy implicit argument?
def getTrees(a: PlantID)(implicit di : DummyImplicit): List[Tree]
Alan Devlin
@spf3000
@soronpo I didn't know about that trick - it's very neat - thanks :)
Jakub Kozłowski
@kubukoz:matrix.org
[m]
hey, are there plans to support refined macros in dotty?
Frank S. Thomas
@fthomas
@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
Jakub Kozłowski
@kubukoz:matrix.org
[m]
I see
Frank S. Thomas
@fthomas
I experimented a little bit with implementing the refine macro in Scala 3 here: fthomas/refined#921 . It seems to be possible for predicates that are implemented in terms of functions like >, !, ==, etc. that can be evaluated at compile-time but it would break down for more complex predicates like MatchesRegex, Size, etc.
Oron Port
@soronpo
@fthomas did you try using compiletime.ops?
And I also think there is more to add there by PR-ing to dotty with what is required.
Both for refined and singleton-ops.
I haven't had a chance to look at this yet, but once I finish my Phd, I will get on that.
Hopefully during May
Frank S. Thomas
@fthomas
For that experiment I didn't looked at compiletime.ops since I was only looking at what functions can be called at compile-time on inline values and not types.
Oron Port
@soronpo
But inline values constraint can rely on type constraints
Endre Galaczi
@chiller
Hello, I have a question about 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)
}
Frank S. Thomas
@fthomas
@chiller If the Arbitrary for Int Refined Less[5] generates a negative value, Gen.buildableOfN cannot generate a value. Try using a predicate that excludes negative values, e.g. String Refined Size[Interval.Closed[0, 5]].
Endre Galaczi
@chiller
Thank you
Pelle Krøgholt
@pellekrogholt

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 ?

Frank S. Thomas
@fthomas
Try using a Float literal: 0.25F
Pelle Krøgholt
@pellekrogholt
Thx for your pointer - I tried with ^^ same error :thought_balloon:
Pelle Krøgholt
@pellekrogholt

@fthomas / others - I still struggle with floats and refined.

To make my struggle more complete I have left out the doobie part

import eu.timepit.refined.api.Refined
import eu.timepit.refined.numeric.Divisible
import eu.timepit.refined.{refineMV}

object RefinedFloatApp extends App {

  type TRefinement = Divisible[0.25f]
  type ValidT      = Float Refined TRefinement

  case class MyType(value: ValidT)

  val myType = MyType(refineMV[TRefinement](0.5f))

  println(s"myType: $myType")

}

raises:

could not find implicit value for parameter v: eu.timepit.refined.api.Validate[Float,RefinedFloatApp.TRefinement]
  val myType = MyType(refineMV[TRefinement](0.f))

when I hover the implicit part through Intellij ide it says:

No implicits found for parameter it: Integral[Float]

and I'm not sure how to get that in scope ?

Frank S. Thomas
@fthomas
@pellekrogholt It seems that there is just no Integral[Float] instance in Scala's std library
Pelle Krøgholt
@pellekrogholt
@fthomas thx for your feedback . I'm a bit lost here because when I walk down the refined Modulo object I get into scala Integral https://www.scala-lang.org/api/current/scala/math/Integral.html - so naive ? do you know how to get Integral in scope ?
Frank S. Thomas
@fthomas
@pellekrogholt If it would be defined in the std library, it would be somewhere around here https://github.com/scala/scala/blob/v2.13.5/src/library/scala/math/Numeric.scala#L141. But since the instance does not exists, you have to define it yourself (if that is possible at all).
Pelle Krøgholt
@pellekrogholt
Oki thx for the explanation/pointers ...
javierg1975
@javierg1975

Hi all,
Very new to Refined. So far only trying to run a simple example, but something as simple as this

import eu.timepit.refined.api.Refined
import eu.timepit.refined.string.MatchesRegex
import eu.timepit.refined._

  type Name = String Refined MatchesRegex[W.`"""[A-Z][a-z]+"""`.T]
  case class User(name: Name)

  val daniel = User("Tom")

Will still fail with

error: type mismatch;
        found   : String("Tom")
        required: Name
           (which expands to)  eu.timepit.refined.api.Refined[String,eu.timepit.refined.string.MatchesRegex[String("[A-Z][a-z]+")]]

From the examples I've seen online this should run, but I'm obviously missing something. Maybe an import?
Any hints will be greatly appreciated

Frank S. Thomas
@fthomas
@javierg1975 Yes, an import is missing. It should work with import eu.timepit.refined.auto._.
1 reply