https://kotlinlang.org logo
#getting-started
Title
# getting-started
a

Ayla

10/18/2023, 12:29 AM
Why is an error reported here? It works fine in js
v

Vampire

10/18/2023, 12:52 AM
Because Kotlin is not JS
👆 3
😂 1
Ohter language, other rules.
💯 1
Even if you in the end cross-compile to JS
e

ephemient

10/18/2023, 12:53 AM
the name isn't bound until after the declaration. this allows for things like
Copy code
fun foo(x: Int?) {
    val x: Int = x ?: default
the
x
on the right-hand side refers to the
x
from the outer scope, the left-hand side
x
doesn't exist yet.
v

Vampire

10/18/2023, 12:53 AM
Not that you should do such shadowing 😄
e

ephemient

10/18/2023, 12:53 AM
also it would be unsafe in Kotlin to allow that, for example
Copy code
val x = run { x }
to write a self-referential lambda, you need to use an object expression,
Copy code
val obs = object : () -> Unit {
    override fun invoke() {
        signalApplyCallback = this
    }
}
note that the name
obs
still isn't bound yet, but you can refer to the lambda itself as
this
a

Ayla

10/18/2023, 1:09 AM
Thanks