# applyIf and letIf?

**URL:** <https://discuss.kotlinlang.org/t/applyif-and-letif/26390>\
**Category:** Libraries\
**Created:** [February 15, 2023, 12:12pm UTC](https://discuss.kotlinlang.org/t/applyif-and-letif/26390 "2023-02-15T12:12:20Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [April 12, 2025, 7:15pm UTC](https://discuss.kotlinlang.org/t/applyif-and-letif/26390/21 "2025-04-12T19:15:41Z")

</div>

> [@kyay10](#):
>
> It’d be `Any` here (well, actually `Comparable<*>` but whatever). The type is the supertype between `letIf`’s receiver and the return type of the block.

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.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [April 12, 2025, 7:26pm UTC](https://discuss.kotlinlang.org/t/applyif-and-letif/26390/22 "2025-04-12T19:26:59Z")

</div>

> [@Landei](#):
>
> ```auto
> val s = 23
> .given{ it % 2 == 0 }
> .then { "$it is even" }
> .otherwise { "$it is odd" }
> 
> ```

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:

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

```

which would be equivalent to:

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

```

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [April 13, 2025, 12:39pm UTC](https://discuss.kotlinlang.org/t/applyif-and-letif/26390/23 "2025-04-13T12:39:42Z")

</div>

> [@dalewking](#):
>
> 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.

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.

[Previous page](https://discuss.kotlinlang.org/t/applyif-and-letif/26390.md?page=1)
