# When with "less than"

#1

Is it possible to do something like this:

``````val x:Int = //bla bla
return when(x){
< 1     -> "less than 1"
1       -> "one"
else    -> "greater than one"
}``````

#2

The nearest I can get to that is the following though itâ€™s not very elegant:

``````val x: Int = //bla bla
return when(x) {
in Int.MIN_VALUE..0 -> "less than 1"
1                   -> "one"
else                -> "greater than one"
}
``````

Personally, Iâ€™d just do:

``````val x: Int = //bla bla
return when {
x < 1    -> "less than 1"
x == 1   -> "one"
else     -> "greater than one"
}
``````

#3

I second the need for this as well. I was wanting the same thing just this week.

I know you can do the long way with the temporary variable but it feels ugly and I donâ€™t like it.

In another thread I proposed the ability to refer to the parameter to the when as â€śitâ€ť to use as a target of method calls as in:

``````when(string)
{
it.contains("A") -> println( "A")
``````

perhaps that could be extended to include relational expressions involving it:

``````when(x)
{
it < 1 -> "less than 1"``````

#4

I had a realization that it might be nice to do this with lambdas or function references:

E.g.:

``````when(string)
{
{ contains("A") } -> println( "A")
``````

or

``````when(x)
{
{ it < 1 } -> "less than 1"``````

#5

The current Kotlin approach looks cleaner, to my taste:

``````when {
x < 1 -> "less than 1"
``````

#6

Doesnâ€™t that nearly defeat the purpose of using a case based when statement?
Why does it support keywords such as â€śinâ€ť and â€śisâ€ť, but not operators? The syntax for that would be identical.

#7

In cases when it is a simple variable that is cleaner. But with more complex expressions that you only want evaluated once, it requires storing it in a variable to do that. After a little while doing FP and Kotlin you get to the point where it just feels dirty to actually create a named local variable.

#8

If you donâ€™t want to assign an explicit name to your complex expression, then `let` is there to help you:

``````complexExpressionHere.let {
when {
it < 1 -> "less than 1"
``````

#9

That still defeats the purpose of a case based when statementâ€¦
With what youâ€™re proposing, you might as well just use if statements and avoid the bloat of creating the when.

#10

I know there are other ways to work around it, and all that we are saying is that it would be really nice if the compiler didnâ€™t force you to do that. If I have a when with a value it would be nice to be able to refer to it inside the when as it and use it in more complex conditionals. The when(x) statement is just rather limiting in its capabilities and it would be nice if it were more powerful so I can just say:

``````    when(complexExpressionHere) {
it < 1 -> "less than 1"
``````

Unrelated to using it in conditions, your version also brought up an idea I suggested a while back, the idea of having an â€śextensionâ€ť when that is the equivalent of when(x) but expressed as x.when. It would be useful in chains to easilly sort of do a conditional mapping.

So for example now you have to use let like you did:

``````someComplexExpressionChain
.let {
when(it) {
is Foo -> "Foo"
is Bar -> "Bar"
else -> throw Error()
}
}
``````

My suggestion with the addition of a â€ś.whenâ€ť extension would allow you to express that as:

``````someComplexExpressionChain
.when {
is Foo -> "Foo"
is Bar -> "Bar"
else -> throw Error()
}