lambda with receiver, with, apply Kotlin



fun alphabet(): String {
val result = StringBuilder()
for (letter in 'A'..'Z') {
result.append(letter)
}
result.append("\n Now I know the alphabet!")
return result.toString()
}

fun alphabet_with(): String {
return with(StringBuilder()) {
for (letter in 'A'..'Z') {
append(letter)
}
append("\n Now I know the alphabet!")
toString()
}
}

fun alphabet_apply(): String {
return StringBuilder().apply {
for (letter in 'A'..'Z') {
append(letter)
}
append("\n Now I know the alphabet!")
}.toString()
}

fun alphabet_buildString() = buildString {
for (letter in 'A'..'Z') {
append(letter)
}
append("\n Now I know the alphabet!")
}


with 내에서 this. 생략할 수 있음. 외부함수를 명시적으로 지정하려면 this@OuterClass.method()











덧글

댓글 입력 영역