Skip to content

Instantly share code, notes, and snippets.

@haikusw
Created October 27, 2019 01:48
Show Gist options
  • Save haikusw/3bf255771ccf73c404cac426d789938c to your computer and use it in GitHub Desktop.
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).
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