Void withCheckedThrowingContinuation Generic parameter 'T' could not be inferred

Viewed 2194

I'm unable to get withCheckedThrowingContinuation to compile when there’s no return type. For example:

    public
    func
    write(toDevice inDeviceID: Int, atAddress inAddr: Int, value inVal: Float)
        async
        throws
    {
        try await withCheckedThrowingContinuation
               // ^ Generic parameter 'T' could not be inferred
        { inCont in
            self.workQ.async
            {
                do
                {
                    self.deviceID = inDeviceID
                    try self.write(address: inAddr, value: inVal)
                    inCont.resume()
                }
                
                catch (let e)
                {
                    inCont.resume(throwing: e)
                }
            }
        }
    }

A version that returns works just fine:

    public
    func
    readRegister(address inAddr: Int, fromDevice inDeviceID: Int)
        async
        throws
        -> UInt16
    {
        try await withCheckedThrowingContinuation
        { inCont in
            self.workQ.async
            {
                do
                {
                    self.deviceID = inDeviceID
                    let r = try self.readRegister(address: inAddr)
                    inCont.resume(returning: r)
                }
                
                catch (let e)
                {
                    inCont.resume(throwing: e)
                }
            }
        }
    }
2 Answers

As I was writing this I found being explicit like this works:

    public
    func
    write(toDevice inDeviceID: Int, atAddress inAddr: Int, value inVal: Float)
        async
        throws
    {
        try await withCheckedThrowingContinuation
        { (inCont: CheckedContinuation<Void, Error>) -> Void in
            self.workQ.async
            {
                do
                {
                    self.deviceID = inDeviceID
                    try self.write(address: inAddr, value: inVal)
                    inCont.resume()
                }
                
                catch (let e)
                {
                    inCont.resume(throwing: e)
                }
            }
        }
    }

I filed a bug with Apple.

I thought I would add another example

extension Something {
    public func sync() async throws {
        // The swift compiler demands that we be explicit where there is no return type.
        try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) -> Void in
            self.sync { result in
                switch result {
                case .success(let entry):
                    continuation.resume(with: .success(entry))
                case .failure(let error):
                    continuation.resume(with: .failure(error))
                }
            }
        }
    }
}
Related