Create a gist now

Instantly share code, notes, and snippets.

Playground contents demonstrating an ugly, but, ultimately works just fine, approach to raw enums with structs for values
import Foundation
public struct RegionStruct: StringLiteralConvertible, Equatable {
let name: String
let platformId: String
let host: NSURL
typealias UnicodeScalarLiteralType = Character
/// Create an instance initialized to `value`.
public init(unicodeScalarLiteral value: Character) {
self.init(stringLiteral: String(value))
}
typealias ExtendedGraphemeClusterLiteralType = String
public init(extendedGraphemeClusterLiteral value: String) {
self.init(stringLiteral: value)
}
typealias StringLiteralType = String
public init(stringLiteral value: String) {
let parts = value.componentsSeparatedByString(" ")
self.init(name: parts[0], platformId: parts[1], host: NSURL(string: parts[2])!)
}
public init(name: String, platformId: String, host: NSURL) {
self.name = name
self.platformId = platformId
self.host = host
}
}
public func ==(lhs: RegionStruct, rhs: RegionStruct) -> Bool {
let computedLhs = "\(lhs.name) \(lhs.platformId) \(lhs.host)"
let computedRhs = "\(rhs.name) \(rhs.platformId) \(rhs.host)"
return computedLhs == computedRhs
}
let x: RegionStruct = "name0 plaf0 http://x.y/0"
public enum Region: RegionStruct {
case One = "nameOne plafOne http://one.one/one";
case Two = "nameTwo plafTwo http://two.two/two";
case Three = "nameThree plafThree http://three.three/three";
}
for region in [Region.One, Region.Two, Region.Three] {
switch(region) {
case .One:
println(region.rawValue.host)
case .Two:
println(region.rawValue.host)
case .Three:
println(region.rawValue.host)
}
}
Region.One.rawValue
Region.One.rawValue == Region.One.rawValue
Region.Two.rawValue == Region.One.rawValue
let r = Region(rawValue: Region.One.rawValue)
r == Region.One
let rs: RegionStruct = RegionStruct(name: "nameOne", platformId: "plafOne", host: NSURL(string: "http://one.one/one")!)
Region.One == Region(rawValue: rs)
@ztj
Owner
ztj commented Mar 7, 2015

Some elements could be implemented better in production code. This is an example. It is a starting point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment