Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Created April 10, 2024 22:37
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 JadenGeller/00c11318deee32118f12d96e123ff520 to your computer and use it in GitHub Desktop.
Save JadenGeller/00c11318deee32118f12d96e123ff520 to your computer and use it in GitHub Desktop.
struct NilMaxInteger<Base: FixedWidthInteger>: RawRepresentable, ExpressibleByNilLiteral {
var rawValue: Base
init(rawValue: Base) {
self.rawValue = rawValue
}
init(_ value: Base?) {
assert(value != .max)
self.init(rawValue: value ?? .max)
}
init(nilLiteral: ()) {
self.init(rawValue: .max)
}
var optionalValue: Base? {
get {
rawValue == .max ? nil : rawValue
}
set {
self = .init(newValue)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment