Added literal initializations to Failable type

This commit is contained in:
Caleb Kleveter 2018-11-28 12:19:56 -06:00
parent 866aaa04e4
commit 75d4fd9f45
No known key found for this signature in database
GPG Key ID: B38DBD5CF2C98D69
2 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,80 @@
extension Failable: ExpressibleByIntegerLiteral where T: ExpressibleByIntegerLiteral {
/// See [`ExpressibleByIntegerLiteral.IntegerLiteralType`](https://developer.apple.com/documentation/swift/expressiblebyintegerliteral/2295254-integerliteraltype).
public typealias IntegerLiteralType = T.IntegerLiteralType
/// See [`ExpressibleByIntegerLiteral.init(integerLiteral:)`](https://developer.apple.com/documentation/swift/expressiblebyintegerliteral/2298913-init).
public init(integerLiteral value: T.IntegerLiteralType) {
let t = T.init(integerLiteral: value)
try! self.init(t)
}
}
extension Failable: ExpressibleByFloatLiteral where T: ExpressibleByFloatLiteral {
/// See [`ExpressibleByFloatLiteral.FloatLiteralType`](https://developer.apple.com/documentation/swift/expressiblebyfloatliteral/2296813-floatliteraltype).
public typealias FloatLiteralType = T.FloatLiteralType
/// See [`ExpressibleByFloatLiteral.init(floatLiteral:)`](https://developer.apple.com/documentation/swift/expressiblebyfloatliteral/2294405-init).
public init(floatLiteral value: T.FloatLiteralType) {
let t = T.init(floatLiteral: value)
try! self.init(t)
}
}
extension Failable: ExpressibleByBooleanLiteral where T: ExpressibleByBooleanLiteral {
/// See [`ExpressibleByBooleanLiteral.BooleanLiteralType`](https://developer.apple.com/documentation/swift/expressiblebybooleanliteral/2296130-booleanliteraltype).
public typealias BooleanLiteralType = T.BooleanLiteralType
/// See [`ExpressibleByBooleanLiteral.init(booleanLiteral:)`](https://developer.apple.com/documentation/swift/expressiblebybooleanliteral/2296011-init).
public init(booleanLiteral value: T.BooleanLiteralType) {
let t = T.init(booleanLiteral: value)
try! self.init(t)
}
}
extension Failable: ExpressibleByNilLiteral where T: ExpressibleByNilLiteral {
/// See [`ExpressibleByNilLiteral.init(nilLiteral:)`](https://developer.apple.com/documentation/swift/expressiblebynilliteral).
public init(nilLiteral: ()) {
let t = T.init(nilLiteral: ())
try! self.init(t)
}
}
extension Failable: ExpressibleByStringLiteral where T: ExpressibleByStringLiteral {
/// See [`ExpressibleByStringLiteral.StringLiteralType`](https://developer.apple.com/documentation/swift/expressiblebystringliteral/2295780-stringliteraltype).
public typealias StringLiteralType = T.StringLiteralType
/// See [`ExpressibleByStringLiteral.init(stringLiteral:)`](https://developer.apple.com/documentation/swift/expressiblebystringliteral/2294174-init)
public init(stringLiteral value: T.StringLiteralType) {
let t = T.init(stringLiteral: value)
try! self.init(t)
}
}
extension Failable: ExpressibleByExtendedGraphemeClusterLiteral where T: ExpressibleByExtendedGraphemeClusterLiteral {
/// See [`ExpressibleByExtendedGraphemeClusterLiteral.ExtendedGraphemeClusterLiteralType`](https://developer.apple.com/documentation/swift/expressiblebyextendedgraphemeclusterliteral/2297729-extendedgraphemeclusterliteralty).
public typealias ExtendedGraphemeClusterLiteralType = T.ExtendedGraphemeClusterLiteralType
/// See [`ExpressibleByExtendedGraphemeClusterLiteral.init(extendedGraphemeClusterLiteral:)`](https://developer.apple.com/documentation/swift/expressiblebyextendedgraphemeclusterliteral/2294280-init).
public init(extendedGraphemeClusterLiteral value: T.ExtendedGraphemeClusterLiteralType) {
let t = T.init(extendedGraphemeClusterLiteral: value)
try! self.init(t)
}
}
extension Failable: ExpressibleByUnicodeScalarLiteral where T: ExpressibleByUnicodeScalarLiteral {
/// See [`ExpressibleByUnicodeScalarLiteral.UnicodeScalarLiteralType`](https://developer.apple.com/documentation/swift/expressiblebyunicodescalarliteral/2297763-unicodescalarliteraltype).
public typealias UnicodeScalarLiteralType = T.UnicodeScalarLiteralType
/// See [`ExpressibleByUnicodeScalarLiteral.init(unicodeScalarLiteral:)`](https://developer.apple.com/documentation/swift/expressiblebyunicodescalarliteral/2296043-init).
public init(unicodeScalarLiteral value: T.UnicodeScalarLiteralType) {
let t = T.init(unicodeScalarLiteral: value)
try! self.init(t)
}
}

View File

@ -8,6 +8,8 @@
/// ///
/// try "the quick brown fox...".failable(Length1028.self) /// try "the quick brown fox...".failable(Length1028.self)
/// ///
/// ## Mutation
///
/// A `Failable` type is a struct, so the stored value can only be mutated if the `Failable` instance is a variable. /// A `Failable` type is a struct, so the stored value can only be mutated if the `Failable` instance is a variable.
/// The stored value does not make its setter public, because then you would be able to set the value directly and bypass the validations. /// The stored value does not make its setter public, because then you would be able to set the value directly and bypass the validations.
/// Instead you use the `<~` operator to assign a new value: /// Instead you use the `<~` operator to assign a new value:
@ -19,6 +21,18 @@
/// ///
/// var story = try "the quick brown fox...".failable(Length1028.self) /// var story = try "the quick brown fox...".failable(Length1028.self)
/// print(story.value) /// print(story.value)
///
/// ## Literal Initialization
///
/// `Failable` supprts initialization with certain type literals if the `value` type `T` also supports it.
/// Initialization is supported for `Int`, `Float`, `Bool`, `nil`, and `String` types.
///
/// let string = Failable<String, EmptyValidationM<String>> = "Hello world"
///
/// - Warning: Because literal initializers cannot fail, your program will crash if the value passed in does not pass validation.
///
/// `Dictionary` and `Array` types are not supported for literal initialization yet because array
/// splatting for variadic parameters is not supported yet.
public struct Failable<T, Validations> where Validations: Validation, Validations.Supported == T { public struct Failable<T, Validations> where Validations: Validation, Validations.Supported == T {
/// The underlaying value that has been validated. /// The underlaying value that has been validated.