Skip to content

Instantly share code, notes, and snippets.

@ddunbar
Created August 12, 2016 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddunbar/71155bfad9fdff6d45c4fcaa29181185 to your computer and use it in GitHub Desktop.
Save ddunbar/71155bfad9fdff6d45c4fcaa29181185 to your computer and use it in GitHub Desktop.
Extensible enum raw values
import Foundation
struct X: Equatable, ExpressibleByStringLiteral {
let strings: Set<String>
init?(_ characters: String.CharacterView) {
let strings = Set<String>(String(characters).components(separatedBy: ",").map{ String($0) })
if strings.isEmpty {
return nil
}
self.strings = strings
}
init(stringLiteral value: String) {
self.init(value.characters)!
}
init(extendedGraphemeClusterLiteral value: String) {
self.init(stringLiteral: value)
}
init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
}
func ==(lhs: X, rhs: X) -> Bool {
return lhs.strings == rhs.strings
}
enum E: X {
case fooBar = "foo-bar,f"
}
print(E.fooBar.rawValue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment