@@ -24,10 +24,11 @@ In order to exemplify this tutorial, we will consider the minimal macro library
24
24
lazy val example = project
25
25
.in(file(" example" ))
26
26
.settings(
27
- scalaVersion := " 2.13.11 " ,
27
+ scalaVersion := " 2.13.14 " ,
28
28
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
+ ),
31
32
)
32
33
```
33
34
@@ -45,7 +46,7 @@ case class Location(path: String, line: Int)
45
46
object Macros {
46
47
def location : Location = macro locationImpl
47
48
48
- private def locationImpl (c : Context ): c.Tree = {
49
+ def locationImpl (c : Context ): c.Tree = {
49
50
import c .universe ._
50
51
val location = typeOf[Location ]
51
52
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:
73
74
You can add Scala 3 to the list of ` crossScalaVersions ` of your project:
74
75
75
76
``` scala
76
- crossScalaVersions := Seq (" 2.13.11 " , " 3.3.1 " )
77
+ crossScalaVersions := Seq (" 2.13.14 " , " 3.3.3 " )
77
78
```
78
79
79
80
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:
81
82
82
83
``` scala
83
84
// build.sbt
@@ -91,15 +92,15 @@ libraryDependencies ++= {
91
92
}
92
93
```
93
94
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 ` .
96
97
97
98
## 2. Rearrange the code in version-specific source directories
98
99
99
100
If you try to compile with Scala 3 you should see some errors of the same kind as:
100
101
101
102
{% highlight text %}
102
- sbt: example > ++3.3.1
103
+ sbt: example > ++3.3.3
103
104
sbt: example > example / compile
104
105
[ error] -- Error: /example/src/main/scala/location/Location.scala:15:35
105
106
[ error] 15 | val location = typeOf[ Location]
@@ -143,7 +144,7 @@ import scala.language.experimental.macros
143
144
object Macros {
144
145
def location : Location = macro locationImpl
145
146
146
- private def locationImpl (c : Context ): c.Tree = {
147
+ def locationImpl (c : Context ): c.Tree = {
147
148
import c .universe ._
148
149
val location = typeOf[Location ]
149
150
val line = Literal (Constant (c.enclosingPosition.line))
@@ -156,7 +157,7 @@ object Macros {
156
157
{% endtabs %}
157
158
158
159
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.
160
161
161
162
{% tabs scala-3-location_1 %}
162
163
{% tab 'Scala 3 Only' %}
@@ -191,9 +192,9 @@ object Macros:
191
192
private def locationImpl (using quotes : Quotes ): Expr [Location ] =
192
193
import quotes .reflect .Position
193
194
val pos = Position .ofMacroExpansion
194
- val file = Expr (pos.sourceFile.jpath.toString )
195
+ val path = Expr (pos.sourceFile.path )
195
196
val line = Expr (pos.startLine + 1 )
196
- ' {new Location ($file , $line)}
197
+ ' {new Location ($path , $line)}
197
198
```
198
199
{% endtab %}
199
200
{% endtabs %}
@@ -222,13 +223,13 @@ class MacrosSpec extends munit.FunSuite {
222
223
You should now be able to run the tests in both versions.
223
224
224
225
{% highlight text %}
225
- sbt: example > ++2.13.11
226
+ sbt: example > ++2.13.14
226
227
sbt: example > example / test
227
228
location.MacrosSpec:
228
229
+ location
229
230
[ info] Passed: Total 1, Failed 0, Errors 0, Passed 1
230
231
[ success]
231
- sbt: example > ++3.3.1
232
+ sbt: example > ++3.3.3
232
233
sbt: example > example / test
233
234
location.MacrosSpec:
234
235
+ location
0 commit comments