ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [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

    ๋Œ“๊ธ€

Designed by Tistory.