Swift: Compiler's conversion from type to optional type -


it looks compiler automatically converts type optional type when needed, though there no inheritance relationship here.

where in documentation behavior specified?

func test(value: string?) {     // string passed in optional string instead.     print(value ?? "") }  // pass actual string test(value: "test") 

this behaviour explicitly documented in well-hidden corner of docs folder of swift github repo.

citing swift/docs/archive/langref.html [changed formatting; emphasis mine]:

types

type ::= attribute-list type-function type ::= attribute-list type-array  ...  type-simple ::= type-optional 

swift has small collection of core datatypes that built compiler. user-facing datatypes defined standard library or declared user defined types.

...

optional types

similar constructs exist in haskell (maybe), boost library (optional), , c++14 (optional).

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

an optional type syntactic sugar library type optional<t>. enum 2 cases: none , some, used represent value may or may not present.

swift provides number of special, builtin behaviors involving library type:

  • there implicit conversion type t corresponding optional type t?.

...

see htmlpreview.github.io rendering of html easier overview of docs .html source:


now, me speculating, reason why not publicly available (and not entirely date; placed in archive sub-folder, , still using old none , some cases rather none , some, respectively) because swift team (no longer?) see reason general swift users know details regarding compiler "magic" associated special type optional, rather focuses on use cases , grammar (in context of swift language , not compiler) of optional.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -