# Can kotlin's function return a function object as a result?

**URL:** <https://discuss.kotlinlang.org/t/can-kotlins-function-return-a-function-object-as-a-result/8011>\
**Category:** Support\
**Created:** [June 4, 2018, 1:16am UTC](https://discuss.kotlinlang.org/t/can-kotlins-function-return-a-function-object-as-a-result/8011 "2018-06-04T01:16:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![esbolatovakezhan](https://avatars.discourse-cdn.com/v4/letter/e/8e8cbc/32.png) [@esbolatovakezhan](https://discuss.kotlinlang.org/u/esbolatovakezhan)\
**Post date:** [June 4, 2018, 1:16am UTC](https://discuss.kotlinlang.org/t/can-kotlins-function-return-a-function-object-as-a-result/8011/1 "2018-06-04T01:16:39Z")

</div>

can kotlin’s function return a function object as a result?

---

<div class="post-metadata">

**Author:** ![Radzell](https://avatars.discourse-cdn.com/v4/letter/r/65b543/32.png) [@Radzell](https://discuss.kotlinlang.org/u/Radzell)\
**Post date:** [June 4, 2018, 2:35am UTC](https://discuss.kotlinlang.org/t/can-kotlins-function-return-a-function-object-as-a-result/8011/2 "2018-06-04T02:35:50Z")

</div>

yes

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [June 4, 2018, 3:36pm UTC](https://discuss.kotlinlang.org/t/can-kotlins-function-return-a-function-object-as-a-result/8011/3 "2018-06-04T15:36:23Z")

</div>

Yes, consider for example a function that takes a predicate and returns an inverted predicate:

```auto
fun <T> invert(predicate: (T) -> Boolean): (T) -> Boolean {
    return { !predicate(it) }
}

```

This function takes a functional type `(T) -> Boolean` as a parameter and returns it as a result.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [June 4, 2018, 5:47pm UTC](https://discuss.kotlinlang.org/t/can-kotlins-function-return-a-function-object-as-a-result/8011/4 "2018-06-04T17:47:10Z")

</div>

Also another useful feature of Kotlin regarding this are typealiases. For functions you pass around a lot you can create a type alias like this

```kotlin
typealias Predicate<T> = (T) -> Boolean

fun <T> invert (predicate: Predicate <T>): Predicate <T> {
    return { ! predicate(it) }
}

```

You can also name arguments of the typealias which can be helpful if your function takes multiple arguments

```kotlin
typealias Predicate<T> = (someArgName: T) -> Boolean

```
