Last active
April 28, 2018 12:03
-
-
Save Moximillian/db428af7c8522607c73ed12c637e122e to your computer and use it in GitHub Desktop.
Example of how init/func behaves in protocols and protocol extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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