[Kotlin ๊ธฐ์ดˆ] ์ฝ”ํ‹€๋ฆฐ ๊ธฐ๋ณธ ๋ฌธ๋ฒ• - ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜
๋ฐ˜์‘ํ˜•

 

 

 

10 ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜ (Scope Functions)

 

 

  • ์ฝ”๋“œ๋ฅผ ์ถ•์•ฝํ•ด์„œ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ํ•จ์ˆ˜, ์˜์—ญ ํ•จ์ˆ˜
  • ์‚ฌ์šฉ๋ฒ•์€ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์“ฐ์ง€ ์•Š๊ณ  run, let์ฒ˜๋Ÿผ ๊ด„ํ˜ธ ์—†์ด ์ผ์ข…์˜ ํ‚ค์›Œ๋“œ ๊ฐ™์ด ์‚ฌ์šฉ
  • lateinit๊ณผ ํ•จ๊ป˜ Safe Call ๋‚จ์šฉ์„ ๋ง‰์•„์ฃผ๋Š” ์—ญํ• , ๋งŽ์ด ์‚ฌ์šฉ
  • ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ž„์‹œ๋กœ Scope๋ฅผ ๋งŒ๋“ค์–ด์„œ ํŽธ๋ฆฌํ•œ ์ฝ”๋“œ ์ž‘์„ฑ์„ ๋„์™€์คŒ
  • ์Šค์ฝ”ํ”„ ๋‚ด์—์„œ๋Š” ๊ฐ์ฒด์˜ ์ด๋ฆ„์„ ํ†ตํ•ด ์ผ์ผํžˆ ์ฐธ์กฐํ•  ํ•„์š” ์—†์ด ๊ฐ์ฒด๋ฅผ ์ ‘๊ทผํ•˜๊ณ  ํ•ธ๋“ค๋งํ•˜๋Š” ํŽธ๋ฆฌํ•จ
  • ์ข…๋ฅ˜: run, let, apply, also, with

 


 

 

์Šค์ฝ”ํ”„ ํ•จ์ˆ˜ ์ข…๋ฅ˜

  • ์ž์‹ ์˜ ํ•จ์ˆ˜ ์Šค์ฝ”ํ”„(์ฝ”๋“œ ๋ธ”๋ก) ์•ˆ์—์„œ ํ˜ธ์ถœํ•œ ๋Œ€์ƒ์„  this ํ˜น์€ it๋กœ ๋Œ€์ฒดํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

 

 

let

var list = mutableListOf("Scope", "Function")
list.let { // it -> ์ƒ๋žต๋œ ํ˜•ํƒœ. it -> ๋Œ€์‹ ์— target -> ๋“ฑ์œผ๋กœ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
    val listSize = it.size // ๋ชจ๋“  ์†์„ฑ๊ณผ ํ•จ์ˆ˜๋ฅผ it.๋ฉค๋ฒ„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
    println("๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด let = $listSize")
}

// MutableList์˜ size ์†์„ฑ์„ let ์Šค์ฝ”ํ”„ ์•ˆ์—์„œ it.size๋กœ ํ˜ธ์ถœ
	var strNum = "10"

    var result = strNum?.let { // ์ค‘๊ด„ํ˜ธ ์•ˆ์—์„œ๋Š” it์œผ๋กœ ํ™œ์šฉํ•จ
        Integer.parseInt(it)   // ์—ฌ๊ธฐ์„œ์˜ it์€ strNum
    }

    println(result!!+1)
  • ํ•จ์ˆ˜ ์˜์—ญ ์•ˆ์—์„œ ํ˜ธ์ถœํ•œ ๋Œ€์ƒ์„ it์œผ๋กœ ์‚ฌ์šฉ
  • it์€ ์ƒ๋žต ๋ถˆ๊ฐ€. ๋‹ค๋งŒ target ๋“ฑ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Œ

 

 

with

	var alphabets = "abcd"

    with(alphabets) {
        var result = subSequence(0,2)   // this.subSequence(0,2)์ง€๋งŒ this. ์ƒ๋žต
        println(result)
    }
  • ์ค‘๊ด„ํ˜ธ ๋ธ”๋ก์•ˆ์— this๋กœ ์ž์‹ ์˜ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰
  • this๋Š” ์ƒ๋žตํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋ฐ˜๋“œ์‹œ null์ด ์•„๋‹๋•Œ๋งŒ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์ข‹์Œ (run๊ณผ ๋‹ฌ๋ฆฌ null์ฒดํฌ ๋ถˆ๊ฐ€)

 

 

run

var list = mutableListOf("Scope", "Function")
list.run {
    val listSize = size
    println("list์˜ ๊ธธ์ด run = $listSize")
}

//MutableList๋ฅผ run ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์Šค์ฝ”ํ”„๋ฅผ ์ง€์ •ํ•œ ํ›„, ๋‚ด๋ถ€์—์„œ size ํ”„๋กœํผํ‹ฐ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœ
// ๊ฐ์ฒด์—์„œ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

		var totalPrice = run {
        var computer = 10000
        var mouse = 5000

        computer+mouse
    }
    println("์ด ๊ฐ€๊ฒฉ์€ ${totalPrice}์ž…๋‹ˆ๋‹ค")
//๊ฐ์ฒด์—์„œ ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ

fun main() {
    var student = Student("์ฐธ์ƒˆ", 10)
    student?.run {     // ๋˜‘๊ฐ™์ด this๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋Š” with๊ณผ ๋‹ฌ๋ฆฌ null์ฒดํฌ ๊ฐ€๋Šฅ
        displayInfo()     // student.displayInfo() ์ง€๋งŒ this.์„ ์ƒ๋žตํ•จ
    }
}

class Student(name: String, age: Int) {
    var name: String
    var age: Int
    
    init {
        this.name = name
        this.age = age
    }
    
    fun displayInfo() {
        println("์ด๋ฆ„์€ ${name} ์ž…๋‹ˆ๋‹ค")
        println("๋‚˜์ด๋Š” ${age} ์ž…๋‹ˆ๋‹ค")
    }
}
  • ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜ ์•ˆ์—์„œ ํ˜ธ์ถœํ•œ ๋Œ€์ƒ์„ this๋กœ ์‚ฌ์šฉ
  • with์™€ ๋‹ฌ๋ฆฌ null์ฒดํฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋”์šฑ ์•ˆ์ „ํ•˜๊ฒŒ ์‚ฌ์šฉ๊ฐ€๋Šฅ
  • ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•œ ํšจ๊ณผ์ด๊ธฐ ๋•Œ๋ฌธ์—, this๋ฅผ ์ƒ๋žตํ•˜๊ณ  ๋ฉ”์„œ๋“œ๋‚˜ ํ”„๋กœํผํ‹ฐ ๋ฐ”๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

 

 

also

 

fun main() {
    var student = Student("์ฐธ์ƒˆ", 10)

    var result = student?.also {
        it.age = 50
    }
    result?.displayInfo()
    student.displayInfo()
}

class Student(name: String, age: Int) {
    var name: String
    var age: Int

    init {
        this.name = name
        this.age = age
    }

    fun displayInfo() {
        println("์ด๋ฆ„์€ ${name} ์ž…๋‹ˆ๋‹ค")
        println("๋‚˜์ด๋Š” ${age} ์ž…๋‹ˆ๋‹ค")
    }
}
  • ์ค‘๊ด„ํ˜ธ ๋ธ”๋ก์•ˆ์— it์œผ๋กœ ์ž์‹ ์˜ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
  • apply์™€ ํ•จ๊ป˜ ์ž์ฃผ ์‚ฌ์šฉ

 

 

 

apply

 

fun main() {
    var student = Student("์ฐธ์ƒˆ", 10)

    var result = student?.apply {
        student.age = 50
    }
    result?.displayInfo()
    student.displayInfo()
}

class Student(name: String, age: Int) {
    var name: String
    var age: Int
    
    init {
        this.name = name
        this.age = age
    }
    
    fun displayInfo() {
        println("์ด๋ฆ„์€ ${name} ์ž…๋‹ˆ๋‹ค")
        println("๋‚˜์ด๋Š” ${age} ์ž…๋‹ˆ๋‹ค")
    }
}
  • ์ค‘๊ด„ํ˜ธ ๋ธ”๋ก์•ˆ์— this๋กœ ์ž์‹ ์˜ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
  • ์ฃผ๋กœ ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ ๋ณ€ํ™”์‹œํ‚ค๊ณ  ๋ฐ”๋กœ ์ €์žฅํ•˜๊ณ  ์‹ถ์„๋•Œ ์‚ฌ์šฉ

 

 


 

 

this์™€ it์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๊ธฐ

 

this๋กœ ์‚ฌ์šฉ๋˜๋Š” ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜: run, apply, with

var list = mutableListOf("Scope", "Function")
โ€‹
list.apply {
    val listSize = size
    println("๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด apply = $listSize")
}
โ€‹
with (list){
    val listSize = size
    println("๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด with = $listSize")
}
  • apply์™€ with์˜ ์‚ฌ์šฉ ์˜ˆ์ œ / example.apply, with (example)
  • ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜ ์•ˆ์—์„œ this๋กœ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”์„œ๋“œ๋‚˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœ

 

๐Ÿ’ก ํ˜ธ์ถœ ๋Œ€์ƒ์ด null์ผ ๊ฒฝ์šฐ?

  • with์€ ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜์ง€๋งŒ, ์œ„์˜ 2๊ฐœ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ํ™•์žฅ(Extension) ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋จ
  • ๋”ฐ๋ผ์„œ ํ˜ธ์ถœํ•˜๋Š” ๋Œ€์ƒ์ด null์ผ ๊ฒฝ์šฐ์—๋Š” with๋ณด๋‹ค๋Š” apply๋‚˜ run์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ 
  • target?.apply { /* ์ฝ”๋“œ */ }

 

 

it์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜: let, also

var list = mutableListOf("Scope", "Function")
list.let { target -> // it์„ target ๋“ฑ๊ณผ ๊ฐ™์ด ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
    val listSize = target.size // target์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ๊ธฐ์— ๋ฉค๋ฒ„ ์ ‘๊ทผ์€ target.์†์„ฑ
    println("๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด let = $listSize")
}
โ€‹
list.also {
    val listSize = it.size
    println("๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด also = $listSize")
}
  •  let๊ณผ also์˜ ์‚ฌ์šฉ์˜ˆ์ œ / example.let, example.also

 

 


 

 

๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๊ธฐ

  • ๋™์ผํ•˜๊ฒŒ this๋กœ ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜๋ผ๋„, ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ๊ฒฝ์šฐ์—๋Š” ์šฉ๋„๊ฐ€ ๋‹ฌ๋ผ์ง
  • ๊ฒฐ๊ด๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ๊ฒฝ์šฐ, ์Šค์ฝ”ํ”„๊ฐ€ ์ข…๋ฃŒ๋˜๋Š” ์‹œ์ ์—์„œ์˜ ๋ฐ˜ํ™˜๊ฐ’์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ๋‹ค๋ฅธ ์—ญํ• ์˜ ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”

 

 

ํ˜ธ์ถœ ๋Œ€์ƒ์ธ this ์ž์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜: apply, also

var list = mutableListOf("Scope", "Function")
โ€‹
val afterApply = list.apply {
    add("Apply")
    count()
}
println("๋ฐ˜ํ™˜๊ฐ’ apply = $afterApply") // ๋ฐ˜ํ™˜๊ฐ’ apply = [Scope, Function, Apply]
โ€‹
val afterAlso = list.also {
    it.add("Also")
    it.count()
}
println("๋ฐ˜ํ™˜๊ฐ’ also = $afterAlso") // ๋ฐ˜ํ™˜๊ฐ’ also = [Scope, Function, Apply, Also]
  • apply, also๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ฝ”๋“œ๊ฐ€ ๋ชจ๋‘ ์™„๋ฃŒ๋œ ํ›„ ์ž๊ธฐ ์ž์‹ ์„ ๋˜๋Œ๋ ค ์คŒ
  • ๋‹ค์Œ ์˜ˆ์ œ์—์„œ apply ์Šค์ฝ”ํ”„์˜ ๋งˆ์ง€๋ง‰ ์ค„์—์„œ count()๋ฅผ ํ˜ธ์ถœํ–ˆ์ง€๋งŒ ๋งˆ์ง€๋ง‰ ์ฝ”๋“œ์™€ ์ƒ๊ด€์—†์ด ๊ทธ๋ƒฅ MutableList ์ž์‹ ์„ ๋Œ๋ ค์ฃผ๊ธฐ ๋•Œ๋ฌธ์— Scope, Function์— Apply๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฐ’์ด ์ถœ๋ ฅ๋จ
  • apply ํ•จ์ˆ˜๋Š” ๋žŒ๋‹ค ๋ธ”๋ก์˜ ๋งˆ์ง€๋ง‰ ํ‘œํ˜„์‹์ธ count()๋ฅผ ์‹คํ–‰ํ•˜์ง€๋งŒ ํ•ด๋‹น ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Œ. ๋”ฐ๋ผ์„œ afterApply์—๋Š” count() ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์ด ํฌํ•จ๋˜์ง€ ์•Š์Œ
  • also ํ•จ์ˆ˜๋Š” ๋žŒ๋‹ค ๋ธ”๋ก์˜ ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•จ. ๋”ฐ๋ผ์„œ also ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ count()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ๋„ ํ•ด๋‹น ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜๋˜์ง€ ์•Š๊ณ  ์›๋ž˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•จ.

 

 

๋งˆ์ง€๋ง‰ ์‹คํ–‰ ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜: let, run, with

var list = mutableListOf("Scope", "Function")
โ€‹
val lastCount = list.let {
    it.add("Run")
    it.count()
}
println("๋ฐ˜ํ™˜๊ฐ’ let = $lastCount") // ๋ฐ˜ํ™˜๊ฐ’ let = 3
โ€‹
val lastItem = list.run {
    add("Run")
    get(size-1)
}
println("๋ฐ˜ํ™˜๊ฐ’ run = $lastItem") // ๋ฐ˜ํ™˜๊ฐ’ run = Run
โ€‹
val lastItemWith = with(list){
    add("With")
    get(size-1)
}
println("๋ฐ˜ํ™˜๊ฐ’ with = $lastItemWith") // ๋ฐ˜ํ™˜๊ฐ’ with = With
  • let, run, with์˜ ๊ฒฐ๊ด๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์•ž์˜ 2๊ฐœ์™€๋Š” ์™„์ „ํžˆ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Œ
  • ์ž๊ธฐ ์ž์‹ ์ด ์•„๋‹Œ ์Šค์ฝ”ํ”„์˜ ๋งˆ์ง€๋ง‰ ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜
  • let ํ•จ์ˆ˜:
    let ํ•จ์ˆ˜๋Š” ๋žŒ๋‹ค ๋ธ”๋ก์— ์ˆ˜์‹  ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๊ณ , ๋žŒ๋‹ค ๋ธ”๋ก์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜
    ๋žŒ๋‹ค ๋ธ”๋ก ๋‚ด๋ถ€์—์„œ it์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜์‹  ๊ฐ์ฒด์— ์ ‘๊ทผ
    ์ฒซ ๋ฒˆ์งธ๋กœ ์ˆ˜์‹  ๊ฐ์ฒด์ธ list์— "Run"์„ ์ถ”๊ฐ€ํ•˜๊ณ , ๊ทธ ํ›„์— ์š”์†Œ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
    ๋”ฐ๋ผ์„œ ๋ฐ˜ํ™˜๊ฐ’์€ ์ถ”๊ฐ€ ํ›„์˜ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ ๊ฐœ์ˆ˜์ธ 3
  • run ํ•จ์ˆ˜:
    run ํ•จ์ˆ˜๋Š” ์ˆ˜์‹  ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ
    ๋žŒ๋‹ค ๋ธ”๋ก ๋‚ด๋ถ€์—์„œ this๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜์‹  ๊ฐ์ฒด์— ์ ‘๊ทผ
    run ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋Š” ๋žŒ๋‹ค ๋ธ”๋ก์˜ ๋งˆ์ง€๋ง‰ ํ‘œํ˜„์‹์˜ ๊ฒฐ๊ณผ
    ๋žŒ๋‹ค ๋ธ”๋ก ๋‚ด๋ถ€์—์„œ "Run"์„ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•œ ํ›„, ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
    ๋”ฐ๋ผ์„œ ๋ฐ˜ํ™˜๊ฐ’์€ ์ถ”๊ฐ€ ํ›„์˜ ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ์ธ "Run"
  • with ํ•จ์ˆ˜:
    with ํ•จ์ˆ˜๋Š” ์ˆ˜์‹  ๊ฐ์ฒด์™€ ํ•จ๊ป˜ ํ˜ธ์ถœ
    ๋žŒ๋‹ค ๋ธ”๋ก ๋‚ด๋ถ€์—์„œ๋Š” ์ˆ˜์‹  ๊ฐ์ฒด๋ฅผ this๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ ‘๊ทผํ•  ํ•„์š” ์—†์ด ์ง์ ‘ ์ ‘๊ทผ
    with ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋Š” ๋žŒ๋‹ค ๋ธ”๋ก์˜ ๋งˆ์ง€๋ง‰ ํ‘œํ˜„์‹์˜ ๊ฒฐ๊ณผ
    with ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ "With"๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•œ ํ›„, ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
    ๋”ฐ๋ผ์„œ ๋ฐ˜ํ™˜๊ฐ’์€ ์ถ”๊ฐ€ ํ›„์˜ ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ์ธ "With"

 

 

 


 

 

์ˆ˜์‹ ๊ฐ์ฒด, ๋žŒ๋‹ค์™€์˜ ๊ด€๊ณ„

 

// ์ˆ˜์‹ ๊ฐ์ฒด ์ž์ฒด๋ฅผ ๋žŒ๋‹ค์˜ ์ˆ˜์‹ ๊ฐ์ฒด๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•
public inline fun <T, R> T.run(block: T.() -> R): R
public inline fun <T> T.apply(block: T.() -> Unit): T
public inline fun <T, R> with(receiver: T, block: T.() -> R): R

// ์ˆ˜์‹ ๊ฐ์ฒด๋ฅผ ๋žŒ๋‹ค์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ
public inline fun <T> T.also(block: (T) -> Unit): T
public inline fun <T, R> T.let(block: (T) -> R): R
  • T๋Š” ์ˆ˜์‹ ๊ฐ์ฒด๋ฅผ ์˜๋ฏธ
  • block: ๋‚ด๋ถ€๋Š” ๋žŒ๋‹คํ•จ์ˆ˜์˜ ์†Œ์Šค์ฝ”๋“œ
  • ์ˆ˜์‹ ๊ฐ์ฒด๋Š” it์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ, ๊ทธ๋Ÿฌ๋‚˜ ๋ชจ๋“  ์ˆ˜์‹  ๊ฐ์ฒด๋ฅผ it์œผ๋กœ ํ™œ์šฉํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ์Œ
    Child Function์—์„œ Shadow๊ฐ€ ๋˜์–ด์„œ ์ œ๋Œ€๋กœ ์ฐธ์กฐํ•˜์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ์–ด์š”
    ๋”ฐ๋ผ์„œ it์€ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋ณ€๊ฒฝํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•จ

 

// Scope Function์„ ์ค‘์ฒฉ์œผ๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋ˆ„๊ฐ€ ๋ˆ„๊ตฌ์˜ ๋ฒ”์œ„์ธ์ง€ ์•Œ์ˆ˜ ์—†๋‹ค!
// Implicit parameter 'it' of enclosing lambda is shadowed ๊ฒฝ๊ณ  ๋ฐœ์ƒ!

data class Person(
	var name: String = "",
	var age: Int? = null,
	var child: Person? = null
)

// ์ž˜๋ชป๋œ ์˜ˆ์‹œ
Person().also {
	it.name = "ํ•œ์„๋ด‰"
	it.age = 40
  val child = Person().also {
	  it.name = "ํ™๊ธธ๋™" // ๋ˆ„๊ตฌ์˜ it์ธ์ง€ ๋ชจ๋ฅธ๋‹ค!
    it.age = 10 // ๋ˆ„๊ตฌ์˜ it์ธ์ง€ ๋ชจ๋ฅธ๋‹ค!
  }
  it.child = child
}

// ์ˆ˜์ •ํ•œ ์˜ˆ์‹œ
Person().also {
	it.name = "ํ•œ์„๋ด‰"
	it.age = 40
  val child = Person().also { c ->
	  c.name = "ํ™๊ธธ๋™"
    c.age = 10
  }
  it.child = child
}

 

 

  Scope์—์„œ ์ ‘๊ทผ๋ฐฉ์‹ this Scope์—์„œ ์ ‘๊ทผ๋ฐฉ์‹ it
๋ธ”๋ก ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ run, with let
๊ฐ์ฒด ์ž์‹ ์„ ๋ฐ˜ํ™˜ apply also

 

 

 

 

๋ฐ˜์‘ํ˜•
 ๐Ÿ’ฌ C O M M E N T