-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat: Improved Type checks and casts page #5161
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
base: master
Are you sure you want to change the base?
Conversation
35a8b9d to
edef763
Compare
| if (animal is Dog) { | ||
| println("Special care instructions: This is a dog.") | ||
| } else if (animal is Cat) { | ||
| println("Special care instructions: This is a cat.") | ||
| } |
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 that showing the same version with when could be interesting at this point.
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 see that there's an example below
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.
Yes! I wanted to explain how when worked with smart casts later :)
a6fb573 to
14576d7
Compare
| > | ||
| {style="tip"} | ||
|
|
||
| ## is and !is operators |
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.
Just a thought, to link it to the page title and intro, this header could be "Check operators", "Type checks", or "Checks with is and !is operators"
| > | ||
| {type="tip"} | ||
|
|
||
| ## Type casts |
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 get the intention, but with this additional subheader, we're losing subsection labels in the right-hand menu that seem important
| ``` | ||
| {kotlin-runnable="true" kotlin-min-compiler-version="2.0" id="kotlin-safe-cast-operator"} | ||
|
|
||
| #### Nullable types |
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 for consistency, as and as? operators should either have each a corresponding subsection, or none at all. Nullable types is rather short and could be just a part of Casting operators. But, again, if we want them to be visible in the right menu, two subheaders would be better.
| ### Smart cast prerequisites | ||
| #### Smart cast prerequisites | ||
|
|
||
| > Note that smart casts work only when the compiler can guarantee that the variable won't change between the check and its usage. |
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 know it's not a part of your changes, just caught my eye that a note starts with "Note that" and has a warning color. Also, the table is simple enough to be a MD table
|
|
||
| #### Control flow | ||
|
|
||
| Smart casts work not only for `if` conditional expressions but also for [`when` expressions](control-flow.md#when-expressions-and-statements): |
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.
| Smart casts work not only for `if` conditional expressions but also for [`when` expressions](control-flow.md#when-expressions-and-statements): | |
| Smart casts work not only for `if` conditional expressions, but also for [`when` expressions](control-flow.md#when-expressions-and-statements): |
|
|
||
| var status: Status = robo.status() | ||
| while (status is Ok) { | ||
| // The compiler smart casts status to OK type so the currentRoom |
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.
| // The compiler smart casts status to OK type so the currentRoom | |
| // The compiler smart casts status to OK type, so the currentRoom |
This PR improves the quality of examples on the Type checks and casts page, and answers questions received in external feedback.