Created
May 16, 2018 22:07
-
-
Save oozoofrog/9e53fc88adc31ce88d81b6e8a1a8be25 to your computer and use it in GitHub Desktop.
protocol, generic, extension, conditional conformance
This file contains hidden or 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
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