Fix CustomStringConvertible/CustomDebugStringConvertible conformances

- For CustomStringConvertible, `description` should print elements using `String(reflecting:)` rather than `String(describing:)`, to avoid element output from interfering with syntactically relevant punctuation within structured list displays.
- To make this work without making displays overly verbose, make `debugDescription` mostly the same as `description`, except for escaping raw string displays when and if needed.
- Avoid printing type names in `debugDescription` — it is generally not helpful, as the type is already easily accessible elsewhere. Including it makes for overly verbose displays when the type serves as an Element of a collection.
- Avoid having `debugDescription` return the empty string — it can be confusing when such a description appears in list displays.

Resolves #301.
This commit is contained in:
Karoy Lorentey committed 2023-07-06 17:25:11 -07:00
1 parent 9eece74f39
commit 09c09f0f73
31 files changed
+122 -266

No files matched your search

@@ -33,8 +33,23 @@ extension BitArray: CustomStringConvertible {
extension BitArray: CustomDebugStringConvertible {
/// A textual representation of this instance, suitable for debugging.
///
/// Bit arrays print themselves as a string of binary bits, with the highest-indexed elements
/// appearing first, as in the binary representation of integers:
///
/// let bits: BitArray = [false, false, false, true, true]
/// print(bits) // "11000"
///
/// As a special case, the debug description of an empty bit array is an empty pair of brackets.
/// This makes this case read better in structured printouts, like when the `BitArray` is printed
/// as part of another collection value:
///
/// let list: [BitArray] = [[false, true], [], [false]]
/// print(list) // "[10, [], 0]"
///
public var debugDescription: String {
"BitArray(\(_bitString))"
guard !isEmpty else { return "[]" }
return description
}
}