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

Conversation

anatoliykmetyuk
Copy link
Contributor

No description provided.

@@ -0,0 +1,4 @@
case class Cat(name: String, address: String = "Home", age: Int = 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

This case is trivial. We should add more complex tests like

case class Cat(name: String, address: String = "Home", age: Int = 1)(a: Int = age, b: String = address + age)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a clarification in the docs that currently we are working only against the first argument group. Trying to trivially return symbols for the defs of the other argument groups' params yielded crashes on testing.

@@ -1332,6 +1332,11 @@ 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. Keys are names of the parameters and values –
Copy link
Contributor

Choose a reason for hiding this comment

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

Why only for case classes? We should check what happens with methods and other constructors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The implementation works against companions of case classes. I believe for methods, the logic will not work out of the box and will have to be modified.

I would prefer to take it one step at a time: the most trivial implementation first and more complex will follow. With the current implementation, we can already do something porting-wise. We can create an issue for more advanced features like more argument groups (and the discussion of how exactly that should work) or default params of method symbols.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then it should be called caseClassDefaultParams.

For some reason it fails the Community Build. FTTB disable
it as a hotfix.
Co-authored-by: Nicolas Stucki <nicolas.stucki@gmail.com>

Update tests
@anatoliykmetyuk
Copy link
Contributor Author

On top #9279

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) } })

* 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants