Skip to content

Instantly share code, notes, and snippets.

@pcantrell
Last active June 15, 2017 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcantrell/52162c7ad6c6f44a8475c14acb7ed3aa to your computer and use it in GitHub Desktop.
Save pcantrell/52162c7ad6c6f44a8475c14acb7ed3aa to your computer and use it in GitHub Desktop.
protocol A {
func foo()
}
extension A {
default func foo() { … } // Dynamically dispatched b/c it’s in the protocol
func bar() { … } // Statically dispatched b/c it’s not in the protocol
}
// Dynamic shadows static
protocol B: A {
func bar()
}
extension B {
func foo() { … } // Shouldn’t warn, but should this also require `default`? or maybe `override`?
default func bar() { … } // Should warn about shadowing A.bar()
}
// Static shadows static
protocol C: A {
}
extension C {
func foo() { … } // Should warn about shadowing A.foo()
}
// Static can’t shadow dynamic: anything that implements B will use dynamic dispatch for bar(),
// and any type that tries to separately conform to both B and something else with a static
// extension impl for bar() already gives a compile error:
protocol D {
}
extension D {
func foo() { }
}
class E: B, D { // Already gives a compiler error about conflicting foo() impls
}
// Type implementation shadowing/overriding extensions
class F: A {
func foo() { … } // Legit; shouldn’t warn. Should this require `override`?
func bar() { … } // Should warn about collision with B.bar()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment