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

Add class parameters, flags, and privateWithin to newClass in reflect API #21880

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jchyb
Copy link
Contributor

@jchyb jchyb commented Nov 3, 2024

Still some TODOs here, like tests for flags and privateWithin, scaladoc, adding assertions and adding similar updates to newModule. I probably will need to split off the changes relating to -Xmacro-check from here to a separate PR.

Fixes #21739 and addresses some old TODOs

@jchyb jchyb added the needs-minor-release This PR cannot be merged until the next minor release label Nov 3, 2024
*
* Parameters can be obtained via classSymbol.memberField
*/
@experimental def newClass(parent: Symbol, name: String, parents: List[TypeRepr], decls: Symbol => List[Symbol], selfType: Option[TypeRepr], paramNames: List[String], paramTypes: List[TypeRepr], flags: Flags, privateWithin: Symbol): Symbol
Copy link
Member

Choose a reason for hiding this comment

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

would it be possible to reformulate such that parents is also a function (to support param refs?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean that it should be a Symbol => List[TypeRepr] type? I have some trouble visualizing where we would need that classSymbol to define the parent TypeRepr (class Cls extends Cls.InnerCls?, is that legal?). Could you give me an example of a tree like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just saw class Cls extends Cls.InnerCls in the wild but in that case, Cls also had a companion object which contained InnerCls - not sure if that is possible to define right now but I'll have to look into the newModule method and see if it also needs updates

Copy link
Member

@bishabosha bishabosha Nov 5, 2024

Choose a reason for hiding this comment

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

I mean something like class anon(x: Int) extends Box[x.type] - I don't know the best solution, but this would be good to be able to support I assume - same with tracked eventually, but maybe that can be cleanly added later?

@jchyb jchyb force-pushed the improvement/quotes-new-class-upgrades branch from 28cb854 to 33fbd5f Compare November 4, 2024 11:13
import quotes.reflect.*

val name = nameExpr.valueOrAbort
val parents = List(TypeTree.of[Object])
Copy link
Member

@bishabosha bishabosha Nov 4, 2024

Choose a reason for hiding this comment

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

there should be a test case where the parent needs an argument supplied via the constructor parameters

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 tried doing that in the not-very-well-named tests/run-macros/newClassParamsExtendsClassParams, which generates something like this:

'{
   class `name`(idx: Int) extends Foo(idx)
   new `name`(22)
}

Or do you mean something else?

Copy link
Member

Choose a reason for hiding this comment

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

yeah that looks good

Copy link

@goshacodes goshacodes Jan 5, 2025

Choose a reason for hiding this comment

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

Also such test cases could be added:

  1. Creating a trait with Symbol.newClass (is this possible?)
  2. Creating an abstract class with Symbol.newClass
  3. trait with parameter extending another trait with parameter (not sure about this)
  4. class with multiple parameters (this probably not needed)
  5. extending java class with parameter

@tgodzik tgodzik requested a review from sjrd November 6, 2024 15:51
@jchyb
Copy link
Contributor Author

jchyb commented Nov 6, 2024

To be clear, this is not ready, I did put this up since I wanted some feedback if anyone had some (Thank You @bishabosha!) and did not want to rush it last minute, that's why it a draft

@jchyb jchyb force-pushed the improvement/quotes-new-class-upgrades branch from a839263 to 81cea85 Compare January 2, 2025 11:23
paramNames: List[String],
paramTypes: List[TypeRepr],
clsFlags: Flags,
clsPrivateWithin: Symbol

Choose a reason for hiding this comment

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

Some nit comments.

Since it is public API, maybe align argument names with already existing? E.G. flags and not clsFlags, privateWithin and not clsPrivateWithin

clsPrivateWithin: Symbol,
consFlags: Flags,
consPrivateWithin: Symbol,
conParamFlags: List[List[Flags]]

Choose a reason for hiding this comment

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

constructor related arguments may have same prefix (now there are 3 constructor, cons and con) and maybe put all constructor related arguments together after everything else

@goshacodes
Copy link

Want this very much 👀

@goshacodes
Copy link

@jchyb @nicolasstucki Can I somehow help proceeding with this? Maybe contribute with a spree?

@jchyb
Copy link
Contributor Author

jchyb commented Jan 7, 2025

It's pretty much done in terms of the difficult stuff. I still need to add some test cases (your suggestions should help, thank you!), docs and perhaps more checks for flags. I planned for this for 3.7, and we still have some time for that, thus the seemingly slow progress.

@jchyb jchyb changed the title Add class term parameters, flags, and privateWithin to newClass in reflect API Add class parameters, flags, and privateWithin to newClass in reflect API Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-minor-release This PR cannot be merged until the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Symbol.newClass does not support class parameters
3 participants