Last active
May 5, 2017 16:58
-
-
Save callionica/19da0763c0b0b4c504fc5f46d07e8ee8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
////////////////////// | |
// File 1 | |
public class Base { | |
public struct Overridable { // Do not store | |
private init() {} | |
} | |
public struct Protected { // OK to store | |
private init() {} | |
} | |
public struct Secrets { | |
var message = "Treasure!" | |
} | |
private let secrets : Secrets = Secrets() | |
public func getSecrets(key: Protected)->Secrets { | |
return secrets | |
} | |
public func layout(key: Overridable) { | |
} | |
public func updateLayout() { | |
layout(Overridable()) | |
} | |
public init(_ proof: (Protected)->()) { | |
proof(Protected()) | |
} | |
} | |
////////////////////// | |
// File 2 | |
class Derived : Base { | |
private var key : Protected? = nil | |
init() { | |
var key : Protected? | |
super.init({ k in key = k }) | |
self.key = key! | |
} | |
func callSecrets() { | |
print(getSecrets(key!).message) | |
} | |
override func layout(key: Overridable) { | |
super.layout(key) // Can call the base class | |
} | |
} | |
var derived = Derived() | |
derived.callSecrets() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Swift doesn't provide "protected" as an access level, but you can emulate it. And you can distinguish between methods that are supposed to be called and those that are only supposed to be overridden.
Write up at https://www.callionica.com/developer/#swift-protected