Skip to content

Instantly share code, notes, and snippets.

@KingOfBrian
Last active June 16, 2017 15:37
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 KingOfBrian/3ead2e437a0dd40e43228827c8f55976 to your computer and use it in GitHub Desktop.
Save KingOfBrian/3ead2e437a0dd40e43228827c8f55976 to your computer and use it in GitHub Desktop.

Describing dispatch scenarios is confusing, and spelling out the code is wordy. In an attempt to keep this document clear and compact, I'll use a short hand to describe the declaration scenarios.

Short hand:

T  = Type (class / struct / enum)
ST = Sub Type (only applies to classes)
P  = Protocol
M1.T = Type from the other module

P.F = Function requirement for a protocol
P.F[E] = Function extending a protocol
P.F[D] = Function defining a default implementation
P.F[S] = Function shadowing an existing protocol extension function
T:P = Type adding conformance for protocol
T.F[R:P] = function of type T satisfying a requirement from a protocol.
T.F[S:P] = function of type T shadowing an extension of a protocol
T.F[F] = final function of type T
T.F[D] = dynamic function of type T
T.F[E] = function declared in an extension of type T.
ST.F[O:T] = Subtype declaring a function that overrides the function declared in T. The `:T` is obvious in current swift, but is explicit to explore the use of `override` with protocols.
ST.F[O:T,R:P,F] = final declaration that overrides a declaration in T and satisfies R:P

T.Fa1[R:P](a1: T2? = nil) = Function Fa1 using the normal parameter list syntax.

Showing dispatch scenarios should still be done with normal swift code:

let a = T()
a.Fa1(a1: nil)

Shadow

A) Common shadow

P, T

P.F[E]
T.F[S:P]

B) Subtype shadow

// I think this is conceptually identical to the common shadow
P, T, ST

P.F[E]
ST: T
ST.F[S:P]

B) External module protocol extension shadow

M.P, M.T

M.P.F[E]
M1.P.F[S:P]

C) External module type shadow

M.P, M.T

M.P.F[E]
M1.T.F[E]

Satisfy Protocol Requirements

A) Basic Conformance

P, T

P.F
T.F[S:P]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment