Skip to content

Instantly share code, notes, and snippets.

@LucianoPAlmeida
Last active January 20, 2019 23:15
Show Gist options
  • Save LucianoPAlmeida/18160112445494db3aec851739e5a835 to your computer and use it in GitHub Desktop.
Save LucianoPAlmeida/18160112445494db3aec851739e5a835 to your computer and use it in GitHub Desktop.
struct HTTPResponse {
var statusCode: Int
}
extension HTTPResponse {
func validateStatus<R>(in range: R) -> Bool where R: RangeExpression, R.Bound == Int {
return range~=self.statusCode
}
}
let http = HTTPResponse(statusCode: 200)
http.validateStatus(in: 200...) // PartialRangeFrom
http.validateStatus(in: 200...300) // ClosedRange
http.validateStatus(in: 200..<300) // Range
http.validateStatus(in: ...300) // PartialRangeThrough
http.validateStatus(in: ..<300) // PartialRangeUpTo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment