Hi there.
I am trying to have a default string interpolation for optionals to be able to use it like
let foo: Float? = 3.4
print("foo \(foo)") // "3.4"
let bar: Float? = nil
print("bar \(bar)") // "-"
I wrote
extension Optional: CustomStringConvertible where Wrapped: CustomStringConvertible {
public var description: String {
self?.description ?? "-"
}
}
But this will print "No exact matches in call to instance method 'appendInterpolation'" for "\(foo)"
.
However, I can do "\(foo.description)"
, which works fine.
I guess the issue is that the compiler does not realize that the optional type is actually conforming to "CustomStringConvertible".
Is there a way to fix this?
Just out of curiosity, as I think this solution is actually better.