swift 常量推断为 () 类型,这可能是意外的
swift constant inferred to have type () which may be unexpected
我有以下代码:
public func test_mutex() async
{
let mutex = Mutex()
async let a = mutex.exclusive("a") { await Tasks.after(ms: 300) {print("a")}}
async let b = mutex.exclusive("b") { await Tasks.after(ms: 200) {print("b")}}
async let c = mutex.exclusive("c") { await Tasks.after(ms: 100) {print("c")}}
_ = await [a, b, c]
}
我遇到了三个这样的错误:
Constant 'a' inferred to have type '()', which may be unexpected
然而 - 重点是我正在测试一个 return 什么都没有的函数 - 我有另一个测试来测试一个 return 有东西的函数 - 即 () 并不意外根本 - 而且我不能只做东西 return 东西。
如何消除警告? - 或者我如何以其他方式导致三个异步事件并行发生然后等待它们。
how do I otherwise cause three async things to happen in parallel and then await them
您使用 任务组,如结构化并发视频中所述。
https://developer.apple.com/documentation/swift/3814991-withtaskgroup?changes=__1
每次调用组的 async
方法都是您调用异步方法的机会 运行 并行。当所有调用都完成后,任务上的 await
完成,我们继续。
或者,我想您可以按照 FixIt 的建议将 a、b 和 c 的类型声明为 ()
。但这似乎是一种廉价的方法。最好正确执行此操作。
例如,我编写了一个行为与我想象的 Mutex 行为类似的东西:
class Waiter {
func go(after:TimeInterval, f: @escaping ()->()) async {
await withUnsafeContinuation { (c:UnsafeContinuation<Void,Never>) in
DispatchQueue.global().asyncAfter(deadline:.now()+after) {
f()
c.resume()
}
}
}
}
然后编译,运行没问题:
let w = Waiter()
async let x:() = w.go(after: 1) { print(1) }
async let y:() = w.go(after: 1) { print(2) }
async let z:() = w.go(after: 1) { print(3) }
let _ = [await x, await y, await z]
不过,我还是觉得任务组比较好。
let w = Waiter()
print("start")
await withTaskGroup(of:Void.self) { group in
for i in [1,2,3] {
group.async {
await w.go(after:1) { print(i) }
}
}
}
print("done")
我有以下代码:
public func test_mutex() async
{
let mutex = Mutex()
async let a = mutex.exclusive("a") { await Tasks.after(ms: 300) {print("a")}}
async let b = mutex.exclusive("b") { await Tasks.after(ms: 200) {print("b")}}
async let c = mutex.exclusive("c") { await Tasks.after(ms: 100) {print("c")}}
_ = await [a, b, c]
}
我遇到了三个这样的错误:
Constant 'a' inferred to have type '()', which may be unexpected
然而 - 重点是我正在测试一个 return 什么都没有的函数 - 我有另一个测试来测试一个 return 有东西的函数 - 即 () 并不意外根本 - 而且我不能只做东西 return 东西。
如何消除警告? - 或者我如何以其他方式导致三个异步事件并行发生然后等待它们。
how do I otherwise cause three async things to happen in parallel and then await them
您使用 任务组,如结构化并发视频中所述。
https://developer.apple.com/documentation/swift/3814991-withtaskgroup?changes=__1
每次调用组的 async
方法都是您调用异步方法的机会 运行 并行。当所有调用都完成后,任务上的 await
完成,我们继续。
或者,我想您可以按照 FixIt 的建议将 a、b 和 c 的类型声明为 ()
。但这似乎是一种廉价的方法。最好正确执行此操作。
例如,我编写了一个行为与我想象的 Mutex 行为类似的东西:
class Waiter {
func go(after:TimeInterval, f: @escaping ()->()) async {
await withUnsafeContinuation { (c:UnsafeContinuation<Void,Never>) in
DispatchQueue.global().asyncAfter(deadline:.now()+after) {
f()
c.resume()
}
}
}
}
然后编译,运行没问题:
let w = Waiter()
async let x:() = w.go(after: 1) { print(1) }
async let y:() = w.go(after: 1) { print(2) }
async let z:() = w.go(after: 1) { print(3) }
let _ = [await x, await y, await z]
不过,我还是觉得任务组比较好。
let w = Waiter()
print("start")
await withTaskGroup(of:Void.self) { group in
for i in [1,2,3] {
group.async {
await w.go(after:1) { print(i) }
}
}
}
print("done")