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 typet?
....
see htmlpreview.github.io
rendering of html easier overview of docs .html source:
http://htmlpreview.github.io/?https://github.com/apple/swift/blob/master/docs/archive/langref.html
htmlpreview of
langref.html
@ july 25 2017 (from state information above has been cited)
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
Post a Comment