使用PromiseKit时模棱两可的使用恢复错误 [英] Ambiguous use of recover error while using PromiseKit

查看:230
本文介绍了使用PromiseKit时模棱两可的使用恢复错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理恢复操作时可能引发的错误时,使用恢复时会遇到一个奇怪的错误.

Running into a strange error when using recover while handling errors that can be thrown while executing a promise.

如果.recover块中有多个语句,则用..chain束缚.recover,然后编译.

Chaining .recover with .then results in compilation if there is more than one statement in recover block.

在recover块中只有一个语句可以正常工作,而在单独的情况下可以恢复(promise.recover {},则不行)

Having single statement in recover block works and having recover alone (promise.recover{} without then works)

附加单条语句恢复(有效)和多条语句恢复(其引发编译错误,并显示以下消息的屏幕截图):歧义使用restore(on:__:)

Attaching screenshots of single statement recover (which works) and multiple statement recover (which throws a compilation error with the message: Ambiguous use of recover(on:__:)

任何有关调试方法的帮助都将不胜感激.

推荐答案

recover可以返回Promise.如果您的recovery块中只有1条语句,则编译器不会抱怨,因为只有一行可能返回任何内容.当您添加第二条语句时,编译器无法推断出哪一行返回了某些内容.明确表示您将要返回Void,这是一个可能的解决方法,假设您实际上并不打算返回任何内容.

recover can return a Promise. If there is only 1 statement in your recover block then the compiler won't complain because there is only one line that could possibly return anything. When you add a second statement the compiler cannot infer which line returns something. Explicitly indicating that you are returning Void is one possible fix, assuming you don't intend to actually return anything.

func getNext() {
    taskGroup.getNext().then { data in
        self.initViewWithTask(data as! Task)
    }.recover { error -> Void in
        print("in recover")
        print("in recover 2")
    }
}

说明:

这四个示例在功能上相同,并且将在最后打印One : Two : Three.第一个示例中的闭包是显式的,它将接收什么参数和类型以及返回什么类型.随后的每个示例对正在发生的事情越来越不明确,因此编译器必须推断/推断正在发生的事情.编译器可以弄清楚发生了什么,因为这些示例非常简单.

These four examples are functionally the same and will print One : Two : Three at the end. The closure in the first example is explicit in what parameter+type it will receive and what type be returned. Each subsequent example is less and less explicit about what is happening, so the complier must infer/deduce what is happening. The compiler can figure out what is happening because these examples are rather simple.

firstly {
    return Promise("One")
}.then { (result1: String) -> Promise<String> in
    return Promise("\(result1) : Two")
}.then { (result2: String) -> Promise<String> in
    return Promise("\(result2) : Three")
}.then { (result3: String) -> Void in
    print(result3)
    return
}

firstly {
    Promise("One")
}.then { (result1: String) -> Promise<String> in
    Promise("\(result1) : Two")
}.then { (result2: String) -> Promise<String> in
    Promise("\(result2) : Three")
}.then { (result3: String) -> Void in
    print(result3)
}

firstly {
    Promise("One")
}.then { result1 -> Promise<String> in
    Promise("\(result1) : Two")
}.then { result2 -> Promise<String> in
    Promise("\(result2) : Three")
}.then { result3 -> Void in
    print(result3)
}

firstly {
    Promise("One")
}.then { result1 in
    Promise("\(result1) : Two")
}.then { result2 in
    Promise("\(result2) : Three")
}.then { result3 in
    print(result3)
}

现在将第二行添加到第一个闭包中.编译器会很困惑,因为它不知道返回什么.它可能是999One.即使像您一样添加简单的print语句,也会使编译器感到困惑,并且会抱怨ambiguous something.因此,要么编译器需要变得更聪明(在简单的print语句中肯定可以做到),要么您需要更清楚地了解正在发生的事情.

Now add a second line to the first closure. The compiler will be confused because it doesn't know what to return. It could be 999 or One. Even adding a simple print statement as you did will confuse the compiler and it will complain about ambiguous something. So either the compiler needs to get smarter (which it certainly could in the case of a simple print statement) or you need to be more explicit as to what is happening.

// ambiguous
firstly {
    Promise(999)
    Promise("One")
}.then { result1 in
    Promise("\(result1) : Two")
}.then { result2 in
    Promise("\(result2) : Three")
}.then { result3 in
    print(result3)
}

// clear
firstly {
    Promise(999)
    return Promise("One")
}.then { (result1: String) in
    Promise("\(result1) : Two")
}.then { result2 in
    Promise("\(result2) : Three")
}.then { result3 in
    print(result3)
}

作为旁注...这确实与PromiseKit没有任何关系.这些示例可以在没有PromiseKit的情况下编写.只是了解Swift编译器现在如何解释闭包.

As a side note... this really doesn't have anything to do with PromiseKit specifically. The examples could be written without PromiseKit. It is just understanding how the Swift complier interprets closures right now.

这篇关于使用PromiseKit时模棱两可的使用恢复错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆