applyIf and letIf?

Or is it String? and it evaluates to null if the condition is false. Having it be Any makes such a function about useless.

This sort of ambiguity is justification for it not to be in the standard library. You are free to define such a function yourself (as I said, I created a transformIf function) but it should not be in the standard library.

This is a case for something I have been advocating for, the idea of a when statement extension which would let you write something like this:

val s = 23
    .when {
        this % 2 == 0 -> "$this is even"
        else -> "$this is odd"
     }

which would be equivalent to:

val s = 23
    .run {
        when {
           this % 2 == 0 -> "$this is even"
           else -> "$this is odd"
        }
    }
1 Like

What is so strange or unusual about this? Generics often do this, reduce does this, even elvis operator does this and letIf case is similar to elvis. It is a very common thing to take a common supertype, even in stdlib.

1 Like