type alias๋ฅผ ๊ณต๋ถํ๋ฉด์ ๋ง์ฃผ์ณค๋ associated type์ ๋ํด ์์๋ณธ๋ค.
Type Constraints
func someFunction<T: SomeClass, U: SomeProtocol>(someT: T, someU: U) {
// function body goes here
}
func findIndex<T: Equatable>(of valueToFind: T, in array:[T]) -> Int? {
for (index, value) in array.enumerated() {
if value == valueToFind {
return index
}
}
return nil
}
generic์ ์ฌ์ฉํ๋๋ฐ ์์ด, ๋ด๋ถ์์ ์ฌ์ฉํ๋ ํ์
์ ์ ์ฝ์ฌํญ์ ๊ฑธ ์ ์๋ค. findIndex
์ ๊ฒฝ์ฐ T Type์ด Equatable์ด์ด์ผ๋ง ์ฌ์ฉ๊ฐ๋ฅํ๋ค.
Associated Types
ํ๋กํ ์ฝ์ ์ ์ํ ๋, ํ๋ ์ด์์ ๊ด๋ จ ์ ํ์ ํ๋กํ ์ฝ ์ ์์ ์ผ๋ถ๋ก ์ฌ์ฉํ ์ ์๋ค. Type Alias์์๋ ์ฌ์ฉํ๋๋ฐ, ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
protocol Container {
associatedtype Item
mutating func append(_ item: Item)
var count: Int { get }
subscript(i: Int) -> Item { get }
}
Container
ํ๋กํ ์ฝ์ ์ค์ํ๋ ๋ชจ๋ ํ์
์, ์ ์ฅํ๋ ๊ฐ์ ํ์
์ ๋ช
์ํด์ผ ํ๊ณ , ์ด ๊ฐ์ ์ปจํ
์ด๋์ ์ถ๊ฐ๋ ์ ์๋ ํ์
์์ ๋ณด์ฅํด์ผํ๋ค. ๋ฟ๋ง ์๋๋ผ subscript๋ฅผ ํตํด ๋ฐํ๋ ์ ์๋ ํ์
์ด์ด์ผ ํจ๋ ๋ณด์ฅํด์ผํ๋ค. ์ด๋ฅผ ์ํด์, Container
ํ๋กํ ์ฝ์ ๊ตฌ์ฒด์ ์ธ ์ปจํ
์ด๋์ ํ์
์ ์์ง ๋ชปํ๋๋ผ๋, ์ปจํ
์ด๋๊ฐ ๊ฐ์ง ์์๋ค์ ํ์
์ ์ธ๊ธํ ํ์๊ฐ ์๋ค. ๋ค์ ๋งํด, Container
ํ๋กํ ์ฝ์ append(_:)
๋ฉ์๋๋ฅผ ํตํด ์ ๋ฌ๋๋ ๊ฐ์ด ์ปจํ
์ด๋์ ์์์ ๊ฐ์ ํ์
์ด์ด์ผ ํ๋ค๋ ๊ฒ๊ณผ ์ปจํ
์ด๋์ subscript
์ญ์ ์ปจํ
์ด๋์ ์์์ ๊ฐ์ ํ์
์ด์ด์ผ ํ๋ค๋ ๊ฒ์ ๋ช
์ํด์ผํ๋ค. ์ด๋ฅผ ์ํด์ Container
ํ๋กํ ์ฝ์ associated type
์ธ Item
์ ํ์ฉํ๋ ๊ฒ์ด๋ค.
struct IntStack: Container {
// original IntStack implementation
var items = [Int]()
mutating func push(_ item: Int) {
items.append(item)
}
mutating func pop() -> Int {
return items.removeLast()
}
// conformance to the Container protocol
typealias Item = Int // ์ถ์ ํ์
Item์ Int๋ก ๋ฐ๊ฟ ์ฌ์ฉํ๊ธฐ ์ํ ๊ตฌ๋ฌธ
mutating func append(_ item: Int) {
self.push(item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> Int {
return items[i]
}
}
IntStack
ํ์
์ Container
ํ๋กํ ์ฝ์ ์ฒดํํ๊ณ ์ธ ๊ฐ์ง ํ์ ์๊ตฌ์ฌํญ์ ์ค์ํ๊ณ ์๊ณ , associatedtype
์ธ Item
์ฌ์ฉํ๊ธฐ ์ํด Int
ํ์
์ ์ฌ์ฉํ๊ณ ์๋ค. typealias Item = Int
๋ ํ๋กํ ์ฝ์ ์ค์ํ๊ธฐ ์ํด์ ์ถ์ ํ์
์ธ Item์ Int ๋ก ๋ฐ๊ฟ ์ฌ์ฉํ๊ธฐ ์ํ ๊ตฌ๋ฌธ์ด๋ค. Swift์ ํ์
์ถ๋ก ๋๋ถ์ ์ด ๊ตฌ๋ฌธ์ ์๋ต ๊ฐ๋ฅํ๋ค.
struct Stack<Element>: Container {
// original Stack<Element> implementation
var items = [Element]()
mutating func push(_ item: Element) {
items.append(item)
}
mutating func pop() -> Element {
return items.removeLast()
}
// conformance to the Container protocol
mutating func append(_ item: Element) {
self.push(item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> Element {
return items[i]
}
}
์์ ๊ฐ์ด generic stack ํ์
์ ํตํด์๋ Container
ํ๋กํ ์ฝ์ ์ค์ํ ์ ์๋ค.
Adding Constraints to an Associated Type
Associated Type์๋ Type Constraint๋ฅผ ๊ฑธ ์ ์๋ค.
protocol Container {
associatedtype Item: Equatable
mutating func append(_ item: Item)
var count: Int { get }
subscript(i: Int) -> Item { get }
}
Using a Protocol in Its Associated Typeโs Constraints
ํ๋กํ ์ฝ์ ์๊ธฐ ์์ ์ ์๊ตฌ์ฌํญ์ ์ผ๋ถ๋ก ํํ๋ ์ ์๋ค. ์๋ฅผ ๋ค์ด, ๋ค์์ Container
ํ๋กํ ์ฝ์ suffix(_:)
๋ฉ์๋๋ฅผ ์ถ๊ฐํ์ฌ ๊ธฐ์กด์ ํ๋กํ ์ฝ์ ๊ฐ์ ํ ์ฝ๋์ด๋ค. suffix(_:)
๋ ์ปจํ
์ด๋์ ๋์์๋ถํฐ ์ฃผ์ด์ง ๊ฐ์์ ์์๋ฅผ ๋ฐํํ์ฌ, Suffix
ํ์
์ ์ธ์คํด์ค์ ์ ์ฅํ๋ ๋ฉ์๋์ด๋ค.
protocol SuffixableContainer: Container {
associatedtype Suffix: SuffixableContainer where Suffix.Item == Item
func suffix(_ size: Int) -> Suffix
}
์ฌ๊ธฐ์ Suffix
๋ associated type์ด๋ฉฐ, ๋๊ฐ์ง ์ ์ฝ ์กฐ๊ฑด์ ๊ฐ์ง๋ค.
SuffixableContainer
๋ฅผ ์ค์ํด์ผ ํ๋ค.- ๊ทธ๋ฆฌ๊ณ ํด๋น type์
Item
์Container
์Item
ํ์ ๊ณผ ๊ฐ์์ผ ํ๋ค.
struct Stack<Element>: Container {
// original Stack<Element> implementation
var items = [Element]()
mutating func push(_ item: Element) {
items.append(item)
}
mutating func pop() -> Element {
return items.removeLast()
}
// conformance to the Container protocol
mutating func append(_ item: Element) {
self.push(item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> Element {
return items[i]
}
}
extension Stack: SuffixableContainer {
func suffix(_ size: Int) -> Stack {
var result = Stack()
for index in (count-size)..<count {
result.append(self[index])
}
return result
}
// Inferred that Suffix is Stack.
}
var stackOfInts = Stack<Int>()
stackOfInts.append(10)
stackOfInts.append(20)
stackOfInts.append(30)
let suffix = stackOfInts.suffix(2)
// suffix contains 20 and 30
์ด ์ฝ๋์์ Stack
์ Suffix
associated type
์ ๋ํ Stack
์ด๊ณ , ๋ฐ๋ผ์ Stack
์ suffix
์์
์ ๋ ๋ค๋ฅธ Stack ์ ๋ฐํํ๋ค.