Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment: refine macro in Scala 3 #921

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft

Conversation

fthomas
Copy link
Owner

@fthomas fthomas commented Mar 7, 2021

This works with inline type classes and conditions that can be evaluated
at compile-time like >, !, and ==:

scala> refineMV[Int, Positive1](5)
val res1: eu.timepit.refined.api.Refined[Int, eu.timepit.refined.Positive1] = 5

scala> refineMV[Int, Positive1](-5)
1 |refineMV[Int, Positive1](-5)
  |^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |no

scala> refineMV[String, NonEmpty1]("hello")
val res2: eu.timepit.refined.api.Refined[String, eu.timepit.refined.NonEmpty1] = hello

scala> refineMV[String, NonEmpty1]("")
1 |refineMV[String, NonEmpty1]("")
  |^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |no

This works with inline type classes and conditions that can be evaluated
at compile-time like `>`, `!`, and `==`:
```scala
scala> refineMV[Int, Positive1](5)
val res1: eu.timepit.refined.api.Refined[Int, eu.timepit.refined.Positive1] = 5

scala> refineMV[Int, Positive1](-5)
1 |refineMV[Int, Positive1](-5)
  |^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |no

scala> refineMV[String, NonEmpty1]("hello")
val res2: eu.timepit.refined.api.Refined[String, eu.timepit.refined.NonEmpty1] = hello

scala> refineMV[String, NonEmpty1]("")
1 |refineMV[String, NonEmpty1]("")
  |^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |no
```
@codecov
Copy link

codecov bot commented Mar 7, 2021

Codecov Report

Merging #921 (b3ed76d) into master (98aff1d) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #921   +/-   ##
=======================================
  Coverage   92.39%   92.39%           
=======================================
  Files          63       63           
  Lines         815      815           
  Branches        6        6           
=======================================
  Hits          753      753           
  Misses         62       62           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 98aff1d...b3ed76d. Read the comment docs.

@fthomas fthomas marked this pull request as draft March 8, 2021 06:48
@@ -14,6 +14,48 @@ package object refined {
*/
def refineV[P]: RefinePartiallyApplied[Refined, P] = RefType.refinedRefType.refine[P]

trait Predicate[T, P] {
inline def isValid(inline t: T): Boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm giving this a try in armanbilge/lucuma-core#1. Interestingly I had to add transparent to make it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants