ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [AOS] Kotlin ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜ (Scope Function)
    2023. 4. 28. 10:26
    ๋ฐ˜์‘ํ˜•

     

     

    ๐Ÿฑ
    ์˜ค์†Œ๋ฆฌ๋‹˜, if ๋ฌธ depth๊ฐ€ ๋„ˆ๋ฌด ๊นŠ์€๋ฐ scope ํ•จ์ˆ˜๊ฐ™์€๊ฑฐ๋กœ ์ค„์—ฌ์ฃผ์„ธ์š”~~
    (ํ—‰, if ๋ฌธ ๊นŠ์ด๋„ ๊ณ ๋ คํ•ด์•ผํ•˜๋Š”๊ตฌ๋‚˜...!) ๋„ต ์•Œ๊ฒ ์Šต๋‹ˆ๋‹ค!
    ๐Ÿถ

     

     

     

     

     

    ๐Ÿ’ก ํ•œ ๋ฒˆ ์ฝ”๋“œ๋ฆฌ๋ทฐ์—์„œ if ๋ฌธ์˜ depth๋ฅผ ์‹ ๊ฒฝ ์จ๋‹ฌ๋ผ๋Š” ์ฝ”๋ฉ˜ํŠธ๋ฅผ ๋ฐ›์€ ์ดํ›„๋กœ, if ๋ฌธ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๋•Œ๋งˆ๋‹ค depth๋ฅผ ์ œ์ผ ์‹ ๊ฒฝ ์“ฐ๊ฒŒ ๋˜์—ˆ๋‹ค.
      if ๋ฌธ์˜ depth๋ฅผ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๊ฒ ์ง€๋งŒ ๋‚˜์˜ ๊ฒฝ์šฐ scope ํ•จ์ˆ˜๋ฅผ ์• ์šฉํ•œ๋‹ค.

     

     

    Scope ํ•จ์ˆ˜

     

    [ ๊ณต์‹๋ฌธ์„œ ]

     

    Scope functions | Kotlin

     

    kotlinlang.org

     

     

    The Kotlin standard library contains several functions whose sole purpose is to execute a block of code within the context of an object. When you call such a function on an object with a lambda expression provided, it forms a temporary scope. In this scope, you can access the object without its name. Such functions are called scope functions. There are five of them: letrunwithapply, and also.

     

     

     

    ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜ (Scope Function)๋ž€, ํŠน์ • ๊ฐ์ฒด์˜ ์ปจํ…์ŠคํŠธ ์•ˆ์—์„œ ํŠน์ • ๋™์ž‘(์†์„ฑ ์ดˆ๊ธฐํ™”, ํ™œ์šฉ ๋“ฑ)์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์  ๋งŒ์„ ๊ฐ€์ง„ ํ•จ์ˆ˜๋ฅผ ๋งํ•œ๋‹ค.

    ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ๋žŒ๋‹ค(lambda) ํ‘œํ˜„์‹์„ ํ˜•์„ฑํ•˜๋ฉฐ, ์ด ๋žŒ๋‹ค์‹ ์•ˆ์—์„œ๋Š” ๊ฐœ์ฒด ์ด๋ฆ„ ์—†์ด ๊ฐœ์ฒด์— ์—‘์„ธ์Šค ํ•  ์ˆ˜ ์žˆ๋‹ค. ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜์˜ ์ข…๋ฃŒ์—๋Š” let, run, with, apply, also๊ฐ€ ์žˆ๋‹ค.

     

     

    1. let

    • it์„ ์‚ฌ์šฉํ•ด ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผ
    • ์ตœ์ข… ์‹คํ–‰ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜ (๋งˆ์ง€๋ง‰ ์ค„ == ๋ฐ˜ํ™˜๊ฐ’)
    fun main() {
        val info = Info("์˜ค์†Œ๋ฆฌ", "tistory", 1)
        
        val scope = info.let {
            println("${it.name}๋ผ๋Š” ์ด๋ฆ„์˜ ${it.blog} ๋ธ”๋กœ๊ฑฐ") // ์˜ค์†Œ๋ฆฌ๋ผ๋Š” ์ด๋ฆ„์˜ tistory ๋ธ”๋กœ๊ฑฐ
            it.num = 10
            it.num
        }
        
        println(scope) // 10
    }
    
    class Info(var name: String, var blog: String, var num: Int) {
        fun plus() {
            num += 1
        }
    }

     

     

    2. run

    • this๋ฅผ ์‚ฌ์šฉํ•ด ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผ (this ์ƒ๋žต ๊ฐ€๋Šฅ)
    • return ๊ฐ’์œผ๋กœ ๊ฐ์ฒด ๋ฐ˜ํ™˜
    fun main() {
        val info = Info("์˜ค์†Œ๋ฆฌ", "tistory", 1)
        
        val scope = info.run {
            println("${name}๋ผ๋Š” ์ด๋ฆ„์˜ ${this.blog} ๋ธ”๋กœ๊ฑฐ") // ์˜ค์†Œ๋ฆฌ๋ผ๋Š” ์ด๋ฆ„์˜ tistory ๋ธ”๋กœ๊ฑฐ
            this.num = 10
            num
        }
        
        println(scope) // 10
    }
    
    class Info(var name: String, var blog: String, var num: Int) {
        fun plus() {
            num += 1
        }
    }

     

     

    3. with

    • this๋ฅผ ์‚ฌ์šฉํ•ด ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผ (์ƒ๋žต ๊ฐ€๋Šฅ)
    • return ๊ฐ’์œผ๋กœ ๊ฐ์ฒด ๋ฐ˜ํ™˜
    fun main() {
        val info = Info("์˜ค์†Œ๋ฆฌ", "tistory", 1)
        
        val scope = with(info) {
            println("${name}๋ผ๋Š” ์ด๋ฆ„์˜ ${this.blog} ๋ธ”๋กœ๊ฑฐ") // ์˜ค์†Œ๋ฆฌ๋ผ๋Š” ์ด๋ฆ„์˜ tistory ๋ธ”๋กœ๊ฑฐ
            this.num = 10
            num
        }
        
        println(scope) // 10
    }
    
    class Info(var name: String, var blog: String, var num: Int) {
        fun plus() {
            num += 1
        }
    }

     

     

    4. apply

    • this๋ฅผ ์‚ฌ์šฉํ•ด ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผ (์ƒ๋žต๊ฐ€๋Šฅ)
    • return ๊ฐ’์œผ๋กœ ์ž๊ธฐ ์ž์‹ ์„ ๋ฐ˜ํ™˜ → ์ดˆ๊ธฐํ™” ์‹œ ์ฃผ๋กœ ์‚ฌ์šฉ
    fun main() {
        val info = Info("์˜ค์†Œ๋ฆฌ", "tistory", 1)
        
        val scope = info.apply {
            println("${name}๋ผ๋Š” ์ด๋ฆ„์˜ ${this.blog} ๋ธ”๋กœ๊ฑฐ") // ์˜ค์†Œ๋ฆฌ๋ผ๋Š” ์ด๋ฆ„์˜ tistory ๋ธ”๋กœ๊ฑฐ
            this.num = 10
            num
        }
        
        println(scope) // Info("์˜ค์†Œ๋ฆฌ", "tistory", 10)
    }
    
    class Info(var name: String, var blog: String, var num: Int) {
        fun plus() {
            num += 1
        }
    }

     

    5. also

    • it์„ ์‚ฌ์šฉํ•ด ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผ
    • return ๊ฐ’์œผ๋กœ ์ž๊ธฐ ์ž์‹ ์„ ๋ฐ˜ํ™˜
    fun main() {
        val info = Info("์˜ค์†Œ๋ฆฌ", "tistory", 1)
        
        val scope = info.also {
            println("${it.name}๋ผ๋Š” ์ด๋ฆ„์˜ ${it.blog} ๋ธ”๋กœ๊ฑฐ") // ์˜ค์†Œ๋ฆฌ๋ผ๋Š” ์ด๋ฆ„์˜ tistory ๋ธ”๋กœ๊ฑฐ
            it.num = 10
            it.num
        }
        
        println(scope) // Info("์˜ค์†Œ๋ฆฌ", "tistory", 10)
    }
    
    class Info(var name: String, var blog: String, var num: Int) {
        fun plus() {
            num += 1
        }
    }

     

     

    ๐Ÿ’ก ์š”์•ฝ

    it ์‚ฌ์šฉ ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜ (๋‚˜๋จธ์ง€๋Š” this)

     

    ์ž๊ธฐ ์ž์‹  ๋ฆฌํ„ด ์Šค์ฝ”ํ”„ ํ•จ์ˆ˜ (๋‚˜๋จธ์ง€๋Š” ๋งˆ์ง€๋ง‰ ์ค„ ๋ฆฌํ„ด)

     

     

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.