Skip to content

Commit c26740c

Browse files
committed
Use concurrent.Map in MacroAnnotation example
This is much safer if users copy-paste the example blindly into their code.
1 parent 162b543 commit c26740c

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

library/src/scala/annotation/MacroAnnotation.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ trait MacroAnnotation extends StaticAnnotation:
4242
* This example shows how to modify a `def` and add a `val` next to it using a macro annotation.
4343
* ```scala
4444
* import scala.quoted.*
45-
* import scala.collection.mutable
45+
* import scala.collection.concurrent
4646
*
4747
* class memoize extends MacroAnnotation:
4848
* def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
@@ -52,14 +52,14 @@ trait MacroAnnotation extends StaticAnnotation:
5252
* (param.tpt.tpe.asType, tpt.tpe.asType) match
5353
* case ('[t], '[u]) =>
5454
* val cacheName = Symbol.freshName(name + "Cache")
55-
* val cacheSymbol = Symbol.newVal(Symbol.spliceOwner, cacheName, TypeRepr.of[mutable.Map[t, u]], Flags.Private, Symbol.noSymbol)
55+
* val cacheSymbol = Symbol.newVal(Symbol.spliceOwner, cacheName, TypeRepr.of[concurrent.Map[t, u]], Flags.Private, Symbol.noSymbol)
5656
* val cacheRhs =
5757
* given Quotes = cacheSymbol.asQuotes
58-
* '{ mutable.Map.empty[t, u] }.asTerm
58+
* '{ concurrent.TrieMap.empty[t, u] }.asTerm
5959
* val cacheVal = ValDef(cacheSymbol, Some(cacheRhs))
6060
* val newRhs =
6161
* given Quotes = tree.symbol.asQuotes
62-
* val cacheRefExpr = Ref(cacheSymbol).asExprOf[mutable.Map[t, u]]
62+
* val cacheRefExpr = Ref(cacheSymbol).asExprOf[concurrent.Map[t, u]]
6363
* val paramRefExpr = Ref(param.symbol).asExprOf[t]
6464
* val rhsExpr = rhsTree.asExprOf[u]
6565
* '{ $cacheRefExpr.getOrElseUpdate($paramRefExpr, $rhsExpr) }.asTerm
@@ -82,7 +82,7 @@ trait MacroAnnotation extends StaticAnnotation:
8282
* and the macro will modify the definition to create
8383
* ```scala
8484
* val fibCache$macro$1 =
85-
* scala.collection.mutable.Map.empty[Int, Int]
85+
* scala.collection.concurrent.TrieMap.empty[Int, Int]
8686
* def fib(n: Int): Int =
8787
* fibCache$macro$1.getOrElseUpdate(
8888
* n,

tests/run-macros/annot-memo/Macro_1.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import scala.annotation.{experimental, MacroAnnotation}
44
import scala.quoted._
5-
import scala.collection.mutable
5+
import scala.collection.concurrent
66

77
@experimental
88
class memoize extends MacroAnnotation:
@@ -13,14 +13,14 @@ class memoize extends MacroAnnotation:
1313
(param.tpt.tpe.asType, tpt.tpe.asType) match
1414
case ('[t], '[u]) =>
1515
val cacheName = Symbol.freshName(name + "Cache")
16-
val cacheSymbol = Symbol.newVal(Symbol.spliceOwner, cacheName, TypeRepr.of[mutable.Map[t, u]], Flags.Private, Symbol.noSymbol)
16+
val cacheSymbol = Symbol.newVal(Symbol.spliceOwner, cacheName, TypeRepr.of[concurrent.Map[t, u]], Flags.Private, Symbol.noSymbol)
1717
val cacheRhs =
1818
given Quotes = cacheSymbol.asQuotes
19-
'{ mutable.Map.empty[t, u] }.asTerm
19+
'{ concurrent.TrieMap.empty[t, u] }.asTerm
2020
val cacheVal = ValDef(cacheSymbol, Some(cacheRhs))
2121
val newRhs =
2222
given Quotes = tree.symbol.asQuotes
23-
val cacheRefExpr = Ref(cacheSymbol).asExprOf[mutable.Map[t, u]]
23+
val cacheRefExpr = Ref(cacheSymbol).asExprOf[concurrent.Map[t, u]]
2424
val paramRefExpr = Ref(param.symbol).asExprOf[t]
2525
val rhsExpr = rhsTree.asExprOf[u]
2626
'{ $cacheRefExpr.getOrElseUpdate($paramRefExpr, $rhsExpr) }.asTerm

0 commit comments

Comments
 (0)