Created
February 15, 2016 11:01
-
-
Save ezura/86b254f0198637297fa4 to your computer and use it in GitHub Desktop.
自作の構造体で Range までは作れたけど、比較に使おうとするとできない…(´・ω・`)? #CodePiece
This file contains 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 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