Home Kotlin - Scope Function (run, with, apply, also, let)
Post
Cancel

Kotlin - Scope Function (run, with, apply, also, let)

run

1
2
3
public inline fun <T, R> T.run(block: T.() -> R): R {
    return block()
}

with

1
2
3
public inline fun <T, R> with(receiver: T, block: T.() -> R): R {
    return receiver.block()
}

apply

1
2
3
4
public inline fun <T> T.apply(block: T.() -> Unit): T {
    block()
    return this
}

also

1
2
3
4
public inline fun <T> T.also(block: (T) -> Unit): T {
    block(this)
    return this
}

let

1
2
3
public inline fun <T, R> T.let(block: (T) -> R): R {
    return block(this)
}

Scope Function의 응용 예시는 여기에서 자세하게 확인할 수 있다.

This post is licensed under CC BY 4.0 by the author.

My Development Environment Settings / 나의 개발 환경 세팅들

Utterances - Applying The Jekyll Blog Comment Feature / 블로그 댓글 적용하기