-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Guard conditions in when expressions #4417
base: 2-1-0-doc-update
Are you sure you want to change the base?
Conversation
10f5620
to
de98aa1
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.
Good job!
See my comments in the PR.
docs/topics/coding-conventions.md
Outdated
@@ -999,6 +999,24 @@ when (x) { | |||
|
|||
Prefer using `when` if there are three or more options. | |||
|
|||
### Guard conditions in when expression | |||
|
|||
Prefer using parentheses when combining multiple boolean expressions in `when` expressions or statements with [guard conditions](control-flow.md#guard-conditions-in-when-expressions): |
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.
Actually, I don't like "Prefer" in the beginning of that sentence.
Can we omit it? Like "Use parentheses ..."?
docs/topics/control-flow.md
Outdated
> | ||
{type="warning"} | ||
|
||
Starting from Kotlin 2.1, you can use guard conditions in `when` expressions or statements with a subject. |
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.
Actually, we try to avoid such phrases since our documentation reflects the latest state of the language.
docs/topics/control-flow.md
Outdated
|
||
Guard conditions allow you to include more than one condition to the branches of a `when` expression, making complex control flow more explicit and concise. | ||
|
||
To include a guard condition in a branch, place the guard condition after the primary condition, separated by `if`: |
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.
place it.
Will it work?
Just to avoid repetition.
docs/topics/control-flow.md
Outdated
The code in a branch with a guard condition runs only if both the primary condition and the guard condition evaluate to true. | ||
If the primary condition does not match, the guard condition is not evaluated. | ||
|
||
If you use guard conditions in `when` statements without an `else` branch, if none of the conditions matches, none of the branches is executed. |
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'd like to avoid "if" doubling.
Can we say: If you use guard conditions in when
statements without an else
branch, and none of the conditions match, no branches are executed.
docs/topics/control-flow.md
Outdated
|
||
If you use guard conditions in `when` statements without an `else` branch, if none of the conditions matches, none of the branches is executed. | ||
|
||
Otherwise, if you use guard conditions in `when` expressions without an `else` branch, the compiler requires you to declare all the possible cases (to avoid runtime errors). |
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 guess that "to avoid runtime errors" could be mentioned without parentheses.
docs/topics/control-flow.md
Outdated
``` | ||
|
||
Combine multiple guard conditions within a single branch using the boolean operators `&&` (AND) or `||` (OR). | ||
It is [strongly recommended](coding-conventions.md#guard-conditions-in-when-expression) to use parentheses around the boolean expressions to avoid confusion: |
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.
How about to rephrase it as
"To avoid confusion, use parentheses ...". Or is it too strong?
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.
Left it as "Use parentheses around the boolean expressions to..." to make it action-oriented.
docs/topics/control-flow.md
Outdated
> To enable guard conditions in Gradle, run the following command: | ||
> | ||
> `kotlin.compilerOptions.freeCompilerArgs.add("-Xwhen-guards")t` |
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.
Shouldn't the option be added to the build.gradle.kts
file?
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.
True. Great catch!
This PR adds information about Guard conditions in when expressions and how to use them.
Co-authored-by: Alejandro Serrano <[email protected]>
Co-authored-by: Alejandro Serrano <[email protected]>
74fdc3d
to
33c194c
Compare
This PR adds information about Guard conditions in when expressions and how to use them.