๋กœ์ผ“๐Ÿพ
article thumbnail

 

์žฌ์‚ฌ์šฉ์„ ์œ„ํ•œ ๊ณผ๋„ํ•œ ์œ ์—ฐ์„ฑ

๋‹ค์–‘ํ•œ ํƒ€์ž…์— ๋™์ผํ•œ ๋กœ์ง์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ ๊ณผ๋„ํ•˜๊ฒŒ ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋ถ€ Any๋กœ ๋ฐ›๋Š” ๊ฒƒ์ด ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

open class Car
class K3 : Car()
class Avante : Car()

//“์ฐจ์— ๋Œ€ํ•œ ์„ค๋ช…์„œ” ํ•จ์ˆ˜
fun getInstruction(car: Array<Any>) {
    println("car = ${car.size}")
}

๋งŽ์ด ๊ทน๋‹จ์ ์ธ ์˜ˆ์ œ์ด๊ธฐ๋Š” ํ•ฉ๋‹ˆ๋‹ค..

open class Fruit
class Apple : Fruit()

fun main() {

    val k3 = K3("k3", "๊ธฐ์•„")
    val avante = Avante("์•„๋ฐ˜๋–ผ", "ํ˜„๋Œ€")

		val fruits: Array<Apple> = arrayOf(Apple())
    getInstruction(fruits) // Type mismatch ์—๋Ÿฌ
}

getIstruction ํ•จ์ˆ˜๋Š” “์ฐจ์— ๋Œ€ํ•œ ์„ค๋ช…์„œ” ์˜๋„๋กœ ๋งŒ๋“  ๊ฒƒ์ด์ง€๋งŒ ์ด๋ฅผ ๋ชจ๋ฅด๊ณ  ๊ณผ์ผ์ธ Apple ํƒ€์ž…์„ ๊ฐ€์ง„ Array๋ฅผ ์ง‘์–ด ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธํ…”๋ฆฌ์ œ์ด๊ฐ€ ์›Œ๋‚™ ๋˜‘๋˜‘ํ•ด์„œ ๋ฏธ๋ฆฌ ์ด๋Ÿฐ ์—๋Ÿฌ๋ฅผ ์žก์•„ ๋‚ด๊ธด ํ•˜์ง€๋งŒ ์žฌ์‚ฌ์šฉ์„ ์œ„ํ•ด Any๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํƒ€์ž… ์•ˆ์ •์„ฑ์„ ์ €ํ•˜์‹œํ‚ฌ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ⇒ ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

 


์ œ๋„ค๋ฆญ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ถˆ๋ณ€์„ฑ

์ž๋ฐ”์—์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๋„ค๋ฆญ์€ ํƒ€์ž… ๋ถˆ๋ณ€์„ฑ์„ ๊ฐ•์š”ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ œ๋„ค๋ฆญ ํ•จ์ˆ˜๊ฐ€ ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž… T ๋ฅผ ๋ฐ›๋Š”๋‹ค๋ฉด T ์˜ ๋ถ€๋ชจ ํด๋ž˜์Šค๋‚˜ ์ž์‹ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ํƒ€์ž…์ด ์ •ํ™•ํžˆ ์ผ์น˜ํ•ด์•ผ๋งŒ ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ์— ์ œ์•ฝ์€ ํฌ๊ฒŒ ๊ฑธ์ˆ˜๋ก ์•ˆ์ •์„ฑ์€ ๋†’์•„์ง„๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

open class Car
class K3 : Car()
class Avante : Car()

fun getInstruction(car: Array<Car>) {
    println("car = ${car.size}")
}
fun main() {
    val cars = arrayOf(K3())

    getInstruction(cars)// Type mismatch ์—๋Ÿฌ
}

์ œ๋„ค๋ฆญ์€ ๋ถˆ๋ณ€์„ฑ์ด๋ฏ€๋กœ ๋ถ€๋ชจ ํด๋ž˜์Šค๋“ , ์ž์‹ ํด๋ž˜์Šค๋“  ํƒ€์ž…์ด ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.

fun getInstruction(car: List<Car>) {
    println("car = ${car.size}")
}

fun main() {
    val cars = listOf(K3())

    getInstruction(cars)
}

ํ•˜์ง€๋งŒ car: Array<Car> ๋ฅผ car: List<Car> ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค๋ฉด ์œ„ ์ฝ”๋“œ๋Š” ๋ฌธ์ œ ์—†์ด ์ปดํŒŒ์ผ์ด ๋ฉ๋‹ˆ๋‹ค.

 

์ œ๋„ค๋ฆญ์€ ๊ธฐ๋ณธ์ ์ด๊ณ  ๋ถˆ๋ณ€์„ฑ์ด๊ณ , ํƒ€์ž…์ด ์ •ํ™•์ธ ์ผ์น˜ํ•ด์•ผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š”๋ฐ ์™œ Array<T>๋Š” ์•ˆ๋˜๊ณ , List<T>๋Š” ๋˜๋Š” ๊ฑธ๊นŒ์š”?

 

Array<T> ๋Š” ๊ฐ€๋ณ€(mutable) ์ด์ง€๋งŒ List<T> ๋Š” ๋ถˆ๋ณ€(immutable) ์ž…๋‹ˆ๋‹ค. Array ์˜ ์•„์ดํ…œ์€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์ง€๋งŒ List ์˜ ์•„์ดํ…œ์€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์  ๋•Œ๋ฌธ์— ์•ˆ์ •์„ฑ์ด ๋ณด์žฅ๋œ๋‹ค ์ƒ๊ฐํ•˜์—ฌ getInstruction(car: List<Car>)์—์„œ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋˜ํ•œ Array<T> ๋Š” class Array<T> ๋กœ ์ •์˜๋˜์–ด ์žˆ๊ณ , List<T> ๋Š” interface List<out E> ๋กœ ์ •์˜๋˜์–ด์žˆ๋Š”๋ฐ, ๋ฐ”๋กœ List ์ œ๋„ค๋ฆญ ํƒ€์ž…์— ์‚ฌ์šฉ๋œ out ๋•๋ถ„์ž…๋‹ˆ๋‹ค.

 


<out T> ์œผ๋กœ ๊ณต๋ณ€์„ฑ(covariance) ์‚ฌ์šฉํ•˜๊ธฐ

fun copyFromTo(from: Array<Car>, to: Array<Car>){
    for (i in from.indices){
        to[i] = from[i]
    }
}

fun main() {
    val cars1 = Array<Car>(3) { _ -> Car() }
    val cars2 = Array<Car>(3) { _ -> Car() }
    copyFromTo(cars1, cars2)
}

copyFromTo() ํ•จ์ˆ˜๋Š” from ๋ฐฐ์—ด์˜ ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ to ๋ฐฐ์—ด๋กœ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ด๋•Œ ์ „๋‹ฌ ๋ฐ›์€ ๋‘ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” ๋™์ผํ•˜๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์œ„์™€ ๊ฐ™์ด ๊ตฌํ˜„ ํ•  ๊ฒฝ์šฐ์—๋Š” ๋ณ„ ๋ฌธ์ œ ์—†์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด copyFromTo() ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ Car ํƒ€์ž…์˜ ๋ฐฐ์—ด๋กœ ์ •์˜ํ–ˆ๊ณ , ๊ทธ์— ๋งž์ถฐ ๋„˜๊ฒจ์คฌ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.(๋ถˆ๋ณ€์„ฑ)

fun main() {
    val cars1 = Array<K3>(3) { _ -> K3() }
    val cars2 = Array<Car>(3) { _ -> Car() }
    copyFromTo(cars1, cars2) // type mismatch
}

ํ•˜์ง€๋งŒ ์œ„ ์ฝ”๋“œ๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒ ์ฝ”ํ‹€๋ฆฐ์€ Array<Car> ์ž๋ฆฌ์— Array<K3> ๋ฅผ ์ „๋‹ฌํ•˜์ง€ ๋ชปํ•˜๋„๋ก ๋ง‰๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

(๋ถˆ๋ณ€์„ฑ ์œ„๋ฐฐ)

 

์ด๋•Œ out ์„ ์‚ฌ์šฉํ•˜๋ฉด type mismatch ์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.(๊ณต๋ณ€์„ฑ ์‚ฌ์šฉ)

fun copyFromTo(from: Array<out Car>, to: Array<Car>){
    for (i in from.indices){
        to[i] = from[i]
    }
}

์ด๋•Œ ์ฃผ์˜ ํ• ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ๊ทธ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.

fun copyFromTo(from: Array<out Car>, to: Array<Car>) {
    for (i in from.indices) {
        from[i] = Car() 
    }

		from[0] = K3() // ์–˜!
}

 

Nothing์„ ์š”๊ตฌ

์ธํ…”๋ฆฌ์ œ์ด๋Š” ์ด๋ ‡๊ฒŒ ๋งํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ œ๋„ค๋ฆญ ํƒ€์ž…์— out ํ‚ค์›Œ๋“œ๋ฅผ ๋„ฃ์–ด์ค€ from์€ Nothing ํƒ€์ž…์„ write ํ•ด์ฃผ๊ธธ ์›ํ•˜๋Š”๋ฐ, ์™œ K3 ํƒ€์ž…์„ writeํ•˜๋Š”๊ฐ€?”

 

์ด๋Š” from์ด <out T> ์ œ๋„ค๋ฆญ ํƒ€์ž…์„ ์“ฐ๋ฉด์„œ ๊ณต๋ณ€์„ฑ์„ ๊ฐ€์ง€๊ฒŒ ๋˜์–ด ์ƒ๊ธด ๋ฌธ์ œ์ธ๋ฐ,

๊ณต๋ณ€์„ฑ์„ ๊ฐ€์ง€๊ฒŒ ๋˜๋ฉด ๊ฐ’์— ๋Œ€ํ•œ read๋งŒ ๊ฐ€๋Šฅํ•˜๊ณ , write์ด ๋ถˆ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค.

 


Read๋งŒ ๊ฐ€๋Šฅํ•˜๊ณ  Write๋Š” ๋ถˆ๊ฐ€๋Šฅํ•œ ์ด์œ 

 

read๋งŒ ๊ฐ€๋Šฅํ•œ ์ด์œ 

 

Array<out Car> ํƒ€์ž…์ธ from์€ ๋ถ€๋ชจ๊ฐ€ Car์ธ ๊ฒƒ์„ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ธ์ง€ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด from์˜ ๊ฐ’์„ read ํ•  ๋•Œ์—๋Š” Car, K3, Avante ์ค‘ ํ•˜๋‚˜์ผ ๊ฒƒ๋„ ์•Œ ๊ฒƒ์ด๋ฉฐ, ์ด๋Š” ๋ชจ๋‘๋ฅผ ํฌํ•จํ•˜๋Š” Car๋กœ ํ• ๋‹น ํ•ด ์ค„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ read๋ฅผ ํ•  ๋•Œ์—๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

write๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•œ ์ด์œ 

 

๊ทธ๋Ÿฌ๋‚˜ write์˜ ๊ฒฝ์šฐ์—๋Š” ์ข€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๊ณต๋ณ€์„ฑ์„ ์‚ฌ์šฉํ•œ from์—๊ฒŒ ์‹ค์ œ๋กœ๋Š” Array<K3>์„ ๋„˜๊ฒจ์คฌ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ๋ฉ”์†Œ๋“œ์—์„œ from์€ Array<out T>๋กœ ์„ ์–ธ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ, ์‹ค์ œ from์ด Array<Car>์ธ์ง€, Array<K3>์ธ์ง€, Array<Avante>์ธ์ง€ ๋ชจ๋ฆ…๋‹ˆ๋‹ค. from์˜ ์‹ค์ œ Array Type์„ ๋ชจ๋ฅด๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ํ•จ๋ถ€๋กœ ๊ฐ’์„ write ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

 

์ฆ‰, out ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ณต๋ณ€์„ฑ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ์ฝ๊ธฐ๋งŒ(read-only) ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์ฃ .

 

*๊ณต๋ณ€์„ฑ : A ํƒ€์ž…์ด B ํƒ€์ž…์˜ ์„œ๋ธŒ ํƒ€์ž…์ผ ๋•Œ, C<A>๋Š” C<B>์˜ ์„œ๋ธŒํƒ€์ž… ์ž…๋‹ˆ๋‹ค.

read-only ๊ฐ€ ์žˆ๋‹ค๋ฉด ๋ฐ˜๋Œ€๋กœ ์“ฐ๊ธฐ ์ „์šฉ(write-only) ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 


<in T> ์œผ๋กœ ๋ฐ˜๊ณต๋ณ€์„ฑ(contravariance) ์‚ฌ์šฉํ•˜๊ธฐ

๊ณต๋ณ€์„ฑ์„ ์‚ฌ์šฉํ•  ๋•Œ ์ฝ๊ธฐ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•ด์„œ out์„ ์‚ฌ์šฉํ•˜์˜€๋Š”๋ฐ, ๋ฐ˜๊ณต๋ณ€์„ฑ์—์„œ๋Š” ๋ฐ˜๋Œ€๋กœ ์“ฐ๊ธฐ๋งŒ ๊ฐ€๋Šฅํ•˜์—ฌ in์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

fun copyFromTo(from: Array<out Car>, to: Array<Car>){
    for (i in from.indices){
        to[i] = from[i]
    }
}

T ๊ฐ€ Car ํƒ€์ž…์ด๊ฑฐ๋‚˜ Car ์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋ผ๋ฉด ์•„๋ฌด Array<T> ๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์€ ์ •์ƒ์ ์ž…๋‹ˆ๋‹ค. ๊ณต๋ณ€์„ฑ์€ ์ฝ”ํ‹€๋ฆฐ์ด from ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์œ ์—ฐํ•˜๋„๋ก ํ•˜๋Š” ๊ฒŒ ์•ˆ์ „ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•ด์ค๋‹ˆ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋ฒˆ์—๋Š” to ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. to ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํƒ€์ž…์€ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•œ Array<Car> ์ž…๋‹ˆ๋‹ค.

to ํŒŒ๋ผ๋ฏธํ„ฐ์— Array<Car> ์„ ์ „๋‹ฌํ•œ๋‹ค๋ฉด ๋ฌธ์ œ๊ฐ€ ์—†๊ฒ ์ง€๋งŒ, Car ์˜ ๋ถ€๋ชจ ํด๋ž˜์Šค๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” ์–ด๋–จ๊นŒ์š”?

 

์ด๋•Œ in์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

fun main() {
    val cars1 = Array<K3>(3) { _ -> K3() }
    val cars2 = Array<Any>(3) { _ -> Any() }
    copyFromTo(cars1, cars2)// type mismatch
}

fun copyFromTo(from: Array<out Car>, to: Array<Car>){
    for (i in from.indices){
        to[i] = from[i]
    }
}

์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ํ•˜๊ฒŒ ๋˜๋ฉด ์ œ๋„ค๋ฆญ์˜ ๋ถˆ๊ณต๋ณ€์„ฑ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

fun copyFromTo(from: Array<out Car>, to: Array<in Car>){
    for (i in from.indices){
        to[i] = from[i]
    }
}

๋”ฐ๋ผ์„œ in ์„ ๋ถ™์–ด์ฃผ๊ฒŒ ๋˜๋ฉด ์ด๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

to ํŒŒ๋ผ๋ฏธํ„ฐ์— ์›๋ž˜ ์š”์ฒญ๋œ ํƒ€์ž…์ด๋‚˜ ๊ทธ ํƒ€์ž…์˜ ์กฐ์ƒ ํƒ€์ž…์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ๊ถŒํ•œ(๋ฐ˜๊ณต๋ณ€์„ฑ)์„ ์š”์ฒญํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์•„๋ž˜ ์ฝ”๋“œ๋Š” ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ง์”€ ๋“œ๋ฆฐ๊ฒƒ์ฒ˜๋Ÿผ ๋ฐ˜๊ณต๋ณ€์„ฑ์€ Write๋งŒ ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

fun copyFromTo(from: Array<out Car>, to: Array<in Car>){
    for (i in from.indices){
        to[i] = from[i]
    }

    var any: Car = to[0]
}

์ด์œ ๋Š” ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๊ณต๋ณ€์„ฑ์œผ๋กœ ์ธํ•ด ์ œ๋„ค๋ฆญ ํƒ€์ž… ๋˜๋Š” ์กฐ์ƒ ํƒ€์ž…์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜์˜€์œผ๋ฏ€๋กœ,

์ œ๋„ค๋ฆญ ํƒ€์ž…์œผ๋กœ ํ•ด๋‹น ๊ฐ’์„ read ํ•  ๋•Œ ํ•ด๋‹น ๊ฐ’์˜ ํƒ€์ž…์ด ์ œ๋„ค๋ฆญ ํƒ€์ž…์˜ ์กฐ์ƒ ํƒ€์ž…์ผ ๊ฒฝ์šฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

fun copyFromTo(from: Array<out Car>, to: Array<in Car>){
    for (i in from.indices){
        to[i] = from[i]
    }

    from[0] = K3() as Nothing

    var any: Any = to[0] as Any
}

์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด ๊ฐ•์ œ๋กœ ์บ์ŠคํŒ… ํ•ด์ฃผ๋ฉด ๋ฌธ์ œ๋Š” ์—†์Šต๋‹ˆ๋‹ค.

๊ฒฐ๊ตญ ํƒ€์ž…์˜ ์•ˆ์ •์„ฑ์„ ๋ณด์žฅํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š”๋ฐ out, in ์„ ์ด์šฉํ•˜์—ฌ ์ œ๋„ค๋ฆญ์ด ๊ฐ–๋Š” ํƒ€์ž…์— ๋Œ€ํ•œ ์œ ์—ฐ์„ฑ์„ ์กฐ๊ธˆ์ด๋‚˜๋งˆ ๋Š˜๋ ค์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

in,out ์‚ฌ์šฉ ์ด์œ  ⇒ ์ œ๋„ค๋ฆญ์„ ํ†ตํ•ด ํƒ€์ž…์˜ ์•ˆ์ •์„ฑ ์ œํ•œ์„ ์กฐ๊ธˆ ๋Š์Šจํ•˜๊ฒŒ ํ•˜๋ฉด์„œ ์ฝ”๋“œ์˜ ์œ ์—ฐ์„ฑ์„ ์˜ฌ๋ ค์ฃผ๊ธฐ ์œ„ํ•ด!!

profile on loading

Loading...