Skip to content

Commit

Permalink
Strip annotations from types
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperFKorban committed Mar 19, 2024
1 parent 8ad9a64 commit f41e91d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 9 additions & 3 deletions guinep/src/main/scala/macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ private[guinep] object macros {
private def prettyName: String =
s.name.stripSuffix("$")

extension (tpe: TypeRepr)
private def stripAnnots: TypeRepr = tpe match {
case AnnotatedType(tpe, _) => tpe.stripAnnots
case _ => tpe
}

private def functionNameImpl(f: Expr[Any]): Expr[String] = {
val name = f.asTerm match {
case Inlined(_, _, Lambda(_, body)) =>
Expand Down Expand Up @@ -100,13 +106,13 @@ private[guinep] object macros {
fields.map { valdef =>
functionFormElementFromTree(
valdef.name,
valdef.tpt.tpe.substituteTypes(typeDefParams, ntpe.typeArgs)
valdef.tpt.tpe.substituteTypes(typeDefParams, ntpe.typeArgs).stripAnnots
)
}
)
case ntpe if isSumTpe(ntpe) =>
val classSymbol = ntpe.typeSymbol
val childrenAppliedTpes = classSymbol.children.map(child => appliedChild(child, classSymbol, ntpe.typeArgs))
val childrenAppliedTpes = classSymbol.children.map(child => appliedChild(child, classSymbol, ntpe.typeArgs)).map(_.stripAnnots)
val childrenFormElements = childrenAppliedTpes.map(t => functionFormElementFromTree("value", t))
val options = classSymbol.children.map(_.prettyName).zip(childrenFormElements)
FormElement.Dropdown(paramName, options)
Expand Down Expand Up @@ -170,7 +176,7 @@ private[guinep] object macros {
val classSymbol = ntpe.typeSymbol
val className = classSymbol.name
val children = classSymbol.children
val childrenAppliedTpes = children.map(child => appliedChild(child, classSymbol, ntpe.typeArgs))
val childrenAppliedTpes = children.map(child => appliedChild(child, classSymbol, ntpe.typeArgs)).map(_.stripAnnots)
val paramMap = '{ ${param.asExpr}.asInstanceOf[Map[String, Any]] }.asTerm
val paramName = paramMap.select("apply").appliedTo(Literal(StringConstant("name")))
val paramValue = paramMap.select("apply").appliedTo(Literal(StringConstant("value")))
Expand Down
6 changes: 6 additions & 0 deletions testcases/src/main/scala/main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ case class IntValue(value: Int) extends WeirdGADT[Int]
case class SomeValue[+A](value: A) extends WeirdGADT[A]
case class SomeOtherValue[+A, +B](value: A, value2: B) extends WeirdGADT[A]

// This fails on unknown type params
def printsWeirdGADT(g: WeirdGADT[String]): String = g match
case SomeValue(value) => s"SomeValue($value)"
case SomeOtherValue(value, value2) => s"SomeOtherValue($value, $value2)"

// This loops forever
def concatAll(elems: List[String]): String =
elems.mkString

@main
def run: Unit =
guinep.web(
Expand All @@ -82,4 +87,5 @@ def run: Unit =
roll20,
roll6(),
// printsWeirdGADT
// concatAll
)

0 comments on commit f41e91d

Please sign in to comment.