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

Fix defaultParams macro for generic class #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Global / idePackagePrefix := Some("dummy")

def DottyProject(name: String): Project =
Project.apply(name, file(name)).settings(
scalaVersion := "3.1.0",
scalaVersion := "3.1.3",
Compile / scalaSource := baseDirectory.value / "src",
)

Expand Down
2 changes: 1 addition & 1 deletion build.sc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mill._, scalalib._

trait DottyModule extends ScalaModule {
def scalaVersion = "3.1.0"
def scalaVersion = "3.1.3"
def scalacOptions = Seq("-Xcheck-macros")
}

Expand Down
6 changes: 3 additions & 3 deletions defaultParamsInference/src/Test.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package dummy

case class Person(name: String, address: String = "Zuricch", foo: Int, age: Int = 26)
case class Person[T](name: String, address: String = "Zuricch", foo: Int, age: Int = 26, bar: List[T] = Nil)

object Person:
val x = 10

@main def test(): Unit =
val p1 = Person("John", foo = 10)
println(p1)
println(defaultParams[Person])
assert(defaultParams[Person] == Map("address" -> "Zuricch", "age" -> 26))
println(defaultParams[Person[Double]])
assert(defaultParams[Person[Double]] == Map("address" -> "Zuricch", "age" -> 26, "bar" -> Nil))
10 changes: 7 additions & 3 deletions defaultParamsInference/src/macro.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package dummy

import scala.annotation.experimental
import scala.quoted.*

inline def defaultParams[T]: Map[String, Any] = ${ defaultParmasImpl[T] }

@experimental // because .typeArgs is @experimental
def defaultParmasImpl[T](using quotes: Quotes, tpe: Type[T]): Expr[Map[String, Any]] =
import quotes.reflect.*
val sym = TypeTree.of[T].symbol
val typ = TypeRepr.of[T]
val sym = typ.typeSymbol
val typeArgs = typ.typeArgs
val comp = sym.companionClass
val mod = Ref(sym.companionModule)
val names =
Expand All @@ -16,10 +20,10 @@ def defaultParmasImpl[T](using quotes: Quotes, tpe: Type[T]): Expr[Map[String, A
Expr.ofList(names.map(Expr(_)))

val body = comp.tree.asInstanceOf[ClassDef].body
val idents: List[Ref] =
val idents: List[Term] =
for case deff @ DefDef(name, _, _, _) <- body
if name.startsWith("$lessinit$greater$default")
yield mod.select(deff.symbol)
yield mod.select(deff.symbol).appliedToTypes(typeArgs)
val identsExpr: Expr[List[Any]] =
Expr.ofList(idents.map(_.asExpr))

Expand Down