Failable

public struct Failable<T, Validations> where Validations: Validation, Validations.Supported == T

A type that can fail when being set because the new value does pass certain validations.

You can create a Failable instance in 2 ways. The first is to use the Failable initializer:

try Failable<String, Length1028>("the quick brown fox...")

Or you can call .failable on any intance of a type conforming to CustomStringConvertible:

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. 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:

var story = try "the quick brown fox...".failable(Length1028.self)
try story <~ "Long long ago, on a pencil far far away..."

Use the .value property to access the actual value:

var story = try "the quick brown fox...".failable(Length1028.self)
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.

  • The underlaying value that has been validated.

    Note

    The setter for this property is not public.

    Declaration

    Swift

    public internal(set) var value: T
  • Creates a new Failable instance.

    Declaration

    Swift

    public init(_ t: T)throws

    Parameters

    t

    The orginal value for the instance. This value will be validated and the initializer will fail if it doesn’t pass.

  • Gets the value of a property of the value property using a key path.

    Declaration

    Swift

    public subscript<Value>(keyPath path: KeyPath<T, Value>) -> Value

    Parameters

    path

    The key path of the property to get from the value property.

  • Declaration

    Swift

    public static func == (lhs: Failable<T, Validations>, rhs: Failable<T, Validations>) -> Bool
  • Declaration

    Swift

    public static func < (lhs: Failable<T, Validations>, rhs: Failable<T, Validations>) -> Bool
  • See FixedWidthInteger.bigEndian.

    Warning

    This property has no failing options, so your program will crash if it produces a value that does not pass validation.

    Declaration

    Swift

    public var bigEndian: Failable<T, Validations>
  • A representation of this integer with the byte order swapped.

    Warning

    This property has no failing options, so your program will crash if it produces a value that does not pass validation.

    Declaration

    Swift

    public var byteSwapped: Failable<T, Validations>
  • Declaration

    Swift

    public var leadingZeroBitCount: Int
  • See FixedWidthInteger.littleEndian.

    Warning

    This property has no failing options, so your program will crash if it produces a value that does not pass validation.

    Declaration

    Swift

    public var littleEndian: Failable<T, Validations>
  • Declaration

    Swift

    public var nonzeroBitCount: Int
  • Declaration

    Swift

    public static var bitWidth: Int
  • max

    See FixedWidthInteger.max.

    Warning

    This property has no failing options, so your program will crash if it produces a value that does not pass validation.

    Declaration

    Swift

    public static var max: Failable<T, Validations>
  • min

    See FixedWidthInteger.min.

    Warning

    This property has no failing options, so your program will crash if it produces a value that does not pass validation.

    Declaration

    Swift

    public static var min: Failable<T, Validations>
  • Declaration

    Swift

    public static func < (lhs: Failable<T, Validations>, rhs: Failable<T, Validations>) -> Bool
  • Declaration

    Swift

    public static func <= (lhs: Failable<T, Validations>, rhs: Failable<T, Validations>) -> Bool
  • Declaration

    Swift

    public static func > (lhs: Failable<T, Validations>, rhs: Failable<T, Validations>) -> Bool
  • Declaration

    Swift

    public static func >= (lhs: Failable<T, Validations>, rhs: Failable<T, Validations>) -> Bool
  • Declaration

    Swift

    public static func * (_ lhs: Failable<T, Validations>, _ rhs: Failable<T, Validations>)throws -> Failable<T, Validations>
  • Declaration

    Swift

    public static func *= (_ lhs: inout Failable<T, Validations>, _ rhs: Failable<T, Validations>)throws
  • Declaration

    Swift

    public static func + (_ lhs: Failable<T, Validations>, _ rhs: Failable<T, Validations>)throws -> Failable<T, Validations>
  • Declaration

    Swift

    public static func += (_ lhs: inout Failable<T, Validations>, _ rhs: Failable<T, Validations>)throws
  • Declaration

    Swift

    public prefix static func - (_ x: Failable<T, Validations>)throws -> Failable<T, Validations>
  • Declaration

    Swift

    public static func - (_ lhs: Failable<T, Validations>, _ rhs: Failable<T, Validations>)throws -> Failable<T, Validations>
  • Declaration

    Swift

    public static func -= (_ lhs: inout Failable<T, Validations>, _ rhs: Failable<T, Validations>)throws
  • Declaration

    Swift

    public static func / (_ lhs: Failable<T, Validations>, _ rhs: Failable<T, Validations>)throws -> Failable<T, Validations>
  • Declaration

    Swift

    public static func /= (_ lhs: inout Failable<T, Validations>, _ rhs: Failable<T, Validations>)throws
  • Declaration

    Swift

    public static func == (_ lhs: Failable<T, Validations>, _ rhs: Failable<T, Validations>) -> Bool
  • Declaration

    Swift

    public func map<U, Wrapped>(_ transform: (Wrapped) throws -> U) rethrows -> U? where T == Optional<Wrapped>
  • Declaration

    Swift

    public func flatMap<U, Wrapped>(_ transform: (Wrapped) throws -> U?) rethrows -> U? where T == Optional<Wrapped>
  • Declaration

    Swift

    public static func == <Wrapped>(lhs: Failable<Wrapped?, Validations>, rhs: Failable<Wrapped?, Validations>)
        -> Bool where T == Optional<Wrapped>, Wrapped: Equatable
  • Declaration

    Swift

    public static func != <Wrapped>(lhs: Failable<Wrapped?, Validations>, rhs: Failable<Wrapped?, Validations>)
        -> Bool where T == Optional<Wrapped>, Wrapped: Equatable
  • See UnsignedInteger.magnitude

    Warning

    This property has no failing options, so your program will crash if it produces a value that does not pass validation.g

    Declaration

    Swift

    public var magnitude: Failable<T, Validations>