swift Void withCheckedThrowingContinuation无法推断泛型参数“T”

amrnrhlw  于 2023-01-12  发布在  Swift
关注(0)|答案(3)|浏览(185)

当没有返回类型时,我无法编译withCheckedThrowingContinuation。例如:

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)
                }
            }
        }
    }

返回的版本工作正常:

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)
                }
            }
        }
    }
w80xi6nr

w80xi6nr1#

在我写这篇文章的时候,我发现像这样直截了当是有效的:

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)
                }
            }
        }
    }

我向苹果公司提交了一个漏洞。

uqdfh47h

uqdfh47h2#

我想我应该再加一个例子

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))
                }
            }
        }
    }
}

更新:Xcode现在可以用async/await continuation封装基于闭包的函数。

djmepvbi

djmepvbi3#

除非苹果在他们这边解决了这个问题,否则我会找到一个更简洁的方法:

public
func
write(toDevice inDeviceID: Int, atAddress inAddr: Int, value inVal: Float)
    async
    throws
{
    let _: () = try await withCheckedThrowingContinuation
    // ^ Letting it know the type we expect
    { 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)
            }
        }
    }
}

相关问题