๐Ÿ—ป ์ฝ”ํ‹€๋ฆฐ Scope ํ•จ์ˆ˜

๋ฐ•์ค€๊ทœยท2022๋…„ 10์›” 17์ผ
0

Kotlin์‹ ๋ฌธ๋ฒ•: Scope ํ•จ์ˆ˜

์ฝ”ํ‹€๋ฆฐ์˜ scope ํ•จ์ˆ˜๋Š” ๊ฐ์ฒด ์กฐ์ž‘์„ ํ›จ์”ฌ ๋” ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ Kotlin์—์„œ ์ง€์›ํ•˜๋Š” ํ•จ์ˆ˜์˜ ์ผ์ข…์ž„

๋Œ€ํ‘œ์ ์œผ๋กœ

let , run , with , apply , also์ด๋ ‡๊ฒŒ 5๊ฐœ์˜ Scope ํ•จ์ˆ˜๊ฐ€ ์žˆ์Œ

1. let

null์ด ์•„๋‹Œ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•˜๊ณ  ๊ทธ์— ๋”ฐ๋ฅธ ์ƒˆ๋กœ์šด ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•จ. ๋˜ํ•œ lambdaํ•จ์ˆ˜์ฒ˜๋Ÿผ ๋งˆ์ง€๋ง‰์ค„์˜ ๊ฐ’์ด return๋จ. let์„ ์ค‘์ฒฉํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋…์„ฑ์ด ๋ณ„๋กœ ์ข‹์ง€ ์•Š์•„์„œ ์ค‘์ฒฉํ•  ์ผ์ด ์žˆ์œผ๋ฉด, if else๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. it์œผ๋กœ ๊ฐ์ฒด์— ์ ‘๊ทผํ•˜๊ฒŒ ๋จ.

val str: String? = "null"

val result: Int? = str?.let {
    println(it)
    val abc: String? = "abc"
    val def: String? = "def"
    if (!abc.isNullOrEmpty() && !def.isNullOrEmpty()) {
        println("abcdef๊ฐ€ null์ด ์•„๋‹˜")
    }
    1234
}
println(result) // 1234

val str: String? = "null"
val abc: String? = "abc"
val def: String? = "def"

val result2: Int? =  str?.let {
		println(it)
		abc?.let {
				println(it)
				def?.let {
						println(it)
						}
				}
		1234
		}
println(result2) // 1234

2. run

์ผ๋ฐ˜์ ์œผ๋กœ class๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์งœ๊ฒŒ ๋จ.

fun main() {
	val config = DatabaseClient()
	config.url = "localhost:3306"
	config.username = "mysql"
	config.password = "1234"
	val contected = config.connect()
	println(contected)
	// DB ์ ‘์† ์ค‘...
	// DB ์ ‘์† ์™„๋ฃŒ
}

class DatabaseClient {

    var url: String? = null
    var username: String? = null
    var password: String? = null

    // DB์— ์ ‘์†ํ•˜๊ณ  Boolean ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜
    fun connect(): Boolean {
        println("DB ์ ‘์† ์ค‘...")
        Thread.sleep(1000)
        println("DB ์ ‘์† ์™„๋ฃŒ")
        return true
    }
}

์œ„ ์ฝ”๋“œ๋ฅผ run์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐ”๊พธ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Œ. ์ค‘๋ณต๋˜๋Š” ์ฝ”๋“œ ์—†์ด ๋ฐ”๋กœ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ž„. ์•„๋ž˜ ์ฝ”๋“œ ์—ญ์‹œ let์„ ํ†ตํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ it์ด ์ค‘๋ณต๋˜๋ฏ€๋กœ, let๋ณด๋‹ค๋Š” run์„ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ์ข‹๋‹ค. this๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด property์— ์ ‘๊ทผํ•˜๊ฒŒ ๋จ.

fun main() {
	val connected: Boolean = DatabaseClient().run {
    url = "localhost:3306"
    username = "mysql"
    password = "1234"
    connect()
    }
    println(connected)
	// DB ์ ‘์† ์ค‘...
	// DB ์ ‘์† ์™„๋ฃŒ
}

3. with

with๋Š” ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜์—†์ด ๋‚ด๋ถ€์—์„œ ์ˆ˜์‹ ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•  ๋•Œ ์‚ฌ์šฉํ•จ. ์ด ์—ญ์‹œ lambdaํ•จ์ˆ˜ ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋งˆ์ง€๋ง‰ ์ค„์ด return๊ฐ’.

fun main() {
	val withEx = "์•ˆ๋…•ํ•˜์„ธ์š”"
	val length = with(withEx) {
    	length
	}
	println(length) // 5

    val connectedUsingWith = with(DatabaseClient()) {
        url = "localhost:3306"
        username = "mysql"
        password = "1234"
        connect()
    }
	println(connectedUsingWith) // DatabaseClient@372f7a8d

}

4. apply

apply๋Š” ์ˆ˜์‹ ๊ฐ์ฒด์˜ property๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ๊ทธ ์ˆ˜์‹ ๊ฐ์ฒด๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. this๋กœ ์‚ฌ์šฉ

val connectedUsingApply: DatabaseClient = DatabaseClient().apply {
        url = "localhost:3306"
        username = "mysql"
        password = "1234"
        connect()
    }
println(connectedUsingApply)

5. also

also๋Š” ๊ฐ์ฒด ๊ทธ ์ž์ฒด์˜ ๋™์ž‘์„ ์ œ์–ดํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

class User(val name: String, val password: String) {
    fun validate() {
        if (name.isNotEmpty() && password.isNotEmpty()) {
            println("๊ฒ€์ฆ์„ฑ๊ณต")
        } else {
            println("๊ฒ€์ฆ์‹คํŒจ")
        }
    }
    fun printName() = println(name)
}

fun main() {

	val user: User = User(name = "tony", password = "1234")
    user.validate()

    User(name = "tony", password = "1234").also {
        it.validate() // "๊ฒ€์ฆ์„ฑ๊ณต"
        it.printName() // "๊ฒ€์ฆ์‹คํŒจ"
    }
}
profile
'๊ฐœ๋ฐœ'์€ '์˜ˆ์ˆ '์ด๊ณ  '์„œ๋น„์Šค'๋Š” '์ž‘ํ’ˆ'์ด๋‹ค

0๊ฐœ์˜ ๋Œ“๊ธ€