Skip to content

Instantly share code, notes, and snippets.

@jeremiegirault
Created December 4, 2015 10:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremiegirault/a2f41f98f9a41cb88f39 to your computer and use it in GitHub Desktop.
Save jeremiegirault/a2f41f98f9a41cb88f39 to your computer and use it in GitHub Desktop.
Protocol retrofitting for constrained generic extension in swift
let myString: String? = nil
protocol PossiblyEmpty {
var isEmpty: Bool { get }
}
extension String: PossiblyEmpty {}
extension Optional where Wrapped: PossiblyEmpty {
var isEmpty: Bool {
switch self {
case .None: return true
case .Some(let value): return value.isEmpty
}
}
}
myString.isEmpty
@eonist
Copy link

eonist commented Mar 7, 2017

@jeremiegirault

Swift 3 update:
Change:

.none
.some

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment