Allow when to be used like an extension

Today I had an idea that it might be handy to have the when expression

when(foo)
{
    0 -> something
    1 -> somethingElse
    else -> anotherThing
}

callable like an extension so you could instead say something like this:

foo.when
{
    0 -> something
    1 -> somethingElse
    else -> anotherThing
}

The use case I had was the desire to have the result of one when expression be used as the input of another when using a fluent style.

Without this to get a fluent style you would have to do something like this:

when(foo) { /**/ }.run { when(this) { /* */ } }

The other non-fluent options are to use a variable or nest them as in this unreadable version:

when(when(foo) { /**/ }) { /* */ } }
3 Likes

Alternative, more general solution: have a pipe operator.

1 Like