Skip to content

Instantly share code, notes, and snippets.

@ukitaka
Last active November 15, 2017 12:47
Show Gist options
  • Save ukitaka/66ea5fcb60f70c53658d058bf5c791ad to your computer and use it in GitHub Desktop.
Save ukitaka/66ea5fcb60f70c53658d058bf5c791ad to your computer and use it in GitHub Desktop.
存在型
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