# Using functions as lamdas

**URL:** <https://discuss.kotlinlang.org/t/using-functions-as-lamdas/16439>\
**Category:** Support\
**Created:** [February 21, 2020, 3:34pm UTC](https://discuss.kotlinlang.org/t/using-functions-as-lamdas/16439 "2020-02-21T15:34:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kurtg](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@kurtg](https://discuss.kotlinlang.org/u/kurtg)\
**Post date:** [February 21, 2020, 3:34pm UTC](https://discuss.kotlinlang.org/t/using-functions-as-lamdas/16439/1 "2020-02-21T15:34:35Z")

</div>

I can get this to compile:

```
class Example {
    val myLambda = ::addExt
    val lambdaMap : Map<String, (String) -> String> = mapOf(
        "world" to { value -> addExt(value) } 
    )

    fun addExt(value: String): String {
        return "$value.gif"
    }
}

```

But, I can’t seem to get this work:

```
class Example {
    val myLambda = ::addExt
    val lambdaMap: Map<String, (String) -> String> = mapOf(
        "world" to { value -> addExt(value) }, 
        "mars" to ::addExt
    )

    fun addExt(value: String): String {
        return "$value.gif"
    }
}

```

Oddly, it’s the line with “world” that is the problem. If I comment it out, it will compile.

I get the error:  
Type mismatch.  
Required:  
KFunction1\<@ParameterName String, String\>  
Found:  
(???) → String

If I change it to:

```
class Example {
    val myLambda = ::addExt
    val lambdaMap: Map<String, (String) -> String> = mapOf(
        "world" to { value:String -> addExt(value) },
        "mars" to ::addExt
    )

    fun addExt(value: String): String {
        return "$value.gif"
    }
}

```

With the explicit, “value: String”, it will compile again.

I would expect Kotlin to implicitly figure out the return type as it did in the first example.

Bug or not a Bug?

---

<div class="post-metadata">

**Author:** ![denkspuren](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/denkspuren/32/7267_2.png) [@denkspuren](https://discuss.kotlinlang.org/u/denkspuren)\
**Post date:** [February 21, 2020, 5:53pm UTC](https://discuss.kotlinlang.org/t/using-functions-as-lamdas/16439/2 "2020-02-21T17:53:23Z")

</div>

The following line does compile in the map:

```auto
Pair<String, (String) -> String>("moon", ::addExt)

```

With `to` some type information seems to be missing.

Hold on, that’s amazing. Once the magic `Pair` is added, it starts to work:

```auto
    val lambdaMap : Map<String, (String) -> String> = mapOf(
        "world" to { value -> addExt(value) },
        "mars" to { v -> addExt(v) },
        Pair("pluto", { v -> myLambda(v) }),
        Pair<String, (String) -> String>("moon", ::addExt), // magic
        Pair("earth", ::addExt),
        "venus" to ::addExt
    )

```

What’s going on here?

Looks like the problem boils down to:

```auto
fun addExt(value: String): String = "$value.gif"
println({ value -> addExt(value) }) // cannot infer type for value
println(::addExt)

```

Add the type information and you see that a lambda/fun literal does not produce the same kind of thing like a function reference does:

```auto
println({ value: String -> addExt(value) })
println(fun(value: String) = addExt(value))
println(::addExt)

```

Prints:

```auto
(kotlin.String) -> kotlin.String
(kotlin.String) -> kotlin.String
fun Experiment.addExt(kotlin.String): kotlin.String

```

I think you are onto something. When these different kinds of functions are combined, type inference needs a hint for one kind or the other to compile properly. Type unification doesn’t work accross these different function kinds. I have no explanation for this.

```auto
fun addExt(value: String): String = "$value.gif"

val map1: Map<String, (String) -> String> = mapOf(
    "world" to { value -> addExt(value) }
)

val map2: Map<String, (String) -> String> = mapOf(
    "mars" to ::addExt
)

val map3: Map<String, (String) -> String> = mapOf(
    "world" to { value -> addExt(value) },
    Pair<String, (String) -> String>("moon", ::addExt),
    "mars" to ::addExt
)

val map4: Map<String, (String) -> String> = mapOf(
    "world" to { value: String -> addExt(value) },
    "earth" to { value -> addExt(value) },
    "moon" to ::addExt
)

```

---

<div class="post-metadata">

**Author:** ![kurtg](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@kurtg](https://discuss.kotlinlang.org/u/kurtg)\
**Post date:** [February 21, 2020, 8:48pm UTC](https://discuss.kotlinlang.org/t/using-functions-as-lamdas/16439/3 "2020-02-21T20:48:26Z")

</div>

I’m still a kotlin newbie. Should I write an issue? Or, how do I push it forward?

---

<div class="post-metadata">

**Author:** ![denkspuren](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/denkspuren/32/7267_2.png) [@denkspuren](https://discuss.kotlinlang.org/u/denkspuren)\
**Post date:** [February 21, 2020, 10:03pm UTC](https://discuss.kotlinlang.org/t/using-functions-as-lamdas/16439/4 "2020-02-21T22:03:12Z")

</div>

I’m not sure, since I’m new to the Kotlin community. Maybe wait one or two days if somebody else adds comments to this issue. Maybe a crosspost in “language design” gets some attention to your topic. I guess that there is a technical reason for the behavior you observed. Nonetheless, type inference should infer what is inferable. 😄

---

<div class="post-metadata">

**Author:** ![ivan.kubyshkin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ivan.kubyshkin/32/7066_2.png) [@ivan.kubyshkin](https://discuss.kotlinlang.org/u/ivan.kubyshkin)\
**Post date:** [February 25, 2020, 6:34pm UTC](https://discuss.kotlinlang.org/t/using-functions-as-lamdas/16439/5 "2020-02-25T18:34:30Z")

</div>

For reference: [https://youtrack.jetbrains.com/issue/KT-36940](https://youtrack.jetbrains.com/issue/KT-36940)
