swift - 什么是永不返回类型?

标签 swift types

返回类型为 Neverfunc 有什么作用?

例如:

func addNums() -> Never {

    //my code

}

如果我像这样将返回类型保留为 Void 会有什么不同?

func addNums() -> Void {

    //my code

}

假设我想处理一个 fatalError(如 dpassage 所说);下面的代码就足够了:

print("its an error")
return

Apple 文档说:

The return type of functions that do not return normally, that is, a type with no values.

来源:Developer

这不是 When and how to use @noreturn attribute in Swift? 的重复问题,因为我希望得到更详细的答案,需要如下详细信息:

  1. NeverVoid 作为返回类型的区别的实例

  2. 我们应该采用这些返回类型的条件。

  3. 返回类型也有可能是nil;我也需要那个功能的比较

答案应该集中在差异上。

最佳答案

Never 返回类型在 Swift 3 中引入以替代 @noreturn 键。

查看此提案的理由:
SE-0102 Remove @noreturn attribute and introduce an empty Never type

正如官方文档所解释的那样:

The return type of functions that do not return normally; a type with no values.

Use Never as the return type when declaring a closure, function, or method that unconditionally throws an error, traps, or otherwise does not terminate.

Source: https://developer.apple.com/documentation/swift/never

基本说明:

// The following function is our custom function we would use
// to manually and purposefully trigger crash. In the logs,
// we can specify what exactly went wrong: e.g. couldn't cast something, 
// couldn't call something or some value doesn't exist:
func crashApp() -> Never {
    fatalError("Something very, very bad happened! Crash the app!")
}

@noreturn 的使用细节和优势,由 Erica Sadun 引用:

  • 绝不允许函数或方法抛出:例如() 抛出 -> 从不。抛出允许错误补救的辅助路径,即使在预计不会返回的函数中也是如此。
  • 作为一流的类型,从不以@noreturn 属性无法做到的方式使用泛型。
  • 从不主动阻止函数同时声明返回类型和不返回类型。这是旧系统下的一个潜在问题。

第一个注意事项(关于二次错误修复)可能特别重要。 Never 函数可以有复杂的逻辑并抛出 - 不一定会崩溃。

让我们看看一些有趣的用例以及 NeverVoid 之间的比较

从不

示例 1

func noReturn() -> Never {
    fatalError() // fatalError also returns Never, so no need to `return`
}

func pickPositiveNumber(below limit: Int) -> Int {
    guard limit >= 1 else {
        noReturn()
        // No need to exit guarded scope after noReturn
    }
    return rand(limit)
}

示例 2

func foo() {
    abort()
    print("Should not reach here") // Warning for this line
}

示例 3

func bar() -> Int {
    if true {
        abort() // No warning and no compiler error, because abort() terminates it.
    } else {
        return 1
    }
}

abort() 定义为:

public func abort() -> Never

无效

如果返回 Void,这些示例将不可能实现:

public func abortVoid() -> Void {
    fatalError()
}

func bar() -> Int {
    if true {
        abortVoid() // ERROR: Missing return in a function expected to return 'Int'
    } else {
        return 1
    }
}

并用 abort() 返回 Never 打包它:

func bar() -> Int {
    if true {
        abort() // No ERROR, but compiler sees it returns Never and warns:
        return 2 // Will never be executed
    } else {
        return 1
    }
}

我们使用 Void 告诉编译器没有返回值。应用程序保持运行。

我们使用 Never 告诉编译器不会返回到调用者站点。应用程序运行循环终止。

关于swift - 什么是永不返回类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46902700/

相关文章:

types - 如何将 OCaml 整数类型限制为整数范围?

Swift - 从卷中的最后一张照片更新 UIButton 图像

swift - 向多个 FCM token 发送通知

ios - 尝试将元素连接/添加到 Xcode 7.3 中的 View Controller 时,我收到锁定警报

iphone - 双数据类型+iphone

C++ 类型名称作为 static_assert 中错误报告的文字字符串

typescript - 从联合类型中提取,其中鉴别器也是联合

ios - 在哪里处理电话中断导致的 fatal error ?

ios - 在运行时增加 tableview 高度以包含全部内容

scala - 通过砖墙驱动单例类型