Swift - cannot explicitly specialize a generic function

I am facing a compiler problem. This happens when I use SwiftTask and Async, here's an example:

// - General method

import Async
import SwiftTask

class AsyncTask {
    func background<T>(job: (((Float -> Void), (T -> Void), (NSError -> Void), SwiftTask.TaskConfiguration) -> Void)) -> SwiftTask.Task<Float, T, NSError> {
        return SwiftTask.Task<Float, T, NSError> { (progress: (Float -> Void), fulfill: (T -> Void), reject: (NSError -> Void), configure: SwiftTask.TaskConfiguration) -> Void in
            Async.background {
                job(progress, fulfill, reject, configure)
                return
            }

            return
        }
    }
}

      

Now this compiles, but when I try to use the generic type:

// - Using a common method

let task = AsyncTask.background<MyAwesomeObject> { progress, fulfill, reject, configure in
    let obj = MyAwesomeObject()
    //-- ... do work here
    fulfill(obj)
    return
}

      

Then I get the following error Cannot explicitly specialize generic function

+3


source to share


1 answer


Give the closure an explicit type to fix T

:



let task = AsyncTask.background{ (progress: Float -> Void, fulfill: MyAwesomeObject -> Void, reject: NSError -> Void, configure: SwiftTask.TaskConfiguration) -> Void in
    let obj = MyAwesomeObject()
    //-- ... do work here
    fulfill(obj)
}

      

+2


source







All Articles