Partial

public struct Partial<Wrapped>

Utility for type-safe partial class fields

Concept is very similar to the one implemented in the Javascript superset language TypeScript.

It constructs a type with all properties of Wrapped set to optional. This utility will return a type that represents all subsets of a given type.

  • Initialises a new partial instance, projecting on the given value

    Declaration

    Swift

    public init(backingValue: Wrapped? = nil)

    Parameters

    backingValue

    Value to project

  • Returns or updates the value for the given key, might return nil if not found or an error is thrown

    Declaration

    Swift

    public subscript<ValueType>(key: KeyPath<Wrapped, ValueType>) -> ValueType? { get set }
  • Returns or updates the optional value for the given key, might return nil if not found or an error is thrown

    Declaration

    Swift

    public subscript<ValueType>(key: KeyPath<Wrapped, ValueType?>) -> ValueType? { get set }
  • Returns the value for the given key, but fails if an error is thrown

    Declaration

    Swift

    public subscript<ValueType>(unsafe key: KeyPath<Wrapped, ValueType>) -> ValueType { get }
  • Returns the optional value for the given key, but fails if an error is thrown

    Declaration

    Swift

    public subscript<ValueType>(unsafe key: KeyPath<Wrapped, ValueType?>) -> ValueType { get }
  • Returns or updates the value at a given keypath

    If the value at the keypath is also an instance of Partial it is returned directly. Otherwise a new partial is created, either holding the value if found, or empty if not found.

    Declaration

    Swift

    public subscript<ValueType>(key: KeyPath<Wrapped, ValueType>) -> Partial<ValueType> where ValueType : PartialConvertible { get set }
  • Returns or updates the optional value at a given keypath

    If the value at the keypath is also an instance of Partial it is returned directly. Otherwise a new partial is created, either holding the value if found, or empty if not found.

    Declaration

    Swift

    public subscript<ValueType>(key: KeyPath<Wrapped, ValueType?>) -> Partial<ValueType> where ValueType : PartialConvertible { get set }
  • Returns the value for the given key and casts it into the type declared by it.

    Throws

    • Error.invalidValueType, if the value is not of the expected type
    • Error.missingKey, if the value is unknown

    Declaration

    Swift

    public func value<ValueType>(for key: KeyPath<Wrapped, ValueType>) throws -> ValueType
  • Returns the value for the given key and casts it into the type declared by it.

    Throws

    • Error.invalidValueType, if the value is not of the expected type
    • Error.missingKey, if the value is unknown

    Declaration

    Swift

    public func value<ValueType>(for key: KeyPath<Wrapped, ValueType?>) throws -> ValueType
  • Returns the value for the given key and creates another instance of Partial from it

    Throws

    • Error.invalidValueType, if the value is not of the expected type
    • Error.missingKey, if the value is unknown

    Declaration

    Swift

    public func value<ValueType>(for key: KeyPath<Wrapped, ValueType>) throws -> ValueType where ValueType : PartialConvertible
  • Returns the value for the given key and creates another instance of Partial from it

    Throws

    • Error.invalidValueType, if the value is not of the expected type
    • Error.missingKey, if the value is unknown

    Declaration

    Swift

    public func value<ValueType>(for key: KeyPath<Wrapped, ValueType?>) throws -> ValueType where ValueType : PartialConvertible