Skip to content

Commit 079a786

Browse files
committed
Update MacroAnnotation documentation
1 parent 5fcb869 commit 079a786

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

library/src/scala/annotation/MacroAnnotation.scala

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,41 @@ package annotation
44

55
import scala.quoted._
66

7-
/** Base trait for macro annotation that will transform a definition */
7+
/** Base trait for macro annotation implementation.
8+
* Macro annotations can transform definitions and add new definitions.
9+
*
10+
* See: `MacroAnnotation.transform`
11+
*
12+
* @syntax markdown
13+
*/
814
@experimental
915
trait MacroAnnotation extends StaticAnnotation:
1016

11-
/** Transform the `tree` definition and add other definitions
17+
/** Transform the `tree` definition and add new definitions
1218
*
1319
* This method takes as argument the annotated definition.
1420
* It returns a non-empty list containing the modified version of the annotated definition.
1521
* The new tree for the definition must use the original symbol.
1622
* New definitions can be added to the list before or after the transformed definitions, this order
17-
* will be retained.
23+
* will be retained. New definitions will not be visible from outside the macro expansion.
1824
*
19-
* All definitions in the result must have the same owner. The owner can be recovered from `tree.symbol.owner`.
25+
* #### Restrictions
26+
* - All definitions in the result must have the same owner. The owner can be recovered from `Symbol.spliceOwner`.
27+
* - Special case: an annotated top-level `def`, `val`, `var`, `lazy val` can return a `class`/`object`
28+
definition that is owned by the package or package object.
29+
* - Can not return a `type`.
30+
* - Annotated top-level `class`/`object` can not return top-level `def`, `val`, `var`, `lazy val`.
31+
* - Can not see new definition in user written code.
2032
*
21-
* The result cannot add new `class`, `object` or `type` definition. This limitation will be relaxed in the future.
33+
* #### Good practices
34+
* - Make your new definitions private if you can.
35+
* - New definitions added as class members should use a fresh name (`Symbol.freshName`) to avoid collisions.
36+
* - New top-level definitions should use a fresh name (`Symbol.freshName`) that includes the name of the annotated
37+
* member as a prefix to avoid collisions of definitions added in other files.
2238
*
23-
* IMPORTANT: When developing and testing a macro annotation, you must enable `-Xcheck-macros` and `-Ycheck:all`.
39+
* **IMPORTANT**: When developing and testing a macro annotation, you must enable `-Xcheck-macros` and `-Ycheck:all`.
2440
*
25-
* Example 1:
41+
* #### Example 1
2642
* This example shows how to modify a `def` and add a `val` next to it using a macro annotation.
2743
* ```scala
2844
* import scala.quoted.*
@@ -54,7 +70,10 @@ trait MacroAnnotation extends StaticAnnotation:
5470
* List(tree)
5571
* ```
5672
* with this macro annotation a user can write
57-
* ```scala sc:nocompile
73+
* ```scala
74+
* //{
75+
* class memoize extends scala.annotation.StaticAnnotation
76+
* //}
5877
* @memoize
5978
* def fib(n: Int): Int =
6079
* println(s"compute fib of $n")
@@ -74,7 +93,7 @@ trait MacroAnnotation extends StaticAnnotation:
7493
* )
7594
* ```
7695
*
77-
* Example 2:
96+
* #### Example 2
7897
* This example shows how to modify a `class` using a macro annotation.
7998
* It shows how to override inherited members and add new ones.
8099
* ```scala
@@ -164,7 +183,10 @@ trait MacroAnnotation extends StaticAnnotation:
164183
* }
165184
* ```
166185
* with this macro annotation a user can write
167-
* ```scala sc:nocompile
186+
* ```scala
187+
* //{
188+
* class equals extends scala.annotation.StaticAnnotation
189+
* //}
168190
* @equals class User(val name: String, val id: Int)
169191
* ```
170192
* and the macro will modify the class definition to generate the following code
@@ -184,5 +206,7 @@ trait MacroAnnotation extends StaticAnnotation:
184206
*
185207
* @param Quotes Implicit instance of Quotes used for tree reflection
186208
* @param tree Tree that will be transformed
209+
*
210+
* @syntax markdown
187211
*/
188212
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition]

0 commit comments

Comments
 (0)