IIFE in kotlin?
# announcements
c
IIFE in kotlin?
t
I don't think you'll need such idiom in Kotlin. To me, the purpose of IIFE is to restrict the visibility of variables defined in its scope. Since all variables in Kotlin are only visible inside their declaration block, this should not be a problem. The closest to an IIFE in Kotlin would be the following:
Copy code
val result: Int = { param: String ->
    // Do something that returns an Int. All local variables in this scope are private, and you can reference variables from the outside.
}("Hello World!")
Please note that this is really a weird syntax for Kotlin. If all you need is to avoid polluting the global scope, use the
run
function:
Copy code
val result = run {
    val foo = "I'm local"
    foo.toUppercase()
}