Skip to content

Add when concept - #742

Open
kahgoh wants to merge 2 commits into
exercism:mainfrom
kahgoh:concept/when
Open

Add when concept#742
kahgoh wants to merge 2 commits into
exercism:mainfrom
kahgoh:concept/when

Conversation

@kahgoh

@kahgoh kahgoh commented Jun 25, 2026

Copy link
Copy Markdown
Member

This adds the when concept for Kotlin. Also took the opportunity to fix the name for the "Log Levels" exercise.

@SleeplessByte, this is ready for review

Comment thread concepts/when/about.md Outdated
# Introduction

Kotlin's `when` is a conditional expression for choosing among several options.
It is similar to `switch` in Java, but more flexible than a chain of `if` ... `else if` ... `else`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
It is similar to `switch` in Java, but more flexible than a chain of `if` ... `else if` ... `else`.
It is similar to `switch` in Java, and more flexible than a chain of `if` ... `else if` ... `else`.

Comment thread concepts/when/about.md
Comment on lines +23 to +26
- Each branch is written as `pattern -> result`.
- Branches are tried from to bottom until a match is found. Only the first branch runs.
- Use `else` for any value not listed above (similar to the final `else` in an `if` chain).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So this is pattern matching then?

Could be something to call out!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added mention about pattern matching.

Comment thread concepts/when/about.md
val x = 5
val y = 10
val description = when (x) {
5 if y % 2 == 0 -> "x is 5, y is even"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does regexp work?

@kahgoh kahgoh Aug 3, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, regexp can work as part of the guard. Something like this:

val code = 3
val classification = "animal"
val type = when (code) {
    3 if """.*mal""".toRegex().matches(classification) -> "animal"
    else -> "unknown"
}

But if using it match the value, I think you can only do it by matching conditions (i.e. without a value to match). For example:

val classification = "animal"
val type = when {
    Regex("''.*mal""").matches(classification) -> "animal"
    Regex(''"fru.*""").matches(classification) -> "fruit"
    else -> "unknown"
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh, I just learn I can indeed use Regexp but would need to define an extension function:

operator fun Regex.contains(text: CharSequence): Boolean = this.matches(text)

fun main() {
    val classification = "animal"
    val res = when(classification) {
        in Regex(""".*mal""") -> println("animal")
        in Regex("""fru.*""") -> println("fruit")
        else -> println("unknown")
    }

    println(res)
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added something about this in the about.md.

Comment thread concepts/when/introduction.md Outdated
# Introduction

Kotlin's `when` is a conditional expression for choosing among several options.
It is similar to `switch` in Java, but more flexible than a chain of `if` ... `else if` ... `else`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
It is similar to `switch` in Java, but more flexible than a chain of `if` ... `else if` ... `else`.
It is similar to `switch` in Java, and more flexible than a chain of `if` ... `else if` ... `else`.

These are the rules for the different replies:

- If the guess is `42`: "Correct"
- If the guess is `41` or `43` and there have been less than : "So close"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
- If the guess is `41` or `43` and there have been less than : "So close"
- If the guess is `41` or `43` and there have been no more than 5 guesses: "So close"

# Introduction

Kotlin's `when` is a conditional expression for choosing among several options.
It is similar to `switch` in Java, but more flexible than a chain of `if` ... `else if` ... `else`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
It is similar to `switch` in Java, but more flexible than a chain of `if` ... `else if` ... `else`.
It is similar to `switch` in Java, and more flexible than a chain of `if` ... `else if` ... `else`.

@kahgoh
kahgoh requested a review from SleeplessByte August 4, 2026 09:50
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.

2 participants