Cannot explicitly specialize a generic function

后端 未结 7 2322
时光取名叫无心
时光取名叫无心 2020-12-04 11:06

I have issue with following code:

func generic1(name : String){
}

func generic2(name : String){
     generic1(name)
}
         


        
7条回答
  •  庸人自扰
    2020-12-04 11:24

    I had a similar problem with my generic class function class func retrieveByKey(key: String) -> T?.

    I could not call it let a = retrieveByKey(key: "abc") where Categories is a subclass of GrandLite.

    let a = Categories.retrieveByKey(key:"abc") returned GrandLite, not Categories. Generic functions do not infer type based on the class that calls them.

    class func retrieveByKey(aType: T, key: String>) -> T? gave me an error when I tried let a = Categories.retrieveByKey(aType: Categories, key: "abc") gave me an error that it could not convert Categories.Type to GrandLite, even though Categories is a subclass of GrandLite. HOWEVER...

    class func retrieveByKey(aType: [T], key: String) -> T? did work if I tried let a = Categories.retrieveByKey(aType: [Categories](), key: "abc") apparently an explicit assignment of a subclass does not work, but an implicit assigment using another generic type (array) does work in Swift 3.

提交回复
热议问题