Mainly Java 9 and a little Kotlin
Nicolai Parlog
51

I recently began learning Kotlin and stumbled across your post. It inspired me to look deeper into ‘when’, which apparently is extremely forgiving since it allows expressions. It let me take your example surprisingly far:

fun validMonth(month: Int): Boolean
{
return month in 1..12
}

fun checkMonth(month: Int)
{
val is24 = { m: Int -> m == 11 }
when
{
month in 1..6 -> println("1 .. 6")
month <= 10 -> println("< 10")
is24(month) -> println("is 11")
({ m: Int -> m == 12 })(month) -> println("is 12")
!validMonth(month) -> println("Invalid month")
}
}

checkMonth(6) // "1 .. 6"
checkMonth(9) // "< 10"
checkMonth(11) // "is 11"
checkMonth(12) // "is 12"
checkMonth(72) // "Invalid month"

Pretty cool. Thanks for the post!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.