Skip to content

Instantly share code, notes, and snippets.

@mrdziuban
Created December 15, 2022 17:12
Show Gist options
  • Save mrdziuban/823013e7ec0cc51ab608d4c0287cdb2c to your computer and use it in GitHub Desktop.
Save mrdziuban/823013e7ec0cc51ab608d4c0287cdb2c to your computer and use it in GitHub Desktop.
autoGiven.scala
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