1
- :orphan:
2
-
3
1
Literals
4
2
========
5
3
@@ -12,12 +10,12 @@ literals, so let's look at those.
12
10
High-Level View
13
11
---------------
14
12
15
- ::
16
-
17
- window.setTitle("Welcome to Xcode")
13
+ ``` swift
14
+ window. setTitle ( " Welcome to Xcode " )
15
+ ```
18
16
19
- In this case, we have a string literal and an enclosing context. If `` window ` `
20
- is an NSWindow, there will only be one possible method named `` setTitle ` `,
17
+ In this case, we have a string literal and an enclosing context. If ` window `
18
+ is an NSWindow, there will only be one possible method named ` setTitle ` ,
21
19
which takes an NSString. Therefore, we want the string literal expression to
22
20
end up being an NSString.
23
21
@@ -33,35 +31,37 @@ if it is in range for that type.
33
31
The ExpressibleByStringLiteral Protocol
34
32
---------------------------------------
35
33
36
- Here is the ExpressibleByStringLiteral protocol as defined in the standard
34
+ Here is the ` ExpressibleByStringLiteral ` protocol as defined in the standard
37
35
library's CompilerProtocols.swift::
38
36
39
- /// A type that can be initialized with a string literal.
40
- ///
41
- /// The `String` and `StaticString` types conform to the
42
- /// `ExpressibleByStringLiteral` protocol. You can initialize a variable or
43
- /// constant of either of these types using a string literal of any length.
37
+ ``` swift
38
+ /// A type that can be initialized with a string literal.
39
+ ///
40
+ /// The `String` and `StaticString` types conform to the
41
+ /// `ExpressibleByStringLiteral` protocol. You can initialize a variable or
42
+ /// constant of either of these types using a string literal of any length.
43
+ ///
44
+ /// let picnicGuest = "Deserving porcupine"
45
+ ///
46
+ /// Conforming to ExpressibleByStringLiteral
47
+ /// ========================================
48
+ ///
49
+ /// To add `ExpressibleByStringLiteral` conformance to your custom type,
50
+ /// implement the required initializer.
51
+ public protocol ExpressibleByStringLiteral
52
+ : ExpressibleByExtendedGraphemeClusterLiteral {
53
+
54
+ /// A type that represents a string literal.
44
55
///
45
- /// let picnicGuest = "Deserving porcupine"
46
- ///
47
- /// Conforming to ExpressibleByStringLiteral
48
- /// ========================================
56
+ /// Valid types for `StringLiteralType` are `String` and `StaticString`.
57
+ associatedtype StringLiteralType : _ExpressibleByBuiltinStringLiteral
58
+
59
+ /// Creates an instance initialized to the given string value.
49
60
///
50
- /// To add `ExpressibleByStringLiteral` conformance to your custom type,
51
- /// implement the required initializer.
52
- public protocol ExpressibleByStringLiteral
53
- : ExpressibleByExtendedGraphemeClusterLiteral {
54
-
55
- /// A type that represents a string literal.
56
- ///
57
- /// Valid types for `StringLiteralType` are `String` and `StaticString`.
58
- associatedtype StringLiteralType: _ExpressibleByBuiltinStringLiteral
59
-
60
- /// Creates an instance initialized to the given string value.
61
- ///
62
- /// - Parameter value: The value of the new instance.
63
- init(stringLiteral value: StringLiteralType)
64
- }
61
+ /// - Parameter value: The value of the new instance.
62
+ init (stringLiteral value : StringLiteralType )
63
+ }
64
+ ```
65
65
66
66
Curiously, the protocol is not defined in terms of primitive types, but in
67
67
terms of any StringLiteralType that the implementer chooses. In most cases,
@@ -77,17 +77,19 @@ points could be constructed...which may be what's desired in some cases.)
77
77
The _ ExpressibleByBuiltinStringLiteral Protocol
78
78
-----------------------------------------------
79
79
80
- CompilerProtocols.swift contains a second protocol::
80
+ ` CompilerProtocols.swift ` contains a second protocol::
81
81
82
- // NOTE: the compiler has builtin knowledge of this protocol
83
- public protocol _ExpressibleByBuiltinStringLiteral
84
- : _ExpressibleByBuiltinExtendedGraphemeClusterLiteral {
82
+ ``` swift
83
+ // NOTE: the compiler has builtin knowledge of this protocol
84
+ public protocol _ExpressibleByBuiltinStringLiteral
85
+ : _ExpressibleByBuiltinExtendedGraphemeClusterLiteral {
85
86
86
- init(
87
- _builtinStringLiteral start: Builtin.RawPointer,
88
- utf8CodeUnitCount: Builtin.Word,
89
- isASCII: Builtin.Int1)
90
- }
87
+ init (
88
+ _builtinStringLiteral start : Builtin.RawPointer,
89
+ utf8CodeUnitCount : Builtin.Word ,
90
+ isASCII : Builtin.Int1)
91
+ }
92
+ ```
91
93
92
94
The use of builtin types makes it clear that this is * only* for use in the
93
95
standard library. This is the actual primitive function that is used to
@@ -118,23 +120,23 @@ terms of constraints, but it's easiest to understand as a linear process.
118
120
1 . Filter the types provided by the context to only include those that are
119
121
ExpressibleByStringLiteral.
120
122
2 . Using the associated StringLiteralType, find the appropriate
121
- `` _convertFromBuiltinStringLiteral ` `.
123
+ ` _convertFromBuiltinStringLiteral ` .
122
124
3 . Using the type from step 1, find the appropriate
123
- `` convertFromStringLiteral ` `.
125
+ ` convertFromStringLiteral ` .
124
126
4 . Build an expression tree with the appropriate calls.
125
127
126
128
How about cases where there is no context? ::
127
129
128
130
var str = "abc"
129
131
130
132
Here we have nothing to go on, so instead the type checker looks for a global
131
- type named `` StringLiteralType ` ` in the current module-scope context, and uses
133
+ type named ` StringLiteralType ` in the current module-scope context, and uses
132
134
that type if it is actually a ExpressibleByStringLiteral type. This both allows
133
135
different standard libraries to set different default literal types, and allows
134
136
a user to * override* the default type in their own source file.
135
137
136
138
The real story is even more complicated because of implicit conversions:
137
- the type expected by `` setTitle ` ` might not actually be literal-convertible,
139
+ the type expected by ` setTitle ` might not actually be literal-convertible,
138
140
but something else that * is* literal-convertible can then implicitly convert
139
141
to the proper type. If this makes your head spin, don't worry about it.
140
142
@@ -143,7 +145,7 @@ Arrays, Dictionaries, and Interpolation
143
145
---------------------------------------
144
146
145
147
Array and dictionary literals don't have a Builtin* Convertible form. Instead,
146
- they just always use a variadic list of elements (`` T... ` `) in the array case
148
+ they just always use a variadic list of elements (` T... ` ) in the array case
147
149
and (key, value) tuples in the dictionary case. A variadic list is always
148
150
exposed using the standard library's Array type, so there is no separate step
149
151
to jump through.
@@ -152,6 +154,6 @@ The default array literal type is always Array, and the default dictionary
152
154
literal type is always Dictionary.
153
155
154
156
String interpolations are a bit different: they create an instance of
155
- `` T.StringInterpolation ` ` and append each segment to it, then initialize
156
- an instance of `` T ` ` with that instance. The default type
157
- for an interpolated literal without context is also `` StringLiteralType ` `.
157
+ ` T.StringInterpolation ` and append each segment to it, then initialize
158
+ an instance of ` T ` with that instance. The default type
159
+ for an interpolated literal without context is also ` StringLiteralType ` .
0 commit comments