Skip to content

Instantly share code, notes, and snippets.

@vittoriom
Created April 27, 2016 07:06
Show Gist options
  • Save vittoriom/35660b67114f173f4f2e2e61fbab14ed to your computer and use it in GitHub Desktop.
Save vittoriom/35660b67114f173f4f2e2e61fbab14ed to your computer and use it in GitHub Desktop.
Protocol extension dynamic dispatch
protocol Test {
func doStuff()
}
extension Test {
// default protocol implementation
func doStuff() {
print("Stuff!")
}
}
// Using default implementation
class Implementor: Test {
}
// subclassing and overriding implementation
class Implementor2: Implementor {
func doStuff() {
print("Not stuff!")
}
}
let y: Test = Implementor2()
y.doStuff() // "Stuff!" is printed?
@nashysolutions
Copy link

class Implementor: Test {
  func doStuff() {
    print("Stuff!")
  }
}

class Implementor2: Implementor {
  override func doStuff() {
    print("Not stuff!")
  }
}

let y: Test = Implementor2()
y.doStuff() // "Not stuff!" is printed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment