-
[Swift] Type Alias (typealias) (ํ์ ๋ณ์นญ ์ ์ธ)Swift 2022. 3. 23. 09:45
ํ์ ๋ณ์นญ ์ ์ธ (Type alias Declaration) ์ด๋, ๊ธฐ์กด ํ์ ์ ๋ํ ์๋ก์ด ์ด๋ฆ์ ํ๋ก๊ทธ๋จ์ ์ถ๊ฐํ๋ ๊ฒ์ด๋ค.
typealias ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ธํ๋ฉฐ, ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ๋ค.

type alias๊ฐ ์ ์ธ๋๊ณ ๋๋ฉด, ์๋กญ๊ฒ ์์ฑ๋ ์ด๋ฆ (name)์ ๊ธฐ์กด ํ์ (exisisting type) ๋์ ์ ํ๋ก๊ทธ๋จ์์ ์ฌ์ฉ๋ ์ ์๋ค.
๊ธฐ์กด ํ์ ์ ๋ช ๋ช ๋ ํ์ (named type) ๋๋ ๋ณตํฉ ํ์ (compound type) ์ผ ์ ์๋ค.
Type alias๋ ์ ํ์ ์ ๋ง๋๋ ๊ฒ์ด ์๋, ๋จ์ํ ๊ธฐ์กด์ ์กด์ฌํ๋ ํ์ ์ ์ฐธ์กฐํ ์ ์๋๋ก ์๋ก์ด ์ด๋ฆ์ ํ์ฉํ๋ ๊ฒ์ด๋ค.
type alias ์ ์ธ์ generic parameter๋ฅผ ์ด์ฉํ์ฌ ๊ธฐ์กด์ generic type์ ์ด๋ฆ์ ๋ถ์ผ ์ ์๋ค.
type alias๋ ๊ธฐ์กด ํ์ ์ ์ผ๋ถ ๋๋ ๋ชจ๋ generic parameter์ ๋ํ ๊ตฌ์ฒด์ ์ธ ํ์ ์ ์ ๊ณตํ ์ ์๋ค.
typealias StringDictionary<Value> = Dictionary<String, Value> // The following dictionaries have the same type. var dictionary1: StringDictionary<Int> = [:] var dictionary2: Dictionary<String, Int> = [:]type alias๊ฐ generic parameter๋ก ์ ์ธ๋๋ฉด, ํ๋ผ๋ฏธํฐ์ ์ ์ฝ ์กฐ๊ฑด์ ๊ธฐ์กด ํ์ generic parameter์ ์ ์ฝ์กฐ๊ฑด๊ณผ ์์ ํ ์ผ์นํด์ผํ๋ค.
type alias์ ๊ธฐ์กด ํ์ ์ ์๋ก ๋ฐ๊ฟ์ ์ฌ์ฉํ ์ ์๊ธฐ ๋๋ฌธ์, type alias๋ ์ถ๊ฐ์กฑ์ generic constraint (์ ์ฝ ์กฐ๊ฑด)์ ์ ์ฉํ ์ ์๋ค.
typealias DictionaryOfInts<Key: Hashable> = Dictionary<Key, Int>type alias๋ ์ ์ธ์์ ๋ชจ๋ generic parameter๋ฅผ ์๋ตํ์ฌ ๊ธฐ์กด ํ์ ์ generic parameter๋ฅผ ์ ๋ฌํ ์ ์๋ค.
์๋ฅผ ๋ค๋ฉด, ์๋์์ ์ ์ธ๋ Diccionario type alias๋ Dictionary์ ๋์ผํ generic parameter ๋ฐ ์ ์ฝ ์กฐ๊ฑด์ ๊ฐ๋๋ค.
typealias Diccionario = Dictionaryํ๋กํ ์ฝ ์ ์ธ ๋ด์์ type alias๋ ์์ฃผ ์ฌ์ฉ๋๋ ํ์ ์ ๋ ์งง๊ณ ํธ๋ฆฌํ ์ด๋ฆ์ ์ ๊ณตํ ์ ์๋ค.
๋ง์ฝ type alias๊ฐ ์๋ค๋ฉด, sum ํจ์๋ T.Element ๋์ ์ T.Iterator.Element๋ก ์ฐ๊ด๋ ํ์ ์ ์ฐธ์กฐํด์ผํ๋ค.
protocol Sequence { associatedtype Iterator: IteratorProtocol typealias Element = Iterator.Element } func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int { // ... }์์ฝ
- Type alias๋ ๊ธฐ๋ณธ ์๋ฃํ์ ์๋ก์ด ์ด๋ฆ์ ์ถ๊ฐํ๋ ๊ฒ์ด๋ค.
- ์๋ก์ด ์๋ฃํ์ ๋ง๋๋ ๊ฒ์ด ์๋, ์๋ฃํ์ ๋ณ์นญ์ ์ง์ ํ๋ ๊ฒ์ด๋ค.
- Type alias๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ๋ ์ฑ์ด ๋์์ง๋ค.
- exisiting type์ UpperCamelCase๋ฅผ ์ฌ์ฉํ๋๋ก ํ๋ค.
typealias Size = Double // Double ์๋ฃํ์ ์ด๋ฆ์ผ๋ก Double์ ์จ๋ ๋๊ณ , Size๋ฅผ ์จ๋ ๋๋ค. let size: Double = 10.11 // ์ฌ์ด์ฆ๋ฅผ ์ ์ฅํ๋ ์์๋ผ๋ ๊ฒ์ด ๋ช ํํด์ง๋ค. ๊ฐ๋ ์ฑ์ด ๋์์ง๋ค. let size2: Size = 10.12- ํ๋กํ ์ฝ์ ๊ฒฐํฉํ ๋ ์ฌ์ฉํ๊ธฐ๋ ํ๋ค.

Declarations — The Swift Programming Language (Swift 5.6)
Declarations A declaration introduces a new name or construct into your program. For example, you use declarations to introduce functions and methods, to introduce variables and constants, and to define enumeration, structure, class, and protocol types. Yo
docs.swift.org
GRAMMAR OF A TYPE ALIAS DECLARATION
typealias-declaration → attributes opt access-level-modifier opt typealias typealias-name generic-parameter-clause opt typealias-assignment
typealias-name → identifier
typealias-assignment → = type
'Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๊ธฐํ] ์ ๊ท ํํ์ (Regex) (0) 2022.03.31 [Swift] Key Value Observing (KVO) (0) 2022.03.24 [Swift] Delegate, Notification, KVO (0) 2022.03.15 [Swift] Copy On Write (COW) (0) 2022.03.03 [Swift] Access Control (์ ๊ทผ ์ ์ด) (0) 2022.02.28 - Type alias๋ ๊ธฐ๋ณธ ์๋ฃํ์ ์๋ก์ด ์ด๋ฆ์ ์ถ๊ฐํ๋ ๊ฒ์ด๋ค.