๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IOS๐ŸŽ/iOS+Swift

[Swift] Opaque Type์ด๋ž€

by Jouureee 2022. 4. 12.

SwiftUI๋ฅผ ๋ณด๋ฉด

body์— some ํ‚ค์›Œ๋“œ๊ฐ€ ๋ถ™์€๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๊ฒŒ ๋ญ”์ง€ ๊ถ๊ธˆํ•ด์ ธ์„œ ์ฐพ์•„๋ณด๊ณ  ์ •๋ฆฌํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค  ๐Ÿค”

 

์ฐธ๊ณ  ์ž๋ฃŒ : https://docs.swift.org/swift-book/LanguageGuide/OpaqueTypes.html

 

Opaque Types — The Swift Programming Language (Swift 5.6)

Opaque Types A function or method with an opaque return type hides its return value’s type information. Instead of providing a concrete type as the function’s return type, the return value is described in terms of the protocols it supports. Hiding type

docs.swift.org

https://jcsoohwancho.github.io/2019-08-24-Opaque-Type-%EC%82%B4%ED%8E%B4%EB%B3%B4%EA%B8%B0/

 

Opaque Type ์‚ดํŽด๋ณด๊ธฐ

SwiftUI์˜ ์˜ˆ์ œ๋ฅผ ๋ณด๋‹ค๋ณด๋ฉด ๋‚ฎ์„  ๋ฌธ๋ฒ•์ด ๋ˆˆ์— ๋•๋‹ˆ๋‹ค. struct FeatureCard: View { var landmark: Landmark var body: some View { // some? landmark.featureImage .resizable() .aspectRatio(3/2, contentMode: .fit) .overlay(TextOverlay(landmark)) }

jcsoohwancho.github.io

 

Opaque type


๋ฆฌํ„ด๋˜๋Š” ๊ฐ’์˜ ํƒ€์ž… ์ •๋ณด๋ฅผ ์ˆจ๊ธฐ๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ํ•จ์ˆ˜๊ฐ€ ๋ฉ”์†Œ๋“œ๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์„ ์ œ๊ณตํ•˜๋Š” ๋Œ€์‹ ์— ๋ฆฌํ„ด๊ฐ’์€ ํ•ด๋‹น ํƒ€์ž…์ด ์ฑ„ํƒํ•˜๊ณ  ์žˆ๋Š” ํ”„๋กœํ† ์ฝœ ์ธก๋ฉด์—์„œ ์„ค๋ช…๋ฉ๋‹ˆ๋‹ค.


Opaque type์€ ์‹ค์ œ ํƒ€์ž… ์ •๋ณด๋ฅผ ๊ฐ์ถ”์–ด ๋ชจ๋“ˆ๊ณผ ๋ชจ๋“ˆ์˜ ๊ฒฐํ•ฉ์„ฑ์„ ๋‚ฎ์ถ”๋Š”๋ฐ ์œ ์šฉํ•˜๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์‹œ )
๋งŒ์•ฝ ASCII ๊ทธ๋ฆผ ๋ชจ์–‘์„ ๊ทธ๋ฆฌ๋Š” ๋ชจ๋“ˆ์„ ์ž‘์„ฑํ•˜๊ณ  ์žˆ๋‹ค ํ•ฉ์‹œ๋‹ค. 

protocol Shape {
    func draw() -> String
}

struct Triangle: Shape {
    var size: Int
    func draw() -> String {
        var result: [String] = []
        for length in 1...size {
            result.append(String(repeating: "*", count: length))
        }
        return result.joined(separator: "\n")
    }
}
let smallTriangle = Triangle(size: 3)
print(smallTriangle.draw())
// *
// **
// ***

ํ”„๋กœํ† ์ฝœ Shape์„ ์ค€์ˆ˜ํ•˜๋Š” Triangle์€ ์œ„์˜ ์ฝ”๋“œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ด๋ฒˆ์—” ๋ชจ์–‘์„ ์ˆ˜์ง์œผ๋กœ ๋’ค์ง‘๋Š” FippledShape๋ฅผ ์ž‘์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

struct FlippedShape<T: Shape>: Shape {
    var shape: T
    func draw() -> String {
        let lines = shape.draw().split(separator: "\n")
        return lines.reversed().joined(separator: "\n")
    }
}

let flippedTriangle = FlippedShape(shape: smallTriangle)
print(flippedTriangle.draw())
// ***
// **
// *

 

ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์—๋Š” ์ œ์•ฝ์‚ฌํ•ญ์ด ์žˆ๋Š”๋ฐ์š”. FlippedShape์„ ๋ณด๋ฉด <T: Shape>์™€ ๊ฐ™์ด ์ œ๋„ค๋ฆญ ํƒ€์ž…์ด ๋…ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์— ๋˜ ํ•˜๋‚˜๋ฅผ ๋” ๊ฐ์‹ผ๋‹ค๋ฉด ?

struct JoinedShape<T: Shape, U: Shape>: Shape {
    var top: T
    var bottom: U
    func draw() -> String {
        return top.draw() + "\n" + bottom.draw()
    }
}

let joinedTriangles = JoinedShape(top: smallTriangle, bottom: flippedTriangle)
print(joinedTriangles.draw())
// *
// **
// ***
// ***
// **
// *

JoinedShape<FlippedShape<Triangle>, Triangle> ์™€ ๊ฐ™์ด ํƒ€์ž…์ด ์ค‘์ฒฉ๋˜๊ณ  ๋ถˆํ•„์š”ํ•œ ์ฝ”๋“œ๊ฐ€ ๊ณ„์† ์ž‘์„ฑ๋ฉ๋‹ˆ๋‹ค.

Shape ํ”„๋กœํ† ์ฝœ์˜ ์ƒ์„ฑ์— ๋Œ€ํ•œ ๊ตฌ์ฒด์ ์ธ ์ •๋ณด ๋…ธ์ถœ์€ ์ „์ฒด ๋ฐ˜ํ™˜ ํƒ€์ž…์„ ๋ช…์‹œํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ASCII ๊ทธ๋ฆผ ๋ชจ๋“ˆ์˜ ์ธํ„ฐํŽ˜์ด์Šค ์ผ๋ถ€๊ฐ€ ์•„๋‹Œ ์ „์ฒด ํƒ€์ž…์ด ๋…ธ์ถœ ๋ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 
JoinedShape ๋ฐ FlippedShape์™€ ๊ฐ™์€ Wrapper ํƒ€์ž…์€ ๋ชจ๋“ˆ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ค‘์š”ํ•˜์ง€ ์•Š์œผ๋ฉฐ ํ‘œ์‹œ๋˜์ง€ ์•Š์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

ํ•ด๋‹น ๊ตฌ์กฐ์ฒด๋“ค์ด ๋ชจ๋‘ Shape ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์ ์—์„œ ํƒ€์ž…์˜ ๋…ธ์ถœ์€ ๊ฐ์ถ”๊ณ   ๋ชจ๋“ˆ์˜ ์ธํ„ฐํŽ˜์ด์Šค์—์„œ๋Š” Shape์ž„์„ ์•Œ๋ ค์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 



Returning an Opaque Type

 

Opaque Type์„ Generic Type์˜ ์—ญ์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Generic Type์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ์—์„œ ํ•ด๋‹น ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ํƒ€์ž…์„ ์„ ํƒํ•˜๊ณ  ํ•จ์ˆ˜ ๊ตฌํ˜„์—์„œ ์ถ”์ƒํ™”๋œ ๋ฐฉ์‹์œผ๋กœ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 

func max<T>(_ x: T, _ y: T) -> T where T: Comparable { … }


์ด๋Ÿฌํ•œ ์—ญํ• ์€ Opaque๋ฅผ ๋ฆฌํ„ด ํƒ€์ž…์œผ๋กœ ๊ฐ€์ง€๋Š” ํ•จ์ˆ˜์™€๋Š” ์ •๋ฐ˜๋Œ€์ž…๋‹ˆ๋‹ค. Opaque Type ์‚ฌ์šฉํ•˜๋ฉด ํ•จ์ˆ˜ ๊ตฌํ˜„์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ์—์„œ ์ถ”์ƒํ™”๋œ ๋ฐฉ์‹์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์˜ ์œ ํ˜•์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

generic type : ํ˜ธ์ถœ ํ•˜๋Š” ์ชฝ (๊ตฌ์ฒด์ ์ธ ํƒ€์ž… ์ง€์ •) <-> ํ˜ธ์ถœ ๋ฐ›๋Š” ์ชฝ(generic์„ ์‚ฌ์šฉํ•œ ์ถ”์ƒํ™”)
opaque tyoe: ํ˜ธ์ถœ ํ•˜๋Š” ์ชฝ (๋ชจ๋“ˆ๊ฐ„ ๊ฒฐํ•ฉ์„ ์ค„์ด๊ธฐ ์œ„ํ•œ ์ถ”์ƒํ™”) <-> ํ˜ธ์ถœ ๋ฐ›๋Š” ์ชฝ(ํŠน์ • ์ธํ„ฐํŽ˜์ด์Šค๋กœ ํ†ตํ•ฉ๋œ๋‹ค๋Š” ๊ตฌ์ฒด์ ์ธ ํƒ€์ž… ์ง€์ •)

 

struct Square: Shape {
    var size: Int
    func draw() -> String {
        let line = String(repeating: "*", count: size)
        let result = Array<String>(repeating: line, count: size)
        return result.joined(separator: "\n")
    }
}

func makeTrapezoid() -> some Shape {
    let top = Triangle(size: 2)
    let middle = Square(size: 2)
    let bottom = FlippedShape(shape: top)
    let trapezoid = JoinedShape(
        top: top,
        bottom: JoinedShape(top: middle, bottom: bottom)
    )
    return trapezoid
}
let trapezoid = makeTrapezoid()
print(trapezoid.draw())
// *
// **
// **
// **
// **
// *

makeTrapezoid() ํ•จ์ˆ˜๋Š” some Shape์„ ๋ฆฌํ„ด ํƒ€์ž…์œผ๋กœ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•จ์ˆ˜๋Š” Shape ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๋Š” ์–ด๋–ค ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ์ฒด์ ์ธ type์„ ๋ช…์‹œํ•˜์ง€ ์•Š๊ณ  ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ทผ๋ณธ์ ์ธ ๋ถ€๋ถ„๋งŒ์„ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ๋ฆฌํ„ด ๊ฐ’ shape๊ฐ€ ๊ทธ ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ๋งŒ๋“ค์–ด์กŒ๋Š”๋ฐ ์–ด๋–ค ํƒ€์ž…์œผ๋กœ ๋งŒ๋“ค์–ด์กŒ๋Š”์ง€ ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์„ ๋“œ๋Ÿฌ๋‚ด์ง€ ์•Š์€์ฑ„ shape๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

 

func flip<T: Shape>(_ shape: T) -> some Shape {
    return FlippedShape(shape: shape)
}
func join<T: Shape, U: Shape>(_ top: T, _ bottom: U) -> some Shape {
    JoinedShape(top: top, bottom: bottom)
}

let opaqueJoinedTriangles = join(smallTriangle, flip(smallTriangle))
print(opaqueJoinedTriangles.draw())
// *
// **
// ***
// ***
// **
// *

generic๊ณผ opaque๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. flip(), join() ํ•จ์ˆ˜๊ฐ€ Shape๋ฅผ ์ฑ„ํƒํ•˜๋Š” ์–ด๋–ค ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•จ์œผ๋กœ์จ ํƒ€์ž…์ด ๊ฐ€์‹œํ™”๋˜๋Š” ๊ฒƒ์„ ๋ง‰์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 


Opaque Type๊ณผ Protocol Type์˜ ์ฐจ์ด์ 


opaque type์„ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ๊ณผ protocol type์„ ๋ฆฌํ„ด ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ๋น„์Šทํ•ด๋ณด์ž…๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ type identity๋ฅผ ๋ณด์กดํ•˜๋Š”์ง€ ์•„๋‹Œ์ง€์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
opaque type์€ ๋น„๋ก ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ์ž๊ฐ€ ์–ด๋–ค ํƒ€์ž…์ธ์ง€ ๋ณผ์ˆ˜ ์—†์–ด๋„, ํ•˜๋‚˜์˜ ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด protocol type์€ protocol์„ ์ˆœ์‘ํ•˜๋Š” ์–ด๋–ค ํƒ€์ž…์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. 


์ผ๋ฐ˜์ ์œผ๋กœ,  protocol type์ด opaque type๋ณด๋‹ค ๋” ์œ ์—ฐํ•จ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. opaque type์€ ํƒ€์ž…์— ๋Œ€ํ•ด ๊ฐ•๋ ฅํ•œ ๋ณด์žฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. 

์˜ˆ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค.

func protoFlip<T: Shape>(_ shape: T) -> Shape {
    return FlippedShape(shape: shape)
}

Shape๋ผ๋Š” protocol type์„ ๋ฆฌํ„ดํ•˜๋Š” protoFlip ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 
flip() ํ•จ์ˆ˜์™€ ๋‹ฌ๋ฆฌ protoFlip() ํ•จ์ˆ˜๋Š” Shape ํ”„๋กœํ† ์ฝœ์„ ์ˆœ์‘ํ•˜๊ณ  ์žˆ์„๋ฟ, ํ•ญ์ƒ ๊ฐ™์€ ํƒ€์ž…์„ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์—ฌ๋Ÿฌ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ ํ• ์ˆ˜ ์žˆ๋Š” ์œ ๋™์„ฑ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

 

func protoFlip<T: Shape>(_ shape: T) -> Shape {
     if shape is Square {
         return shape
     }
 
     return FlippedShape(shape: shape)
 }

๋งŒ์•ฝ protoFlip ํ•จ์ˆ˜๊ฐ€ ์œ„์™€ ๊ฐ™์ด ์ˆ˜์ •๋˜์—ˆ๋‹ค๋ฉด, Square ๊ฐ์ฒด ๋˜๋Š” FlippedShape ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰ protoFlip()์˜ ๋ช…ํ™•ํ•˜์ง€ ์•Š์€ ๋ฐ˜ํ™˜๊ฐ’์€ ํƒ€์ž…์— ์˜์กดํ•˜๋Š” ๋งŽ์€ ๊ณณ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. 

์˜ˆ๋ฅผ ๋“ค์–ด == ์—ฐ์‚ฐ์ž๋ฅผ ์“ฐ๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. (๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์„ ๊ฐ€์ง€์ง€ ์•Š์œผ๋ฏ€๋กœ !)

let protoFlippedTriangle = protoFlip(smallTriangle)
let sameThing = protoFlip(smallTriangle)
protoFlippedTriangle == sameThing  // Error

๋จผ์ €, ์–‘์ชฝ ํƒ€์ž…์— ๋Œ€ํ•œ ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์„ ๋ณด์žฅํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ํ”„๋กœํ† ์ฝœ์˜ ์ผ๋ถ€ ์ธํ„ฐํŽ˜์ด์Šค๋งŒ ์ด์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค. Shape์— ==์„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋„๋ก Equatable์„ ์ถ”๊ฐ€๋กœ ์ค€์ˆ˜ํ•œ๋‹ค ํ•˜๋”๋ผ๋„ ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ž์‹ ์˜ ํƒ€์ž… ์ •๋ณด๊ฐ€ ํ•„์š”ํ•œ == ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋˜๋‹ค๋ฅธ ๋ฌธ์ œ๋Š” ํ”„๋กœํ† ์ฝœ๋กœ ๋ฐ˜ํ™˜ํ•œ ๊ฐ’์€ ์ค‘์ฒฉํ•ด์„œ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

let flippedTriangle = protoFlip(smallTriangle)
let original = protoFlip(filppedTriangle) // Error!

ํ”„๋กœํ† ์ฝœ ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ ๊ฐ’์€ ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜์ง€ ์•Š์€ ๊ฒƒ์œผ๋กœ ์—ฌ๊ฒจ์ง€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
๊ฒฐ๊ตญ protocol type์€ ๋ช…ํ™•ํ•œ ํƒ€์ž…์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ~ ๋กœ๋ถ€ํ„ฐ ๋ฐœ์ƒํ•˜๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ด์Šˆ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ๋ช…ํ™•ํ•œ ํƒ€์ž…์ด ์žˆ๋Š” opaque type์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

 

protocol Container {
    associatedtype Item
    var count: Int { get }
    subscript(i: Int) -> Item { get }
}
extension Array: Container { }

< protocol type >

// Error: Protocol with associated types can't be used as a return type.
    func makeProtocolContainer<T>(item: T) -> Container {
        return [item]
    }

    // Error: Not enough information to infer C.
    func makeProtocolContainer<T, C: Container>(item: T) -> C {
        return [item]
    }

ํ”„๋กœํ† ์ฝœ Container๊ฐ€ ์—ฐ๊ด€ ํƒ€์ž… Item์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

any Container๋ฅผ ์“ฐ๋ฉด ํ•ด๊ฒฐ๋œ๋‹ค๊ตฌ ํ•˜๋„ค์š” ,,, ํ•˜์ง€๋งŒ ๋„ํ๋จผํŠธ์—์„  some์œผ๋กœ ํ•ด๊ฒฐํ–ˆ๋Š”๋ฐ ๋‘˜์˜ ์ฐจ์ด๊ฐ€ ๋ญ”์ง€๋Š” ์ข€ ๋” ์ฐพ์•„๋ด…์‹œ๋‹ด ...


๋˜ํ•œ ๋‘๋ฒˆ์งธ ํ•จ์ˆ˜๋Š” Container๋ฅผ ์ถ”๋ก ํ•  ์ •๋ณด๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.



<Opaque type ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐ>

func makeOpaqueContainer<T>(item: T) -> some Container {
    return [item]
}
let opaqueContainer = makeOpaqueContainer(item: 12)
let twelve = opaqueContainer[0]
print(type(of: twelve))
// Prints "Int"

 

 


 

์šฐ์„  generic์˜ ๋ฐ˜๋Œ€ ๊ฐœ๋…์ด ํ•„์š”ํ•  ๋•Œ๋„ ์žˆ๋‹ค๋Š”๊ฒƒ์„ ์ฒ˜์Œ ๋Š๊ผˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“ˆ๊ฐ„ ๊ฒฐํ•ฉ์€ ๋ณต์žกํ•จ์„ ์œ ๋ฐœํ•˜๋‹ˆ some ํ‚ค์›Œ๋“œ๋กœ ๋ช…ํ™•ํ•œ ํƒ€์ž…์„ ๋ณด์žฅํ•œ๋‹ค๋Š” ์ ์—์„œ ํ”„๋กœํ† ์ฝœ๊ณผ ์ œ๋„ค๋ฆญ์˜ ์ ˆ์ถฉํ•œ ๊ฐ™๋‹ค๋Š” ๋Š๋‚Œ์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•ด๋ณธ์ ์ด ์—†์–ด ์ž˜ ์™€๋‹ฟ์ง„ ์•Š์œผ๋‹ˆ ๊ฐœ๋…์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ฝ์–ด๋ด์•ผ ํ• ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค ํ—ˆํ—ˆ ์ƒˆ๋กœ์šด ๊ฐœ๋…์„ ๋ฐฐ์›Œ์„œ ๋ฐฐ์›Œ๋„ ๋ฐฐ์›Œ๋„ ๋ถ€์กฑํ•จ์ด ๋์ด ์—†๋Š”๊ฑฐ ๊ฐ™์Šด๋‹ค ,,, any๋„ ํ•œ๋ฒˆ ์ฐพ์•„๋ด์•ผ๊ฒ ๊ตฐ์š” ๊ทธ๋Ÿผ ์ด๋งŒ ~~

๋Œ“๊ธ€