Skip to content
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

Open
wants to merge 5 commits into
base: 2-1-0-doc-update
Choose a base branch
from

Conversation

AlejandraPedroza
Copy link
Contributor

This PR adds information about Guard conditions in when expressions and how to use them.

@AlejandraPedroza AlejandraPedroza requested a review from a team as a code owner September 4, 2024 13:55
@AlejandraPedroza AlejandraPedroza changed the base branch from master to 2-1-0-doc-update September 4, 2024 13:55
docs/topics/coding-conventions.md Outdated Show resolved Hide resolved
docs/topics/control-flow.md Show resolved Hide resolved
docs/topics/control-flow.md Outdated Show resolved Hide resolved
docs/topics/control-flow.md Outdated Show resolved Hide resolved
docs/topics/control-flow.md Outdated Show resolved Hide resolved
docs/topics/control-flow.md Outdated Show resolved Hide resolved
@koshachy koshachy self-assigned this Sep 10, 2024
docs/topics/control-flow.md Outdated Show resolved Hide resolved
docs/topics/control-flow.md Outdated Show resolved Hide resolved
Copy link
Member

@koshachy koshachy left a 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.

@@ -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):
Copy link
Member

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 ..."?

>
{type="warning"}

Starting from Kotlin 2.1, you can use guard conditions in `when` expressions or statements with a subject.
Copy link
Member

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.


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`:
Copy link
Member

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.

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.
Copy link
Member

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.


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).
Copy link
Member

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.

```

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:
Copy link
Member

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?

Copy link
Contributor Author

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 Show resolved Hide resolved
Comment on lines 250 to 252
> To enable guard conditions in Gradle, run the following command:
>
> `kotlin.compilerOptions.freeCompilerArgs.add("-Xwhen-guards")t`
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. Great catch!

docs/topics/sealed-classes.md Outdated Show resolved Hide resolved
AlejandraPedroza and others added 5 commits November 5, 2024 17:06
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants