-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
def isTypeParam(using ctx: Context): Boolean = | ||
internal.Symbol_isTypeParam(sym) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
List((address,Home), (age,1)) |
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) }) } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't zip in the next stage and use the
Suggested change
|
||||||
end defaultParamsImpl |
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]) |
Uh oh!
There was an error while loading. Please reload this page.