Skip to content

Commit d991b85

Browse files
defaultParams returns a List of pairs
Update tests
1 parent 3c3a06d commit d991b85

File tree

6 files changed

+19
-18
lines changed

6 files changed

+19
-18
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
17791779
case sym if sym.is(Flags.CaseAccessor) => sym.asTerm
17801780
}
17811781

1782-
def Symbol_defaultParams(self: Symbol)(using ctx: Context): Map[String, Symbol] =
1782+
def Symbol_defaultParams(self: Symbol)(using ctx: Context): List[(String, Symbol)] =
17831783
assert(self.isClass && self.flags.is(Flags.Case))
17841784
val comp: Symbol = self.companionClass
17851785
val names: List[String] =
@@ -1792,7 +1792,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
17921792
if name.toString.startsWith("$lessinit$greater$default")
17931793
yield deff.symbol
17941794
assert(names.size == idents.size)
1795-
names.zip(idents).toMap
1795+
names.zip(idents)
17961796
end Symbol_defaultParams
17971797

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

library/src/scala/tasty/Reflection.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,12 +2284,12 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
22842284
def caseFields(using ctx: Context): List[Symbol] =
22852285
internal.Symbol_caseFields(sym)
22862286

2287-
/** Default parameters of a case class. Keys are names of the parameters and values –
2288-
* symbols of the definitions sites of the default values.
2287+
/** Default parameters of a case class. The first elements of the pairs are names of
2288+
* the parameters and values – symbols of the definitions sites of the default values.
22892289
* Implementation restriction: only the default parameters in the first parameter group
22902290
* are returned.
22912291
*/
2292-
def defaultParams(using ctx: Context): Map[String, Symbol] =
2292+
def defaultParams(using ctx: Context): List[(String, Symbol)] =
22932293
internal.Symbol_defaultParams(sym)
22942294

22952295
def isTypeParam(using ctx: Context): Boolean =

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,12 +1332,12 @@ trait CompilerInterface {
13321332
/** Fields of a case class type -- only the ones declared in primary constructor */
13331333
def Symbol_caseFields(self: Symbol)(using ctx: Context): List[Symbol]
13341334

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Map(address -> Home, age -> 1)
1+
List((address,Home), (age,1))
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import scala.quoted._
22

3-
inline def defaultParams[T]: Map[String, Any] = ${ defaultParamsImpl[T] }
3+
inline def defaultParams[T]: List[(String, Any)] = ${ defaultParamsImpl[T] }
44
private def defaultParamsImpl[T](
5-
using tpe: Type[T], qctx: QuoteContext): Expr[Map[String, Any]] =
5+
using tpe: Type[T], qctx: QuoteContext): Expr[List[(String, Any)]] =
66
import qctx.tasty._
77
val sym = tpe.unseal.symbol
8-
val exprs: Map[String, Expr[Any]] =
9-
sym.defaultParams.view.mapValues(sym =>
10-
Ref(sym).seal.cast[Any]).toMap
11-
Expr.ofMapValues(exprs)
8+
val defaultParams = sym.defaultParams
9+
val values: List[Expr[Any]] =
10+
defaultParams.map { case (k, v) => Ref(v).seal }
11+
val names: List[Expr[String]] =
12+
defaultParams.map { case (k, v) => Expr(k) }
13+
'{ ${ Expr.ofList(names) }.zip(${ Expr.ofList(values) }) }
1214
end defaultParamsImpl
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
case class Cat(name: String, address: String = "Home",
2-
age: Int = 1)(a: Int = age, b: String = address + age)
1+
case class Cat(name: String, address: String = "Home", age: Int = 1)(a: Int = age, b: String = address + age)
32

43
@main def Test =
5-
println(defaultParams[Cat].toList.sortBy(_._1))
4+
println(defaultParams[Cat])

0 commit comments

Comments
 (0)