typealias of generic class in Swift

2019-04-04 05:54发布

问题:

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

class Cars<T> {
  ...
}

typealias SportCars = Cars

but I am getting a compile error as follow Reference to generic type 'Cars' requires argument in <...>

回答1:

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


回答2:

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

struct SportCars<Y> {
  typealias T = Cars<Y>
}

/// Usage:
typealias Foo = SportCars<Int>.T


回答3:

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.



标签: swift xcode6