# Function Literals

**URL:** <https://discuss.kotlinlang.org/t/function-literals/306>\
**Category:** Uncategorized\
**Created:** [March 19, 2015, 10:00am UTC](https://discuss.kotlinlang.org/t/function-literals/306 "2015-03-19T10:00:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dittert](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dittert/32/1736_2.png) [@Dittert](https://discuss.kotlinlang.org/u/Dittert)\
**Post date:** [March 19, 2015, 10:00am UTC](https://discuss.kotlinlang.org/t/function-literals/306/1 "2015-03-19T10:00:52Z")

</div>

I'm wondering about this:

&nbsp;

```kotlin
val funLit = { String.() ->
    println("funLit()")
}
```

I'm not really sure what the syntax means in this case. What is the type of funLit and why can I call it the same way as an extension function? What is the advantage declaring it like that (instead of a extension function)?

---

<div class="post-metadata">

**Author:** ![eliteSchaf](https://avatars.discourse-cdn.com/v4/letter/e/58956e/32.png) [@eliteSchaf](https://discuss.kotlinlang.org/u/eliteSchaf)\
**Post date:** [March 19, 2015, 11:37am UTC](https://discuss.kotlinlang.org/t/function-literals/306/2 "2015-03-19T11:37:00Z")

</div>

The type of funLit is **ExtensionFunction\<String, Unit\>**.

With&nbsp;&nbsp;the { } syntax you create either a callable Function ( () -\> Unit)&nbsp;&nbsp;or an Extensionfunction ( Type.() -\> Unit), which can be used for&nbsp;&nbsp;lazy evaluation.  
Declaring an Extensionfunction like “public fun String.funLit() { … }” is the same as your funLit.

---

<div class="post-metadata">

**Author:** ![Dittert](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dittert/32/1736_2.png) [@Dittert](https://discuss.kotlinlang.org/u/Dittert)\
**Post date:** [March 19, 2015, 12:03pm UTC](https://discuss.kotlinlang.org/t/function-literals/306/3 "2015-03-19T12:03:25Z")

</div>

Thanks! I missed that the type changes from Function... to ExtensionFunction....

So basically Kotlin will allow a function to be used as an extension function if the type is ExtensionFunction? Of course I had to try that out in all possible ways. I didn’t succeed creating an extension function in Java, though. There, I have the correct types and interfaces available but I don’t seem to be able to access the result from Kotlin. Is there a way to do that?

But more importantly, I’m wondering about the use cases for declaring extension functions as lambdas. You mention lazy evaluation but I don’t see how that can be the case (as extension functions do not have state of their own). I also can’t imagine that you would want to pass extension functions as a parameter to something else.

Looking at the byte code, you would at least be able to pass such an extension function literal around (because they compile to an instance of ExtensionFunction) whereas “real” extension functions compile to static methods.

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [March 19, 2015, 12:16pm UTC](https://discuss.kotlinlang.org/t/function-literals/306/4 "2015-03-19T12:16:07Z")

</div>

It's useful when creating DSLs, for example: [Type-safe Groovy-style builders](http://kotlinlang.org/docs/reference/type-safe-builders.html).

---

<div class="post-metadata">

**Author:** ![eliteSchaf](https://avatars.discourse-cdn.com/v4/letter/e/58956e/32.png) [@eliteSchaf](https://discuss.kotlinlang.org/u/eliteSchaf)\
**Post date:** [March 19, 2015, 12:54pm UTC](https://discuss.kotlinlang.org/t/function-literals/306/5 "2015-03-19T12:54:34Z")

</div>

I meant that lambdas in general can be used for lazy evaluation (e.g. infinite PrimeNumber iterator).

One usage of the local ExtensionFunction I can think of is when a method requires an ExtensionFunction and your ExtensionFunction is only used at one place.,  
so there is no need to declare it explicitly.

---

<div class="post-metadata">

**Author:** ![Dittert](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dittert/32/1736_2.png) [@Dittert](https://discuss.kotlinlang.org/u/Dittert)\
**Post date:** [March 19, 2015, 1:09pm UTC](https://discuss.kotlinlang.org/t/function-literals/306/6 "2015-03-19T13:09:41Z")

</div>

Thanks. So basically it's about two things: Given that you can call function literals without brackets and the fact that the extension function provides access to the receiver that syntax is mainly relevant to build a DSLs.

---

<div class="post-metadata">

**Author:** ![eliteSchaf](https://avatars.discourse-cdn.com/v4/letter/e/58956e/32.png) [@eliteSchaf](https://discuss.kotlinlang.org/u/eliteSchaf)\
**Post date:** [March 19, 2015, 2:09pm UTC](https://discuss.kotlinlang.org/t/function-literals/306/7 "2015-03-19T14:09:04Z")

</div>

It's like they read your post a couple of weeks before :)

Extensionfunctions like { Type.() -\> … } are deprecated with the new M11.  
You should to write “val funLite = fun String.() -\> { println(“funLite”) }” from now on.

So { … } are Functions only now.
