New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
in Scaladoc, be clearer that asInstanceOf
is unsafe (and platform-dependent)
#10696
Conversation
46f084e
to
c226e09
Compare
It's good to add these cautions, but perhaps it would also be good to give some indication of what the method is for. |
@SethTisue I actually woke up this morning and thought the same 😄 without an explanation for what it is intended for, it makes it look like the method shouldn't be there in the first place. I'll rework the doc, thanks for all input |
|
I think the comment is a bit overly wordy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall I think this new doc spends too many words talking about all the things that could happen in bad situations. It should focus more on what happens in good situations. Then have a section at the end that makes it clear, without giving a dozen examples, that bad casts have undefined behavior. Two examples should be enough: one with an expected JVM-style CCE failure, and one with unexpected "success".
@robstoll just wanted to say thanks for writing and updating this — we're making suggestions for adjustments, but it's super helpful to have somebody step up with that initial draft |
@SethTisue the review is appreciated and necessary. In the end we will have hopefully a better version than the current. I have one question though, seeing that |
Yes, the library, and hence the doc, is shared between Scala 2 and 3. |
@som-snytt @Sporarum @sjrd @bishabosha @SethTisue pushed, please review again |
Better to add some doc about the resulting bytecode(Java) vs patten matching, as https://github.com/JakeWharton/confundus |
@He-Pin see the first commit, I had examples of the resulting byte code (in prosa) and it was a consensus of the reviewers that those are too much detail |
a1178de
to
45ebb10
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better than before, but still seems overly wordy.
I'm not super familiar with scaladoc formatting, would it be possible to use the "one sentence = one line" convention ?
If yes, I would be in favor of changing this doc to that format
(And all other doc, but this is outside this PR's scope)
@Sporarum me neither, do you mean start every new sentence on a new line? |
Yes, doing the following for example: - * On a language level it merely tells the compiler to forget any type information it has about
- * the receiver object and treat it as if it had type `T0`. How the compiler manages to transition from the type
- * of the receiver object to `T0` is unspecified and should be considered an implementation detail of the
- * corresponding compiler backend and can vary between target platform and the target version.
- * Transitioning to T0 might involve inserting conversions (including boxing/unboxing), casts, default values or
- * just no real operation at all.
+ * On a language level it merely tells the compiler to forget any type information it has about the receiver object and treat it as if it had type `T0`.
+ * How the compiler manages to transition from the type of the receiver object to `T0` is unspecified and should be considered an implementation detail of the corresponding compiler backend and can vary between target platform and the target version.
+ * Transitioning to T0 might involve inserting conversions (including boxing/unboxing), casts, default values or just no real operation at all. |
I leave that change to someone else, I think we should still adhere to 120 chars max per line |
asInstanceOf
is unsafe (and platform-dependent)
I agree. I've pushed a commit that shortens this considerably, while still preserving what's most important, in my judgment anyway. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very good. Thanks all for the discussion and turning it into a concise explanation.
I've merged this since 2.13.14 is imminent, and since further improvements can always go in followup PRs. But I note that we lost this:
The new text just talks about "erased" and "erasure" without making it really concrete like this. I guess this was because we wanted to be scrupulous about not documenting JVM-specific semantics? It might be good to add something like this back while being clear it's describing the JVM behavior specifically? On the other hand, the Scaladoc of a particular method isn't the place to launch into long explanations of background about language semantics, what even is type erasure, and so on. But I'm not sure we have a doc page to link to that does explain that stuff. |
see https://contributors.scala-lang.org/t/pre-sip-deprecate-asinstanceof-introduce-unsafeasinstanceof/6568
for the motivation behind this change