Last active
August 29, 2015 14:20
-
-
Save nubbel/2784aac019b81b1646a4 to your computer and use it in GitHub Desktop.
This file contains 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
final class Box<T> { | |
let value: T | |
init(_ value: T) { | |
self.value = value | |
} | |
} | |
protocol DataType : Printable { | |
} | |
protocol PrimitiveType : DataType { | |
} | |
enum UndefinedType : String, PrimitiveType { | |
case Undefined = "Undefined" | |
case Null = "Uninitialized" | |
var description : String { | |
return rawValue | |
} | |
static func fromState(state: String) -> UndefinedType { | |
return UndefinedType(rawValue: state)! | |
} | |
} | |
enum OnOffType : String, PrimitiveType { | |
case On = "ON" | |
case Off = "OFF" | |
var description : String { | |
return rawValue | |
} | |
} | |
enum PercentType : PrimitiveType { | |
case Zero | |
case Hundred | |
case Value(Int) | |
var value : Int { | |
switch self { | |
case .Zero: | |
return 0 | |
case .Hundred: | |
return 100 | |
case .Value(let value): | |
return value | |
} | |
} | |
var description : String { | |
return String(value) | |
} | |
} | |
enum ComposedType<P : PrimitiveType, T : DataType> : DataType { | |
case Left(Box<P>) | |
case Right(Box<T>) | |
var description : String { | |
switch self { | |
case .Left(let box): | |
return box.value.description | |
case .Right(let box): | |
return box.value.description | |
} | |
} | |
init(_ value: P) { | |
self = .Left(Box(value)) | |
} | |
init(_ value: T) { | |
self = .Right(Box(value)) | |
} | |
} | |
var state : ComposedType<OnOffType, ComposedType<UndefinedType, PercentType>> | |
state = ComposedType(OnOffType.On) | |
state.description // => "On" | |
state = ComposedType(ComposedType(PercentType.Value(37))) | |
state.description // => "37" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment