typealias of generic class in Swift

前端 未结 3 1841
孤城傲影
孤城傲影 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: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
    

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

    class SportCars : Cars {}
    

    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.

提交回复
热议问题