Skip to content

Add Symbol.defaultParams and Expr.mapOf* family of methods #9236

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

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class CommunityBuildTest:
@Test def sourcecode = projects.sourcecode.run()
@Test def oslib = projects.oslib.run()
@Test def ujson = projects.ujson.run()
@Test def upickle = projects.upickle.run()
// @Test def upickle = projects.upickle.run()
// @Test def oslibWatch = projects.oslibWatch.run()
@Test def geny = projects.geny.run()
@Test def stdLib213 = projects.stdLib213.run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,22 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
case sym if sym.is(Flags.CaseAccessor) => sym.asTerm
}

def Symbol_defaultParams(self: Symbol)(using ctx: Context): List[(String, Symbol)] =
assert(self.isClass && self.flags.is(Flags.Case))
val comp: Symbol = self.companionClass
val names: List[String] =
for p <- Symbol_caseFields(self) if p.flags.is(Flags.HasDefault)
yield Symbol_name(p)

val body = ClassDef_body(Symbol_tree(comp).asInstanceOf[ClassDef])
val idents: List[Symbol] =
for case deff @ DefDef(name, Nil, Nil, _, _) <- body
if name.toString.startsWith("$lessinit$greater$default")
yield deff.symbol
assert(names.size == idents.size)
names.zip(idents)
end Symbol_defaultParams

def Symbol_children(self: Symbol)(using ctx: Context): List[Symbol] =
self.children

Expand Down
8 changes: 8 additions & 0 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2284,6 +2284,14 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
def caseFields(using ctx: Context): List[Symbol] =
internal.Symbol_caseFields(sym)

/** Default parameters of a case class. The first elements of the pairs are names of
* the parameters and values – symbols of the definitions sites of the default values.
* Implementation restriction: only the default parameters in the first parameter group
* are returned.
*/
def defaultParams(using ctx: Context): List[(String, Symbol)] =
internal.Symbol_defaultParams(sym)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to move the implementation here. There is no reason to add it to the CompilerInterface if it can be computed using it.


def isTypeParam(using ctx: Context): Boolean =
internal.Symbol_isTypeParam(sym)

Expand Down
7 changes: 7 additions & 0 deletions library/src/scala/tasty/reflect/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,13 @@ trait CompilerInterface {
/** Fields of a case class type -- only the ones declared in primary constructor */
def Symbol_caseFields(self: Symbol)(using ctx: Context): List[Symbol]

/** Default parameters of a case class. The first elements of the pairs are names of
* the parameters and values – symbols of the definitions sites of the default values.
* Implementation restriction: only the default parameters in the first parameter group
* are returned.
*/
def Symbol_defaultParams(self: Symbol)(using ctx: Context): List[(String, Symbol)]

def Symbol_of(fullName: String)(using ctx: Context): Symbol

def Symbol_newMethod(parent: Symbol, name: String, flags: Flags, tpe: Type, privateWithin: Symbol)(using ctx: Context): Symbol
Expand Down
1 change: 1 addition & 0 deletions tests/run-macros/reflect-defaultParams.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
List((address,Home), (age,1))
14 changes: 14 additions & 0 deletions tests/run-macros/reflect-defaultParams/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import scala.quoted._

inline def defaultParams[T]: List[(String, Any)] = ${ defaultParamsImpl[T] }
private def defaultParamsImpl[T](
using tpe: Type[T], qctx: QuoteContext): Expr[List[(String, Any)]] =
import qctx.tasty._
val sym = tpe.unseal.symbol
val defaultParams = sym.defaultParams
val values: List[Expr[Any]] =
defaultParams.map { case (k, v) => Ref(v).seal }
val names: List[Expr[String]] =
defaultParams.map { case (k, v) => Expr(k) }
'{ ${ Expr.ofList(names) }.zip(${ Expr.ofList(values) }) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't zip in the next stage and use the Expr.ofList constructor.

Suggested change
'{ ${ Expr.ofList(names) }.zip(${ Expr.ofList(values) }) }
Expr.ofList(names.zip(values).map { case (n, v) => '{ ($n, $v) } })

end defaultParamsImpl
4 changes: 4 additions & 0 deletions tests/run-macros/reflect-defaultParams/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
case class Cat(name: String, address: String = "Home", age: Int = 1)(a: Int = age, b: String = address + age)

@main def Test =
println(defaultParams[Cat])