Created
March 25, 2018 23:26
-
-
Save creaaa/bc2d17f5931103d6a486bc727ac1173c to your computer and use it in GitHub Desktop.
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
protocol P {} | |
struct S: P {} | |
extension Array { | |
func hoge() -> String { return "Any" } // ① | |
} | |
// enum or 構造体は、「連想型: enum or 構造体」で使えない。 | |
//extension Array where Element: S { | |
// func hoge() -> String { return "Equal" } // ② | |
//} | |
// このように「連想型 == enum or 構造体」ならOK | |
// (これはSwift3.1で可能になったやつ) | |
extension Array where Element == S { | |
func hoge() -> String { return "Equal" } // ② | |
} | |
extension Array where Element: P { | |
func hoge() -> String { return "Protocol" } // ③ | |
} | |
extension Array where Element == P { | |
func hoge() -> String { return "Existence" } // ④ | |
} | |
[S()].hoge() // 2が発動 | |
// 1 | |
[S() as Any].hoge() | |
// 3 | |
// 4 | |
let array: [P] = [S()] | |
array.hoge() | |
// もしくは | |
([S()] as [P]).hoge() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment