Last active
November 15, 2017 12:47
-
-
Save ukitaka/66ea5fcb60f70c53658d058bf5c791ad 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 Animal { | |
func bark() | |
} | |
struct Dog: Animal { | |
func bark() { | |
print("わんわん") | |
} | |
} | |
let animal: Animal = Dog() // 暗黙の存在型へのpack(erase) | |
animal.bark() // 暗黙のunpack(opening existential) + メソッド呼び出し | |
func test1(animal: Animal) { } | |
func test2<A: Animal>(animal: A) { } | |
test1(animal: animal) // OK: 存在型をそのまま受ける | |
test2(animal: animal) // NG: ここでopening existentialが暗黙的にされない(これがわかりにくい?) | |
let a = animal openas A // 将来opening existentialが入るとする | |
test2(animal: a) // そしたらこれはOK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment