Skip to content

Commit 896eaca

Browse files
committed
Update and correct macro cross tutorial
1 parent 8bec5d0 commit 896eaca

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

_overviews/scala3-migration/tutorial-macro-cross-building.md

+16-15
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ In order to exemplify this tutorial, we will consider the minimal macro library
2424
lazy val example = project
2525
.in(file("example"))
2626
.settings(
27-
scalaVersion := "2.13.11",
27+
scalaVersion := "2.13.14",
2828
libraryDependencies ++= Seq(
29-
"org.scala-lang" % "scala-reflect" % scalaVersion.value
30-
)
29+
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
30+
"org.scalameta" %% "munit" % "1.0.0" % Test,
31+
),
3132
)
3233
```
3334

@@ -45,7 +46,7 @@ case class Location(path: String, line: Int)
4546
object Macros {
4647
def location: Location = macro locationImpl
4748

48-
private def locationImpl(c: Context): c.Tree = {
49+
def locationImpl(c: Context): c.Tree = {
4950
import c.universe._
5051
val location = typeOf[Location]
5152
val line = Literal(Constant(c.enclosingPosition.line))
@@ -73,11 +74,11 @@ The main idea is to build the artifact twice and to publish two releases:
7374
You can add Scala 3 to the list of `crossScalaVersions` of your project:
7475

7576
```scala
76-
crossScalaVersions := Seq("2.13.11", "3.3.1")
77+
crossScalaVersions := Seq("2.13.14", "3.3.3")
7778
```
7879

7980
The `scala-reflect` dependency won't be useful in Scala 3.
80-
Remove it conditionally with something like:
81+
Add it conditionally under Scala 2 with something like:
8182

8283
```scala
8384
// build.sbt
@@ -91,15 +92,15 @@ libraryDependencies ++= {
9192
}
9293
```
9394

94-
After reloading sbt, you can switch to the Scala 3 context by running `++3.3.1`.
95-
At any point you can go back to the Scala 2.13 context by running `++2.13.11`.
95+
After reloading sbt, you can switch to the Scala 3 context by running `++3.3.3`.
96+
At any point you can go back to the Scala 2.13 context by running `++2.13.14`.
9697

9798
## 2. Rearrange the code in version-specific source directories
9899

99100
If you try to compile with Scala 3 you should see some errors of the same kind as:
100101

101102
{% highlight text %}
102-
sbt:example> ++3.3.1
103+
sbt:example> ++3.3.3
103104
sbt:example> example / compile
104105
[error] -- Error: /example/src/main/scala/location/Location.scala:15:35
105106
[error] 15 | val location = typeOf[Location]
@@ -143,7 +144,7 @@ import scala.language.experimental.macros
143144
object Macros {
144145
def location: Location = macro locationImpl
145146

146-
private def locationImpl(c: Context): c.Tree = {
147+
def locationImpl(c: Context): c.Tree = {
147148
import c.universe._
148149
val location = typeOf[Location]
149150
val line = Literal(Constant(c.enclosingPosition.line))
@@ -156,7 +157,7 @@ object Macros {
156157
{% endtabs %}
157158

158159
Now we can initialize each of our Scala 3 macro definitions in the `src/main/scala-3` folder.
159-
They must have the exact same signature than their Scala 2.13 counterparts.
160+
They must have the exact same signature as their Scala 2.13 counterparts.
160161

161162
{% tabs scala-3-location_1 %}
162163
{% tab 'Scala 3 Only' %}
@@ -191,9 +192,9 @@ object Macros:
191192
private def locationImpl(using quotes: Quotes): Expr[Location] =
192193
import quotes.reflect.Position
193194
val pos = Position.ofMacroExpansion
194-
val file = Expr(pos.sourceFile.jpath.toString)
195+
val path = Expr(pos.sourceFile.path)
195196
val line = Expr(pos.startLine + 1)
196-
'{new Location($file, $line)}
197+
'{new Location($path, $line)}
197198
```
198199
{% endtab %}
199200
{% endtabs %}
@@ -222,13 +223,13 @@ class MacrosSpec extends munit.FunSuite {
222223
You should now be able to run the tests in both versions.
223224

224225
{% highlight text %}
225-
sbt:example> ++2.13.11
226+
sbt:example> ++2.13.14
226227
sbt:example> example / test
227228
location.MacrosSpec:
228229
+ location
229230
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
230231
[success]
231-
sbt:example> ++3.3.1
232+
sbt:example> ++3.3.3
232233
sbt:example> example / test
233234
location.MacrosSpec:
234235
+ location

_overviews/scala3-migration/tutorial-macro-mixing.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ object Macros:
6767

6868
private def locationImpl(using quotes: Quotes): Expr[Location] =
6969
import quotes.reflect.Position
70-
val file = Expr(Position.ofMacroExpansion.sourceFile.jpath.toString)
70+
val path = Expr(Position.ofMacroExpansion.sourceFile.path)
7171
val line = Expr(Position.ofMacroExpansion.startLine + 1)
72-
'{new Location($file, $line)}
72+
'{new Location($path, $line)}
7373
```
7474
{% endtab %}
7575
{% endtabs %}

0 commit comments

Comments
 (0)