Created
December 15, 2022 17:12
-
-
Save mrdziuban/823013e7ec0cc51ab608d4c0287cdb2c to your computer and use it in GitHub Desktop.
autoGiven.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.MacroAnnotation | |
import scala.quoted.* | |
class autoGiven extends MacroAnnotation { | |
final def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = { | |
import quotes.reflect.* | |
tree match { | |
case ValDef(name, tpt, Some(rhs)) => rhs.asExpr match { | |
case '{ $rhsExpr: t } => | |
val treeSym = tree.symbol | |
val givenSym = Symbol.newVal(treeSym.owner, name ++ "Given", TypeRepr.of[t], Flags.Given | treeSym.flags, Symbol.noSymbol) | |
val givenVal = ValDef(givenSym, Some(Ref(treeSym))) | |
List(tree, givenVal) | |
case _ => | |
report.error("Annotation only supported on `val`") | |
List(tree) | |
} | |
case _ => | |
report.error("Annotation only supported on `val`") | |
List(tree) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment