Skip to content

Instantly share code, notes, and snippets.

@ukitaka
Last active January 10, 2017 09:19
Show Gist options
  • Save ukitaka/8a6cc48222c8a5f397fa5e9fb113c1bc to your computer and use it in GitHub Desktop.
Save ukitaka/8a6cc48222c8a5f397fa5e9fb113c1bc to your computer and use it in GitHub Desktop.
型制約をつけると具体型が必要になる.swift
protocol Creature { }
protocol Animal: Creature { }
struct Dog: Animal { }
struct Cat: Animal { }
// OK
class Hoge<A> { }
let hoge = Hoge<Animal>() // OK
// NG
// error: using 'Animal' as a concrete type conforming to protocol 'Creature' is not supported
class Fuga<A: Creature> { }
let fuga = Fuga<Animal>() // NG
// OK
class Piyo<A> { }
extension Piyo where A: Creature { }
let piyo = Piyo<Animal>() // OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment