# Kotlin.JS lambda does not compile, fun does

**URL:** <https://discuss.kotlinlang.org/t/kotlin-js-lambda-does-not-compile-fun-does/13462>\
**Category:** JavaScript\
**Created:** [July 12, 2019, 11:42am UTC](https://discuss.kotlinlang.org/t/kotlin-js-lambda-does-not-compile-fun-does/13462 "2019-07-12T11:42:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomschrot](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tomschrot/32/4346_2.png) [@tomschrot](https://discuss.kotlinlang.org/u/tomschrot)\
**Post date:** [July 12, 2019, 11:42am UTC](https://discuss.kotlinlang.org/t/kotlin-js-lambda-does-not-compile-fun-does/13462/1 "2019-07-12T11:42:37Z")

</div>

Hi

There is a strange compiler behavior that I don’t understand:

```
import kotlin.js.Date
import org.w3c.dom.Document

private external val document: Document = definedExternally

fun now(locale: String = "de-DE") =
        Date().toLocaleTimeString(locale)

fun main() {

    println("Hello Kotlin")
    document.title = now("en-US")

    fun onClick(e: dynamic) {
        e.target.innerText = now()
    }

    val obj: dynamic = document.getElementById("_mainDIV")
    obj.innerText = "Hello Kotlin"
    // does complie & run
    // obj.addEventListener("click", ::onClick)

    // does not compile ?
    obj.addEventListener("click"){ e: dynamic ->
        e.target.innerText = now()
    }
}

```

Resulting compiler error is:

> Task :compileKotlin2Js FAILED  
> e: Y:\myWebTest\kotlin\src\main.kt: (38, 9): Expected a value of type dynamic

---

<div class="post-metadata">

**Author:** ![rrlynx](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@rrlynx](https://discuss.kotlinlang.org/u/rrlynx)\
**Post date:** [July 12, 2019, 1:49pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-lambda-does-not-compile-fun-does/13462/2 "2019-07-12T13:49:25Z")

</div>

Your lambda has to return a value and `e.target.innerText = now()` is not an expression.  
Try this:

```auto
obj.addEventListener("click") { e: dynamic ->
    e.target.innerText = now()
    Unit
}

```

---

<div class="post-metadata">

**Author:** ![tomschrot](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tomschrot/32/4346_2.png) [@tomschrot](https://discuss.kotlinlang.org/u/tomschrot)\
**Post date:** [July 12, 2019, 4:08pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-lambda-does-not-compile-fun-does/13462/3 "2019-07-12T16:08:00Z")

</div>

Does compile & run -thank you.  
But I still can’t guess what the point is.  
The ::onClick function also has no return / is Unit.  
Thus, the compiler amends the term automatically -  
and doesn’t a lambda has such a complement?

---

<div class="post-metadata">

**Author:** ![rrlynx](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@rrlynx](https://discuss.kotlinlang.org/u/rrlynx)\
**Post date:** [July 12, 2019, 10:06pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-lambda-does-not-compile-fun-does/13462/4 "2019-07-12T22:06:27Z")

</div>

Inferred type of the handler is actually `(dynamic) -> dynamic`, not `(dynamic) -> Unit`. So you cannot omit return value.
