10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
- /// Represents a string literal with interpolations while it is being built up.
14
- ///
15
- /// Do not create an instance of this type directly. It is used by the compiler
16
- /// when you create a string using string interpolation. Instead, use string
17
- /// interpolation to create a new string by including values, literals,
13
+ /// Represents a string literal with interpolations while it's being built up.
14
+ ///
15
+ /// You don't need to create an instance of this type directly. It's used by the
16
+ /// compiler when you create a string using string interpolation. Instead, use
17
+ /// string interpolation to create a new string by including values, literals,
18
18
/// variables, or expressions enclosed in parentheses, prefixed by a
19
19
/// backslash (`\(`...`)`).
20
20
///
@@ -68,8 +68,8 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol, Sendable
68
68
/// Creates a string interpolation with storage pre-sized for a literal
69
69
/// with the indicated attributes.
70
70
///
71
- /// Do not call this initializer directly. It is used by the compiler when
72
- /// interpreting string interpolations.
71
+ /// You don't need to call this initializer directly. It's used by the
72
+ /// compiler when interpreting string interpolations.
73
73
@inlinable
74
74
public init ( literalCapacity: Int , interpolationCount: Int ) {
75
75
let capacityPerInterpolation = 2
@@ -80,8 +80,8 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol, Sendable
80
80
81
81
/// Appends a literal segment of a string interpolation.
82
82
///
83
- /// Do not call this method directly. It is used by the compiler when
84
- /// interpreting string interpolations.
83
+ /// You don't need to call this method directly. It's used by the compiler
84
+ /// when interpreting string interpolations.
85
85
@inlinable
86
86
public mutating func appendLiteral( _ literal: String ) {
87
87
literal. write ( to: & self )
@@ -90,8 +90,8 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol, Sendable
90
90
/// Interpolates the given value's textual representation into the
91
91
/// string literal being created.
92
92
///
93
- /// Do not call this method directly. It is used by the compiler when
94
- /// interpreting string interpolations. Instead, use string
93
+ /// You don't need to call this method directly. It's used by the compiler
94
+ /// when interpreting string interpolations. Instead, use string
95
95
/// interpolation to create a new string by including values, literals,
96
96
/// variables, or expressions enclosed in parentheses, prefixed by a
97
97
/// backslash (`\(`...`)`).
@@ -114,8 +114,8 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol, Sendable
114
114
/// Interpolates the given value's textual representation into the
115
115
/// string literal being created.
116
116
///
117
- /// Do not call this method directly. It is used by the compiler when
118
- /// interpreting string interpolations. Instead, use string
117
+ /// You don't need to call this method directly. It's used by the compiler
118
+ /// when interpreting string interpolations. Instead, use string
119
119
/// interpolation to create a new string by including values, literals,
120
120
/// variables, or expressions enclosed in parentheses, prefixed by a
121
121
/// backslash (`\(`...`)`).
@@ -136,8 +136,8 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol, Sendable
136
136
/// Interpolates the given value's textual representation into the
137
137
/// string literal being created.
138
138
///
139
- /// Do not call this method directly. It is used by the compiler when
140
- /// interpreting string interpolations. Instead, use string
139
+ /// You don't need to call this method directly. It's used by the compiler
140
+ /// when interpreting string interpolations. Instead, use string
141
141
/// interpolation to create a new string by including values, literals,
142
142
/// variables, or expressions enclosed in parentheses, prefixed by a
143
143
/// backslash (`\(`...`)`).
@@ -160,8 +160,8 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol, Sendable
160
160
/// Interpolates the given value's textual representation into the
161
161
/// string literal being created.
162
162
///
163
- /// Do not call this method directly. It is used by the compiler when
164
- /// interpreting string interpolations. Instead, use string
163
+ /// You don't need to call this method directly. It's used by the compiler
164
+ /// when interpreting string interpolations. Instead, use string
165
165
/// interpolation to create a new string by including values, literals,
166
166
/// variables, or expressions enclosed in parentheses, prefixed by a
167
167
/// backslash (`\(`...`)`).
@@ -197,6 +197,128 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol, Sendable
197
197
}
198
198
}
199
199
200
+ extension DefaultStringInterpolation {
201
+ /// Interpolates the given optional value's textual representation, or the
202
+ /// specified default string, into the string literal being created.
203
+ ///
204
+ /// You don't need to call this method directly. It's used by the compiler
205
+ /// when interpreting string interpolations where you provide a `default`
206
+ /// parameter. For example, the following code implicitly calls this method,
207
+ /// using the value of the `default` parameter when `value` is `nil`:
208
+ ///
209
+ /// var age: Int? = 48
210
+ /// print("Your age is \(age, default: "unknown")")
211
+ /// // Prints: Your age is 48
212
+ /// age = nil
213
+ /// print("Your age is \(age, default: "unknown")")
214
+ /// // Prints: Your age is unknown
215
+ ///
216
+ /// - Parameters:
217
+ /// - value: The value to include in a string interpolation, if non-`nil`.
218
+ /// - default: The string to include if `value` is `nil`.
219
+ @_alwaysEmitIntoClient
220
+ public mutating func appendInterpolation< T> (
221
+ _ value: T ? ,
222
+ default: @autoclosure ( ) -> some StringProtocol
223
+ ) where T: TextOutputStreamable , T: CustomStringConvertible {
224
+ if let value {
225
+ self . appendInterpolation ( value)
226
+ } else {
227
+ self . appendInterpolation ( `default` ( ) )
228
+ }
229
+ }
230
+
231
+ /// Interpolates the given optional value's textual representation, or the
232
+ /// specified default string, into the string literal being created.
233
+ ///
234
+ /// You don't need to call this method directly. It's used by the compiler
235
+ /// when interpreting string interpolations where you provide a `default`
236
+ /// parameter. For example, the following code implicitly calls this method,
237
+ /// using the value of the `default` parameter when `value` is `nil`:
238
+ ///
239
+ /// var age: Int? = 48
240
+ /// print("Your age is \(age, default: "unknown")")
241
+ /// // Prints: Your age is 48
242
+ /// age = nil
243
+ /// print("Your age is \(age, default: "unknown")")
244
+ /// // Prints: Your age is unknown
245
+ ///
246
+ /// - Parameters:
247
+ /// - value: The value to include in a string interpolation, if non-`nil`.
248
+ /// - default: The string to include if `value` is `nil`.
249
+ @_alwaysEmitIntoClient
250
+ public mutating func appendInterpolation< T> (
251
+ _ value: T ? ,
252
+ default: @autoclosure ( ) -> some StringProtocol
253
+ ) where T: TextOutputStreamable {
254
+ if let value {
255
+ self . appendInterpolation ( value)
256
+ } else {
257
+ self . appendInterpolation ( `default` ( ) )
258
+ }
259
+ }
260
+
261
+ /// Interpolates the given optional value's textual representation, or the
262
+ /// specified default string, into the string literal being created.
263
+ ///
264
+ /// You don't need to call this method directly. It's used by the compiler
265
+ /// when interpreting string interpolations where you provide a `default`
266
+ /// parameter. For example, the following code implicitly calls this method,
267
+ /// using the value of the `default` parameter when `value` is `nil`:
268
+ ///
269
+ /// var age: Int? = 48
270
+ /// print("Your age is \(age, default: "unknown")")
271
+ /// // Prints: Your age is 48
272
+ /// age = nil
273
+ /// print("Your age is \(age, default: "unknown")")
274
+ /// // Prints: Your age is unknown
275
+ ///
276
+ /// - Parameters:
277
+ /// - value: The value to include in a string interpolation, if non-`nil`.
278
+ /// - default: The string to include if `value` is `nil`.
279
+ @_alwaysEmitIntoClient
280
+ public mutating func appendInterpolation< T> (
281
+ _ value: T ? ,
282
+ default: @autoclosure ( ) -> some StringProtocol
283
+ ) where T: CustomStringConvertible {
284
+ if let value {
285
+ self . appendInterpolation ( value)
286
+ } else {
287
+ self . appendInterpolation ( `default` ( ) )
288
+ }
289
+ }
290
+
291
+ /// Interpolates the given optional value's textual representation, or the
292
+ /// specified default string, into the string literal being created.
293
+ ///
294
+ /// You don't need to call this method directly. It's used by the compiler
295
+ /// when interpreting string interpolations where you provide a `default`
296
+ /// parameter. For example, the following code implicitly calls this method,
297
+ /// using the value of the `default` parameter when `value` is `nil`:
298
+ ///
299
+ /// var age: Int? = 48
300
+ /// print("Your age is \(age, default: "unknown")")
301
+ /// // Prints: Your age is 48
302
+ /// age = nil
303
+ /// print("Your age is \(age, default: "unknown")")
304
+ /// // Prints: Your age is unknown
305
+ ///
306
+ /// - Parameters:
307
+ /// - value: The value to include in a string interpolation, if non-`nil`.
308
+ /// - default: The string to include if `value` is `nil`.
309
+ @_alwaysEmitIntoClient
310
+ public mutating func appendInterpolation< T> (
311
+ _ value: T ? ,
312
+ default: @autoclosure ( ) -> some StringProtocol
313
+ ) {
314
+ if let value {
315
+ self . appendInterpolation ( value)
316
+ } else {
317
+ self . appendInterpolation ( `default` ( ) )
318
+ }
319
+ }
320
+ }
321
+
200
322
extension DefaultStringInterpolation : CustomStringConvertible {
201
323
@inlinable
202
324
public var description : String {
@@ -220,9 +342,9 @@ extension DefaultStringInterpolation: TextOutputStream {
220
342
extension String {
221
343
/// Creates a new instance from an interpolated string literal.
222
344
///
223
- /// Do not call this initializer directly. It is used by the compiler when
224
- /// you create a string using string interpolation. Instead, use string
225
- /// interpolation to create a new string by including values, literals,
345
+ /// You don't need to call this initializer directly. It's used by the
346
+ /// compiler when you create a string using string interpolation. Instead, use
347
+ /// string interpolation to create a new string by including values, literals,
226
348
/// variables, or expressions enclosed in parentheses, prefixed by a
227
349
/// backslash (`\(`...`)`).
228
350
///
@@ -244,9 +366,9 @@ extension String {
244
366
extension Substring {
245
367
/// Creates a new instance from an interpolated string literal.
246
368
///
247
- /// Do not call this initializer directly. It is used by the compiler when
248
- /// you create a string using string interpolation. Instead, use string
249
- /// interpolation to create a new string by including values, literals,
369
+ /// You don't need to call this initializer directly. It's used by the
370
+ /// compiler when you create a string using string interpolation. Instead, use
371
+ /// string interpolation to create a new string by including values, literals,
250
372
/// variables, or expressions enclosed in parentheses, prefixed by a
251
373
/// backslash (`\(`...`)`).
252
374
///
0 commit comments