Skip to content

Latest commit

 

History

History
52 lines (43 loc) · 1.48 KB

Functions expecting lambda with receiver.md

File metadata and controls

52 lines (43 loc) · 1.48 KB

Functions expecting lambda with receiver

Extension function turns into a lambda with a parameter.

Explanations

In Kotlin, we can easily create beautiful APIs around an inconvenient Android API.

// FunctionWithExtensionExample.kt

fun funcWithExtension(extension: UsualClassExample.() -> Unit) {
    val someObject = UsualClassExample(
        param1 = "FunctionsExample.funcWithExtension",
        param2 = 500,
        param3 = 600L,
        param4 = 700.0f,
        param5 = 800.0,
        param6 = false
    )
    someObject.extension()
}

private fun example() {
    funcWithExtension {
        this.param1 = "changed"
        this.param2 = 2
        this.param3 = 3L
        this.param4 = 4.0f
        this.param5 = 5.0
        this.param6 = true
        println("$param1 $param2 $param3 $param4 $param5 $param6")
    }
}

In Swift, the extension function is turned into a lambda, where the first parameter is the receiver:

FunctionWithExtensionKt.funcWithExtension(extension: { usualClassExample in
    usualClassExample.param1 = "changed"
    usualClassExample.param2 = 2
    usualClassExample.param3 = 3
    usualClassExample.param4 = 4.0
    usualClassExample.param5 = 5.0
    usualClassExample.param6 = true
    print("\(usualClassExample.param1) \(usualClassExample.param2) \(usualClassExample.param3) \(usualClassExample.param4) \(usualClassExample.param5) \(usualClassExample.param6)")
})

Table of contents