Skip to content

Instantly share code, notes, and snippets.

@ezura
Created February 15, 2016 11:01
Show Gist options
  • Save ezura/86b254f0198637297fa4 to your computer and use it in GitHub Desktop.
Save ezura/86b254f0198637297fa4 to your computer and use it in GitHub Desktop.
自作の構造体で Range までは作れたけど、比較に使おうとするとできない…(´・ω・`)? #CodePiece
struct MyForwardIndexTypeStruct {
var num: Int
init(_ num: Int) {
self.num = num
}
}
// Equatable
func == (lhs: MyForwardIndexTypeStruct, rhs: MyForwardIndexTypeStruct) -> Bool {
return lhs.num == rhs.num
}
extension MyForwardIndexTypeStruct : ForwardIndexType {
// _Incrementable
func successor() -> MyForwardIndexTypeStruct {
return MyForwardIndexTypeStruct(self.num+1)
}
}
let start = MyForwardIndexTypeStruct(1)
let end = MyForwardIndexTypeStruct(5)
let myRange: Range = start ..< end
let range = 1 ..< 2
switch 1 {
case range:
print("match") // => "match"
default:
print("")
}
/*
error: expression pattern of type 'Range<MyForwardIndexTypeStruct>' cannot match values of type 'MyForwardIndexTypeStruct'
case myRange:
*/
switch MyForwardIndexTypeStruct(2) {
case myRange:
print("match")
default:
print("")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment