Swift:编译器从类型到可选类型的转换 [英] Swift: Compiler's conversion from type to optional type

查看:100
本文介绍了Swift:编译器从类型到可选类型的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使在这里没有继承关系,看起来编译器也会在需要时自动将类型转换为可选类型.

It looks like compiler automatically converts a type into an optional type when needed, even though there is no inheritance relationship here.

在文档中的何处指定了此行为?

Where in the documentation is this behavior specified?

func test(value: String?) {
    // String passed in is now an optional String instead.
    print(value ?? "")
}

// Pass an actual string
test(value: "test")

推荐答案

此行为实际上明确记录在Swift github存储库docs文件夹中一个隐藏的角落.

This behaviour is actually explicitly documented in a well-hidden corner of the docs folder of the Swift github repo.

引用 swift/docs/archive/LangRef. html [更改了一些格式; 强调我的]:

Citing swift/docs/archive/LangRef.html [changed some formatting; emphasis mine]:

类型

type ::= attribute-list type-function
type ::= attribute-list type-array

...

type-simple ::= type-optional

Swift具有少量内置的核心数据类型 编译器.大多数面向用户 数据类型由标准库定义或声明为用户 定义的类型.

Swift has a small collection of core datatypes that are built into the compiler. Most user-facing datatypes are defined by the standard library or declared as a user defined types.

...

Haskell中存在类似的构造 (也许), Boost库 (可选), 和C ++ 14 (可选).

Similar constructs exist in Haskell (Maybe), the Boost library (Optional), and C++14 (optional).

type-optional ::= type-simple '?'-postfix

库类型Optional<T>的可选类型是语法糖. 这是enum,有两种情况:NoneSome,用于表示 一个可能存在或可能不存在的值.

An optional type is syntactic sugar for the library type Optional<T>. This is a enum with two cases: None and Some, used to represent a value that may or may not be present.

Swift提供了许多与此相关的特殊内置行为 库类型:

Swift provides a number of special, builtin behaviors involving this library type:

  • 从任何类型T到相应的可选类型T? 都有隐式转换.
  • There is an implicit conversion from any type T to the corresponding optional type T?.

...

请参阅HTML的htmlpreview.github.io呈现,以比.html源代码更容易地概览文档:

See the htmlpreview.github.io rendering of the HTML for easier overview of the docs than the .html source:

  • html LangRef.html在2017年7月25日的预览(上面引用了上面的信息)

    htmlpreview of the LangRef.html at July 25 2017 (from which state the information above has been cited)

    现在,这是我的猜测,但之所以不那么公开(也是也不完全是最新的;放在 archive 子文件夹中,并且仍使用旧的NoneSome的情况,而不是分别为nonesome的情况),可能是因为Swift团队(不再?)看到了一般的Swift用户了解有关编译器魔术"的详细信息的原因.特殊类型Optional,而是专注于Optional的用例和语法(在Swift语言而不是其编译器的上下文中).

    Now, this is me speculating, but the reason why this is not very publicly available (and also not entirely up to date; placed in the archive sub-folder, and still using the old None and Some cases rather than none and some, respectively) is probably because the Swift team (no longer?) see a reason for general Swift users to know details regarding the compiler "magic" associated with the very special type Optional, but rather focuses on the use cases and grammar (in the context of the Swift language and not its compiler) of Optional.

    这篇关于Swift:编译器从类型到可选类型的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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