@@ -3641,6 +3641,66 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
36413641 // TODO: add flags and privateWithin
36423642 @ experimental def newClass (parent : Symbol , name : String , parents : List [TypeRepr ], decls : Symbol => List [Symbol ], selfType : Option [TypeRepr ]): Symbol
36433643
3644+ /** Generates a new module symbol with an associated module class symbol.
3645+ * This returns the module symbol. The module class can be accessed calling `moduleClass` on this symbol.
3646+ *
3647+ * Example usage:
3648+ * ```scala
3649+ * //{
3650+ * given Quotes = ???
3651+ * import quotes.reflect._
3652+ * //}
3653+ * val moduleName: String = Symbol.freshName("MyModule")
3654+ * val parents = List(TypeTree.of[Object])
3655+ * def decls(cls: Symbol): List[Symbol] =
3656+ * List(Symbol.newMethod(cls, "run", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit]), Flags.EmptyFlags, Symbol.noSymbol))
3657+ *
3658+ * val mod = Symbol.newModule(Symbol.spliceOwner, moduleName, Flags.EmptyFlags, Flags.EmptyFlags, parents.map(_.tpe), decls, Symbol.noSymbol)
3659+ * val cls = mod.moduleClass
3660+ * val runSym = cls.declaredMethod("run").head
3661+ *
3662+ * val runDef = DefDef(runSym, _ => Some('{ println("run") }.asTerm))
3663+ * val clsDef = ClassDef(cls, parents, body = List(runDef))
3664+ * val newCls = Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil)
3665+ * val modVal = ValDef(mod, Some(newCls))
3666+ * val modDef = List(modVal, clsDef)
3667+ *
3668+ * val callRun = Apply(Select(Ref(mod), runSym), Nil)
3669+ *
3670+ * Block(modDef, callRun)
3671+ * ```
3672+ * constructs the equivalent to
3673+ * ```scala
3674+ * //{
3675+ * given Quotes = ???
3676+ * import quotes.reflect._
3677+ * //}
3678+ * '{
3679+ * object MyModule$macro$1 extends Object:
3680+ * def run(): Unit = println("run")
3681+ * MyModule$macro$1.run()
3682+ * }
3683+ * ```
3684+ *
3685+ * @param parent The owner of the class
3686+ * @param name The name of the class
3687+ * @param modFlags extra flags to with which the module symbol should be constructed
3688+ * @param clsFlags extra flags to with which the module class symbol should be constructed
3689+ * @param parents The parent classes of the class. The first parent must not be a trait.
3690+ * @param decls The member declarations of the module provided the symbol of this class
3691+ * @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
3692+ *
3693+ * This symbol starts without an accompanying definition.
3694+ * It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3695+ * this symbol to the ClassDef and ValDef constructor.
3696+ *
3697+ * @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3698+ * direct or indirect children of the reflection context's owner.
3699+ *
3700+ * @syntax markdown
3701+ */
3702+ @ experimental def newModule (owner : Symbol , name : String , modFlags : Flags , clsFlags : Flags , parents : List [TypeRepr ], decls : Symbol => List [Symbol ], privateWithin : Symbol ): Symbol
3703+
36443704 /** Generates a new method symbol with the given parent, name and type.
36453705 *
36463706 * To define a member method of a class, use the `newMethod` within the `decls` function of `newClass`.
0 commit comments