@@ -14,7 +14,7 @@ into how it is specified and implemented in both Scala 2 and Scala 3.
14
14
15
15
The Scala 3 implementation is new, and that's the occasion for this
16
16
blog post. Thanks to this recent work, Scala 3 users can now access
17
- the entire Java reflection API.
17
+ the entire Java reflection API, as of Scala 3.3.0-RC1 .
18
18
19
19
## Should I keep reading?
20
20
@@ -49,10 +49,10 @@ landed in Scala 2.12.16 (released June 2022).
49
49
Just recently, [ Dale Wijnand] ported the feature to Scala 3, with the
50
50
assistance of [ Guillaume Martres] and myself, [ Seth Tisue] .
51
51
52
- ( Jason, Lukas, Dale, and myself are members of the Scala compiler team
52
+ Jason, Lukas, Dale, and myself are members of the Scala compiler team
53
53
at [ Lightbend] . We maintain Scala 2 and also contribute to Scala 3.
54
54
Guillaume has worked on the Scala 3 compiler for some years, previously
55
- at [ LAMP] and now at the [ Scala Center] .)
55
+ at [ LAMP] and now at the [ Scala Center] .
56
56
57
57
[ Jason Zaugg ] : https://github.com/retronym
58
58
[ Lukas Rytz ] : https://github.com/lrytz
@@ -75,8 +75,7 @@ could possibly have run into this feature!
75
75
That's because users are not allowed to define their own signature
76
76
polymorphic methods. Only the Java standard library can do that, and
77
77
so far, the creators of Java have only used the feature in these two
78
- classes. (Perhaps the feature will be made more widely available in
79
- some future JVM version?)
78
+ classes.
80
79
81
80
## What does "signature polymorphism" mean, exactly?
82
81
@@ -117,7 +116,7 @@ method. Its signature as seen from Scala is:
117
116
Signature polymorphism means that the ` AnyRef ` s here are just
118
117
placeholders for types to be supplied later.
119
118
120
- To see this work in practice, let's adapt one of the examples in
119
+ To see this work in practice, let's adapt an example from
121
120
the Javadoc. From Scala, we'll make a reflective call to the ` replace `
122
121
method on a ` String ` :
123
122
@@ -193,21 +192,24 @@ For details, see [PR 4139](), [PR 5594](), [PR 9530](), and [PR 9930]().
193
192
194
193
We had to work harder in Scala 3 because it wasn't enough to have an
195
194
an in-memory representation for signature polymorphic call sites. The
196
- call sites must also have a representation in TASTy. (Unlike Scala 2
197
- pickles, TASTy represents method bodies too, not only method
198
- signatures .)
195
+ call sites must also have a representation in TASTy. (Scala 2
196
+ pickles only represent method signatures; in contrast, TASTy
197
+ represents method bodies too .)
199
198
200
- Our initial implementation plan was to add a new node type to TASTy.
201
- We got this working, but it seemed unsatisfying to change TASTy to
202
- support such a rarely-used feature.
199
+ Our initial implementation plan was to add a new node type to TASTy,
200
+ and that's what we ended up doing.
203
201
204
- > TODO: Did we in fact go with Jason 's approach, in the end?
202
+ > TODO: point out what 's interesting
205
203
206
- So in the end, we took a different approach, based on a suggestion
207
- from Jason Zaugg. It works by rewriting each call site to include a
204
+ For details, see [ the pull request] ( https://github.com/lampepfl/dotty/pull/16225 ) .
205
+
206
+ ### The path not taken
207
+
208
+ Along the way we explored an alternative approach, suggested by Jason
209
+ Zaugg, which involved rewriting each call site to include a
208
210
cast to a structural type containing an appropriately typed method.
209
211
210
- For example , the ` replace ` call-site in the example above is
212
+ In that version , the ` replace ` call-site in the example above was
211
213
rewritten from:
212
214
213
215
mh.invokeExact("daddy", 'd', 'n'): String
@@ -220,15 +222,19 @@ to:
220
222
}
221
223
].invokeExact("daddy", 'd', 'n')
222
224
223
- (The actual rewrite is applied to in-memory ASTs, rather than to
225
+ (The actual rewrite was applied to in-memory ASTs, rather than to
224
226
source code.)
225
227
226
- The transformed code can be written and read as TASTy without
227
- trouble. Later in compilation, we detect which call sites are the
228
+ The transformed code could be written and read as TASTy without
229
+ trouble. Later in compilation, we detected which call sites are the
228
230
product of this transform, drop the cast, and emit the correct
229
231
bytecode.
230
232
231
- For details, see [ the pull request] ( https://github.com/lampepfl/dotty/pull/16225 ) .
233
+ In the end, we didn't go with this approach. As Sébastien Doeraene
234
+ pointed out, we avoided a new tag but we gave new semantics to
235
+ existing tags that older compilers wouldn't understand. Therefore the
236
+ work still couldn't ship until the next minor version of the compiler.
237
+ Besides, avoiding the new tag complicated the implementation.
232
238
233
239
## Questions? Discussion?
234
240
0 commit comments