mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
This is mostly just cleanup: 1. Removes `diagnostic-descriptions.md` since it isn't used any more 2. Adds the group name to all the old notes files 3. Removes trailing whitespace 4. Adds "See Also" sections for notes that have links
20 lines
1.9 KiB
Markdown
20 lines
1.9 KiB
Markdown
# Conforming to `StringInterpolationProtocol` (StringInterpolationConformance)
|
|
|
|
## Overview
|
|
|
|
A type conforming to `ExpressibleByStringInterpolation` uses a helper type called `StringInterpolation` to perform its interpolation. Many types can use `DefaultStringInterpolation`, which implements `String`'s interpolation behavior. Types can also implement custom behavior by providing their own type conforming to `StringInterpolationProtocol`.
|
|
|
|
In addition to its formal requirements, `init(literalCapacity:interpolationCount:)` and `appendLiteral(_:)`, `StringInterpolationProtocol` has an additional, informal requirement, `appendInterpolation`. String interpolations using `\()` syntax are translated into calls to matching `appendInterpolation` methods.
|
|
|
|
`StringInterpolationProtocol` conformers must provide at least one `appendInterpolation` method which:
|
|
|
|
- Is an instance method, as opposed to a `static` or `class` method
|
|
- Does not specify a return type, explicitly returns `Void`, or is marked with the `@discardableResult` attribute
|
|
- Is at least as accessible as its containing type
|
|
|
|
There are no restrictions on an `appendInterpolation` method's argument list, generic parameters, availability, or error-throwing behavior.
|
|
|
|
If `appendInterpolation` is overloaded, the Swift compiler will choose an appropriate overload using the labels and argument types of each interpolation. When choosing an overload, any accessible `appendInterpolation` instance method may be used, even if it does not meet all of the requirements above. However, if a `StringInterpolationProtocol` conformer doesn't have any `appendInterpolation` methods which meet all of the requirements, an error will be reported at compile time.
|
|
|
|
To learn more about customizing string interpolation behavior, see the standard library documentation of the `ExpressibleByStringInterpolation` and `StringInterpolationProtocol` protocols.
|