Skip to content

Instantly share code, notes, and snippets.

@msbit
Last active March 21, 2021 02:47
Show Gist options
  • Save msbit/9b6538855d00b473d90f7882ea78d031 to your computer and use it in GitHub Desktop.
Save msbit/9b6538855d00b473d90f7882ea78d031 to your computer and use it in GitHub Desktop.
Swift inheritance hierarchy protocol conformance in generic context
protocol Consumer {
static func consume(_ instance: Self)
}
class Base: Consumer {
class func consume(_ instance: Base) { print("Base.consume(_:)") }
}
class Derived: Base {
class func consume(_ instance: Derived) { print("Derived.consume(_:)") }
}
class Generic <C: Consumer> {
class func consume(_ instance: C) { C.consume(instance) }
}
Generic<Base>.consume(Base())
Generic<Base>.consume(Derived())
Generic<Derived>.consume(Derived())
Base.consume(_:)
Base.consume(_:)
Base.consume(_:)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment