Skip to content

Instantly share code, notes, and snippets.

@Moximillian
Last active April 28, 2018 12:03
Show Gist options
  • Save Moximillian/db428af7c8522607c73ed12c637e122e to your computer and use it in GitHub Desktop.
Save Moximillian/db428af7c8522607c73ed12c637e122e to your computer and use it in GitHub Desktop.
Example of how init/func behaves in protocols and protocol extensions
protocol P1 {
init()
func bar()
}
extension P1 {
init() {
self.init()
print("P1 init")
}
func bar() {
print("P1 func")
}
}
struct S1: P1 {
init() {
print("S1 init")
}
func bar() {
print("S1 func")
}
}
protocol P2 {}
extension P2 {
init() {
self.init()
print("P2 init")
}
func bar() {
print("P2 func")
}
}
struct S2: P2 {
init() {
print("S2 init")
}
func bar() {
print("S2 func")
}
}
let s1 = S1() // prints "S1 init"
s1.bar() // prints "S1 func"
let s2 = S2() // prints "S2 init"
s2.bar() // prints "S2 func"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment