Did you get the following error message in Swift?
Constrained extension must be declared on the unspecialized generic type 'Optional' with constraints specified by a 'where' clause
I got that one, and I tried to extend an optional string as follows:
extension Optional<String> { func ifEmpty(_ replacementString: String) -> String? { (self ?? "").isEmpty ? replacementString : self } }
The correct syntax is:
extension Optional where Wrapped == String { func ifEmpty(_ replacementString: String) -> String? { (self ?? "").isEmpty ? replacementString : self } }
It's still painful that it must return an optional. That can be avoided by a static function, which isn't as nice of course :)