Skip to content

Commit

Permalink
Andrey review
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandraPedroza committed Nov 5, 2024
1 parent f570eee commit 33c194c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/topics/coding-conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ 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):
Use parentheses when combining multiple boolean expressions in `when` expressions or statements with [guard conditions](control-flow.md#guard-conditions-in-when-expressions):

```kotlin
when (status) {
Expand Down
22 changes: 11 additions & 11 deletions docs/topics/control-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ The scope of variable introduced in *when* subject is restricted to the body of
> Guard conditions are an [experimental feature](components-stability.md#stability-levels-explained) that may be changed at any time.
> We would appreciate your feedback in [YouTrack](https://youtrack.jetbrains.com/issue/KT-71140/Guard-conditions-in-when-expressions-feedback).
>
{type="warning"}
{style="warning"}

Starting from Kotlin 2.1, you can use guard conditions in `when` expressions or statements with a subject.
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.
You can use guard conditions in `when` expressions or statements with a subject.

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`:
To include a guard condition in a branch, place it after the primary condition, separated by `if`:

```kotlin
sealed interface Animal {
Expand All @@ -212,9 +212,9 @@ In a single `when` expression, you can combine branches with and without guard c
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.
If you use guard conditions in `when` statements without an `else` branch, and 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).
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.

Additionally, guard conditions support `else if`:

Expand All @@ -232,22 +232,22 @@ when (animal) {
```

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:
Use parentheses around the boolean expressions to [avoid confusion](coding-conventions.md#guard-conditions-in-when-expression):

```kotlin
when (animal) {
is Animal.Cat if (!animal.mouseHunter && animal.hungry) -> feedCat()
}
```

You can use guard conditions in any `when` expression or statement with a subject, except the case when you have multiple conditions separated by a comma
(for example: `0, 1 -> print("x == 0 or x == 1")`).
You can use guard conditions in any `when` expression or statement with a subject, except the case when you have multiple conditions separated by a comma.
For example, `0, 1 -> print("x == 0 or x == 1")`.

> To enable guard conditions in CLI, run the following command:
>
> `kotlinc -Xwhen-guards main.kt`
>
> To enable guard conditions in Gradle, run the following command:
> To enable guard conditions in Gradle, add the following line to the `build.gradle.kts` file:
>
> `kotlin.compilerOptions.freeCompilerArgs.add("-Xwhen-guards")t`
>
Expand Down
2 changes: 1 addition & 1 deletion docs/topics/sealed-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fun main() {
```
{kotlin-runnable="true" kotlin-min-compiler-version="1.5"}

When using sealed classes with `when` expressions, you can also utilize guard conditions to include additional checks in a single branch.
When using sealed classes with `when` expressions, you can also add guard conditions to include additional checks in a single branch.
For more information, see [Guard conditions in when expressions](control-flow.md#guard-conditions-in-when-expressions).

> In multiplatform projects, if you have a sealed class with a `when` expression as an
Expand Down

0 comments on commit 33c194c

Please sign in to comment.