Created
October 27, 2019 01:48
-
-
Save haikusw/3bf255771ccf73c404cac426d789938c to your computer and use it in GitHub Desktop.
RawRepresentable and struct to get enum-like behavior that can be extended in other modules (frameworks for example).
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
import UIKit | |
// RawRepresentable and struct to get enum-like behavior that can be extended | |
// in other modules (frameworks for example). | |
public struct Config: RawRepresentable { | |
public typealias RawValue = String | |
public let rawValue: RawValue | |
public init(rawValue: String) { | |
self.rawValue = rawValue | |
} | |
public static let v1 = Config(rawValue: "v1") | |
public static let v2 = Config(rawValue: "v2") | |
} | |
public struct Common { | |
public static func doWork(with: Config) { | |
print(with.rawValue) | |
} | |
} | |
// in another framework | |
public extension Config { | |
static let v3 = Config(rawValue: "v3") | |
} | |
Common.doWork(with: .v1) | |
Common.doWork(with: .v2) | |
Common.doWork(with: .v3) | |
/* output: | |
v1 | |
v2 | |
v3 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment