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

Draft of macro in Scala 3 #959

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions modules/core/shared/src/main/scala-3.0+/eu/example/Example.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package eu.example

object Example {
import eu.timepit.refined.numeric._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto._
import eu.timepit.refined._
import eu.timepit.refined.api._
import eu.timepit.refined.auto._

@main def main(): Unit =
val x: Int Refined Greater[5] = autoRefineV[Greater[5]](6)
// does not compile
// val x2: Int Refined Greater[5] = autoRefineV[Greater[5]](5)
println("hello")
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package eu.timepit.refined

import eu.timepit.refined.api.RefType
import eu.timepit.refined.api.{RefType, Refined, Validate}
import eu.timepit.refined.internal.WitnessAs

import scala.quoted.{Expr, Quotes, ToExpr, Type, FromExpr}
import scala.compiletime.error
import scala.compiletime.{constValue, erasedValue}

/**
* Module that provides automatic refinements and automatic conversions
Expand Down Expand Up @@ -30,4 +35,35 @@ object auto {
*/
implicit def autoUnwrap[F[_, _], T, P](tp: F[T, P])(implicit rt: RefType[F]): T =
rt.unwrap(tp)

inline def autoRefineV[P](inline t: Int)(implicit validate: Validate[Int, P]): Refined[Int, P] =
${ autoRefineVCode[P]('t, 'validate) }

private def autoRefineVCode[P : Type](t: Expr[Int], validate: Expr[Validate[Int, P]])(using q: Quotes): Expr[Refined[Int, P]] =
val tValue = t.valueOrError
Type.of[P] match
case '[ eu.timepit.refined.numeric.Greater[x] ] =>
val tpe = q.reflect.TypeTree.of[P]

val validateInstance = tpe.tpe match
case appliedType: q.reflect.AppliedType =>
appliedType.args.headOption match
case Some(ct: q.reflect.ConstantType) =>
ct.constant.value match
case limit: Int =>
numeric.Greater.greaterValidate(WitnessAs.apply(limit, limit), summon[Numeric[Int]])
case e =>
q.reflect.report.throwError(s"cannot match: $e")
case e =>
q.reflect.report.throwError(s"cannot match: $e")
case a => q.reflect.report.throwError(s"not here:: $a")

val res = validateInstance.validate(tValue)
if (res.isFailed) {
val msg = validateInstance.showResult(tValue, res)
q.reflect.report.throwError(msg)
}
'{ RefType[Refined].unsafeWrap[Int, P]($t) }
case _ =>
q.reflect.report.throwError("wrong: " + Type.show[P])
}