typealias of generic class in Swift

前端 未结 3 1840
孤城傲影
孤城傲影 2021-02-19 04:29

I am trying to make a typealias of a generic type class as follows

class Cars {
  ...
}

typealias SportCars = Cars

but I am getting a

相关标签:
3条回答
  • 2021-02-19 05:20

    Possible workaround is to wrap type alias into class/struct:

    struct SportCars<Y> {
      typealias T = Cars<Y>
    }
    
    /// Usage:
    typealias Foo = SportCars<Int>.T
    
    0 讨论(0)
  • 2021-02-19 05:23

    I think the farthest you can go with typealias and generics is to create an alias of a specialized type, such as:

    typealias SportsCar = Cars<Int>
    

    If you need a different name for the same generic type, you can just subclass it:

    class SportCars<T> : Cars<T> {}
    

    it's not exactly an alias (you cannot use Cars when SportCars is expected, but the opposite is possible), but in a "controlled" environment it can work. I wouldn't use myself though.

    0 讨论(0)
  • 2021-02-19 05:27

    Right now, you can't do this with generics, as you've discovered.

    typealias Foo = Array
    // Doesn't work: Reference to generic type 'Array' requires argument in <...>
    

    The Swift Programming Language iBook chapter "Type Alias Declaration" doesn't actually state anything about which types cannot be aliased. But it simply looks like that partial types (like generics without placeholders specified) are not allowed.

    If you feel that this something Swift should do, file a Radar (bugreport) with Apple.

    While researching for this answer, I noticed that the partial type problem not only affects typealias but is visible elsewhere as well:

    let foo = Array.self
    // Doesn't work: Cannot convert the expression's type 'Array<T>.Type' to type 'Array<T>.Type'
    // … which is a very confusing error.
    
    var bar: Array.Type
    // Doesn't work: Reference to generic type 'Array' requires arguments in <...>
    
    let bar: Array.Type = Array.self
    // …/usr/bin/swift: Segmentation fault! :-)
    

    All of these work if you specify the placeholder types:

    typealias Foo = Array<Int> // Works
    let foo = Array<Int>.self // Works
    
    0 讨论(0)
提交回复
热议问题