December 2018
Intermediate to advanced
414 pages
10h 19m
English
In the following example, we will build a fully generic light:
enum State<T>: Equatable where T: Equatable { case on case off case dimmed(T)}struct Bits8Dimming: Equatable { let value: Int init(_ value: Int) { assert(value > 0 && value < 256) self.value = value }}struct ZeroOneDimming: Equatable { let value: Double init(_ value: Double) { assert(value > 0 && value < 1) self.value = value }}let nostalgiaState: State<Bits8Dimming> = .dimmed(.init(10))let otherState: State<ZeroOneDimming> = .dimmed(.init(0.4))
The dim type is now specified as a part of the State type. This gives us a lot of flexibility, as well as validation. Wrapping the value into a small struct adds very little overhead in terms of performance, and allows us ...
Read now
Unlock full access