Skip to content

Commit 5b83661

Browse files
committed
[Gardening] De-RST Literals
1 parent 11a1ce1 commit 5b83661

File tree

1 file changed

+51
-49
lines changed

1 file changed

+51
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
:orphan:
2-
31
Literals
42
========
53

@@ -12,12 +10,12 @@ literals, so let's look at those.
1210
High-Level View
1311
---------------
1412

15-
::
16-
17-
window.setTitle("Welcome to Xcode")
13+
```swift
14+
window.setTitle("Welcome to Xcode")
15+
```
1816

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`,
2119
which takes an NSString. Therefore, we want the string literal expression to
2220
end up being an NSString.
2321

@@ -33,35 +31,37 @@ if it is in range for that type.
3331
The ExpressibleByStringLiteral Protocol
3432
---------------------------------------
3533

36-
Here is the ExpressibleByStringLiteral protocol as defined in the standard
34+
Here is the `ExpressibleByStringLiteral` protocol as defined in the standard
3735
library's CompilerProtocols.swift::
3836

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.
4455
///
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.
4960
///
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+
```
6565

6666
Curiously, the protocol is not defined in terms of primitive types, but in
6767
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.)
7777
The _ExpressibleByBuiltinStringLiteral Protocol
7878
-----------------------------------------------
7979

80-
CompilerProtocols.swift contains a second protocol::
80+
`CompilerProtocols.swift` contains a second protocol::
8181

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 {
8586

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+
```
9193

9294
The use of builtin types makes it clear that this is *only* for use in the
9395
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.
118120
1. Filter the types provided by the context to only include those that are
119121
ExpressibleByStringLiteral.
120122
2. Using the associated StringLiteralType, find the appropriate
121-
``_convertFromBuiltinStringLiteral``.
123+
`_convertFromBuiltinStringLiteral`.
122124
3. Using the type from step 1, find the appropriate
123-
``convertFromStringLiteral``.
125+
`convertFromStringLiteral`.
124126
4. Build an expression tree with the appropriate calls.
125127

126128
How about cases where there is no context? ::
127129

128130
var str = "abc"
129131

130132
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
132134
that type if it is actually a ExpressibleByStringLiteral type. This both allows
133135
different standard libraries to set different default literal types, and allows
134136
a user to *override* the default type in their own source file.
135137

136138
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,
138140
but something else that *is* literal-convertible can then implicitly convert
139141
to the proper type. If this makes your head spin, don't worry about it.
140142

@@ -143,7 +145,7 @@ Arrays, Dictionaries, and Interpolation
143145
---------------------------------------
144146

145147
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
147149
and (key, value) tuples in the dictionary case. A variadic list is always
148150
exposed using the standard library's Array type, so there is no separate step
149151
to jump through.
@@ -152,6 +154,6 @@ The default array literal type is always Array, and the default dictionary
152154
literal type is always Dictionary.
153155

154156
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

Comments
 (0)