Why You’re Not Supposed to Call Swift’s description
However, you should absolutely not use the following pattern, where a function argument is constrained to
Custom[Debug]StringConvertible
, either with a generic constraint or a plain type specification[…][…]
Instead, the function should accept any type, because anything is printable:
func doSomething3<T>(with x: T) { // ... // Call String(describing: x) }[…]
When you rely on
LosslessStringConvertible
semantics, you should absolutely access thedescription
property directly, despite the above advice to the contrary. As we have seen, the alternativeString(describing:)
prefersTextOutputStreamable
’s representation overdescription
if it’s available, and you can’t be 100 % certain that representation is identical to the value’sdescription
, however unlikely any differences are.