HBExtensions

public struct HBExtensions<ParentObject>

Extend objects with additional member variables

If you have only one instance of a type to attach you can extend it to conform to StorageKey

struct Object {
    var extensions: Extensions<Object>
}

extension Object {
    var extra: Extra? {
        get { return extensions.get(\.extra) }
        set { extensions.set(\.extra, value: newValue) }
    }
}
  • Initialize extensions

    Declaration

    Swift

    public init()
  • Get optional extension from a KeyPath

    Declaration

    Swift

    public func get<Type>(_ key: KeyPath<ParentObject, Type>) -> Type?
  • Get extension from a KeyPath

    Declaration

    Swift

    public func get<Type>(_ key: KeyPath<ParentObject, Type>, error: StaticString? = nil) -> Type
  • Return if extension has been set

    Declaration

    Swift

    public func exists<Type>(_ key: KeyPath<ParentObject, Type>) -> Bool
  • Set extension for a KeyPath

    Declaration

    Swift

    public mutating func set<Type>(_ key: KeyPath<ParentObject, Type>, value: Type, shutdownCallback: ((Type) throws -> Void)? = nil)

    Parameters

    key

    KeyPath

    value

    value to store in extension

    shutdownCallback

    closure to call when extensions are shutsdown