Skip to content

Instantly share code, notes, and snippets.

@oozoofrog
Created May 16, 2018 22:07
Show Gist options
  • Save oozoofrog/9e53fc88adc31ce88d81b6e8a1a8be25 to your computer and use it in GitHub Desktop.
Save oozoofrog/9e53fc88adc31ce88d81b6e8a1a8be25 to your computer and use it in GitHub Desktop.
protocol, generic, extension, conditional conformance
struct Be<Value> {
let value: Value
}
protocol BeAddible {
var be: Be<Self> { get }
}
extension Be where Value: Comparable {
func inside(_ range: ClosedRange<Value>) -> Bool {
return range.contains(self.value)
}
func inside(_ from: Value, to: Value) -> Bool {
return self.inside(from...to)
}
}
extension BeAddible {
var be: Be<Self> {
return Be(value: self)
}
}
extension Int: BeAddible {}
extension String: BeAddible {}
print(2.be.inside(0...10))
print(12.be.inside(0, to: 10))
print("a".be.inside("a", to: "z"))
print("A".be.inside("a"..."z"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment