Created
September 22, 2021 18:31
-
-
Save yakushevichsv/e44c28238f5452a276fe67d1b4b063ad to your computer and use it in GitHub Desktop.
Contains extensions on Optional type (DSL wrappers)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Optional { | |
var hasValue: Bool { | |
if case .some = self { | |
return true | |
} | |
return false | |
} | |
func valueOrDefault( _ def: Wrapped) -> Wrapped { | |
switch self { | |
case let .some(value): | |
return value | |
case .none: | |
return def | |
} | |
} | |
} | |
extension Optional where Wrapped: StringProtocol { | |
var valueOrEmpty: Wrapped { | |
valueOrDefault("") | |
} | |
} | |
extension Optional where Wrapped == Bool { | |
var valueOrFalse: Wrapped { | |
valueOrDefault(false) | |
} | |
} | |
extension Optional where Wrapped: SignedInteger { | |
var valueOrZero: Wrapped { | |
valueOrDefault(0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment