Created
July 18, 2020 22:55
-
-
Save wookiee/70547c03a0df58b349eed8d600fced9b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Foundation | |
@propertyWrapper public struct Percentage<Value: FloatingPoint> { | |
private var storage: Value | |
private let upperBound: Value | |
public init(wrappedValue: Value, upperBound: Value = (1.0 as! Value)) { | |
self.storage = wrappedValue | |
self.upperBound = upperBound | |
} | |
public var wrappedValue: Value { | |
get { | |
switch storage { | |
case ...0: | |
return 0 | |
case 1...: | |
return 1 | |
default: | |
return storage | |
} | |
} | |
set { | |
assert((0...upperBound).contains(newValue), | |
"Percentage (\(newValue)) > \(upperBound), which is illegal.") | |
storage = newValue | |
} | |
} | |
public var projectedValue: Value { | |
get { | |
return storage | |
} | |
set { | |
storage = newValue | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment