Skip to content

Commit 3a5ec3a

Browse files
committed
adjust for final implementation
1 parent 29f16db commit 3a5ec3a

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

_posts/2022-00-00-signature-polymorphic-methods.md

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ into how it is specified and implemented in both Scala 2 and Scala 3.
1414

1515
The Scala 3 implementation is new, and that's the occasion for this
1616
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.
1818

1919
## Should I keep reading?
2020

@@ -49,10 +49,10 @@ landed in Scala 2.12.16 (released June 2022).
4949
Just recently, [Dale Wijnand] ported the feature to Scala 3, with the
5050
assistance of [Guillaume Martres] and myself, [Seth Tisue].
5151

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
5353
at [Lightbend]. We maintain Scala 2 and also contribute to Scala 3.
5454
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].
5656

5757
[Jason Zaugg]: https://github.com/retronym
5858
[Lukas Rytz]: https://github.com/lrytz
@@ -75,8 +75,7 @@ could possibly have run into this feature!
7575
That's because users are not allowed to define their own signature
7676
polymorphic methods. Only the Java standard library can do that, and
7777
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.
8079

8180
## What does "signature polymorphism" mean, exactly?
8281

@@ -117,7 +116,7 @@ method. Its signature as seen from Scala is:
117116
Signature polymorphism means that the `AnyRef`s here are just
118117
placeholders for types to be supplied later.
119118

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
121120
the Javadoc. From Scala, we'll make a reflective call to the `replace`
122121
method on a `String`:
123122

@@ -193,21 +192,24 @@ For details, see [PR 4139](), [PR 5594](), [PR 9530](), and [PR 9930]().
193192

194193
We had to work harder in Scala 3 because it wasn't enough to have an
195194
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.)
199198

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.
203201

204-
> TODO: Did we in fact go with Jason's approach, in the end?
202+
> TODO: point out what's interesting
205203
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
208210
cast to a structural type containing an appropriately typed method.
209211

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
211213
rewritten from:
212214

213215
mh.invokeExact("daddy", 'd', 'n'): String
@@ -220,15 +222,19 @@ to:
220222
}
221223
].invokeExact("daddy", 'd', 'n')
222224

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
224226
source code.)
225227

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
228230
product of this transform, drop the cast, and emit the correct
229231
bytecode.
230232

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.
232238

233239
## Questions? Discussion?
234240

0 commit comments

Comments
 (0)