Skip to content

Commit

Permalink
still doesn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
smarter committed Aug 15, 2024
1 parent 7e37628 commit c2f51e6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
9 changes: 5 additions & 4 deletions tests/run-macros/val1/A_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ object Macros:
// transparent inline implicit def myMacro[T >: Inv[S], S](x: T): S = ${Macros2.myMacroImpl[T, S]('x)}


// def myMacroImpl[T: Type, S <: T @checkBase : Type](x: Expr[T])(using Quotes): Expr[S] =
transparent inline def myMacro[T, S <: T](x: T): Inv[S] = ${Macros2.myMacroImpl[T, S]('x)}

object Macros2:
def myMacroImpl[T: Type, S <: T : Type](x: Expr[T])(using Quotes): Expr[Macros.Inv[S]] =
import quotes.reflect.*
Expand All @@ -29,11 +30,11 @@ object Macros2:
// failsafe: give up if S isn't of the form X ::= RT[X @check] | T
// need other conv to handle List[T] to List[X]
// need to deal with invariance: Array[PosInt] vs Array[Int]
println(TypeRepr.of[S].show)
println(TypeRepr.of[S])//.show)
'{ $x.asInstanceOf[Macros.Inv[S]] }
// '{ $x.asInstanceOf[S] }

def valImpl[T: Type, S <: T : Type](using Quotes): Expr[Validator[T, Macros.Inv[S]]] =
def valImpl[T: Type, S /*<: T*/ : Type](using Quotes): Expr[Validator[T, Macros.Inv[S]]] =
import quotes.reflect.*
// we might have multiple layers of checks.
// TODO: use a plugin to strip annotations after typer to avoid compiler crash without Matthieu's branch?
Expand Down Expand Up @@ -75,7 +76,7 @@ object Validator:
val identitySingleton: Validator[Any, Any] = new:
extension (f: Any) def validate(): Any = f

transparent inline given [T, S <: T]: Validator[T, Inv[S]] = ${Macros2.valImpl[T, S]}
transparent inline given [T, S/* <: T*/]: Validator[T, Inv[S]] = ${Macros2.valImpl[T, S]}

import scala.reflect.{classTag, ClassTag}
given [T: ClassTag, S <: T](using Validator[T, S]): Validator[Array[T], Array[S]] with
Expand Down
17 changes: 13 additions & 4 deletions tests/run-macros/val1/B_2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@ package b {

class B(a: Array[PosInt])

class C(aa: Int, bb: Inv[Int @check[Int](x => x > aa)])
// class C(aa: Int, bb: Inv[Int @check[Int](x => x > aa)])

def dep(aa: Int)(bb: Inv[Int @check[Int](x => x > aa)]) = 0
def dep(aa: Int)(bb: Inv[Int @check[Int](x => x > aa )]) = 0

// @check[x.type > 0]
// @check[Log2[x.type + y.type] == 0]

// @range[0, 1000]

// @

// class A(a: Int @check[Int](x => Box(3).x == 3))
}

object Test:
import b.*
import ValidatorConv.given
// import ValidatorConv.given
// transparent inline implicit def conv[T, S <: T @a.checkBase](x: T): S = a.Macros.myMacro[T, S](x)
// transparent inline implicit def conv[T, S <: T](x: T): S = a.Macros.myMacro[T, S](x)
// def conv[T, S <: T](x: T): Macros.Inv[S] = ???
def main(args: Array[String]): Unit =
// import a.Macros.myMacro
// new a.A(a.Macros.myMacro(1): Macros.Inv[Int @check[Int](x => Box(3).x == 3)])
Expand All @@ -33,4 +41,5 @@ object Test:
// val x: Array[Int] = Array(1)
// new B(x)
// new C(42, 1)
dep(42)(1)
// dep(42)(conv(1))
dep(42)(Macros.myMacro(1))

0 comments on commit c2f51e6

Please sign in to comment.