FactoryInitializable

public protocol FactoryInitializable

Classes having initializers that actually create derived classes.

To use, make your class conform and forward to init(aliasing:) or init(unsafelyAliasing:) from a convenience init:

public class Base : FactoryInitializable {
  /// Constructs an instance whose dynamic type depends on the value of `one`
  public convenience init(_ one: Bool) {
    self.init(aliasing: one ? Derived1() : Derived2())
  }
}
  • The type of the least-derived class declared to be FactoryInitializable.

    Warning

    Allow the default value to take effect, and do not define or use this type explicitly.

    Declaration

    Swift

    associatedtype FactoryBase : AnyObject, FactoryInitializable = Self

Available where Self: AnyObject

  • init(unsafelyAliasing:) Extension method

    Optimally “creates” an instance that is just another reference to me.

    Requires

    me is Self.

    Taking FactoryBase as a parameter prevents, at compile-time, the category of bugs where me is not derived from the least-derived ancestor of Self conforming to FactoryInitializable.

    However, there are still ways me might not be derived from Self. If you have factory initializers at more than one level of your class hierarchy and you can’t control exactly what is passed here, use init(aliasing:) instead.

    Declaration

    Swift

    public init(unsafelyAliasing me: FactoryBase)
  • init(aliasing:) Extension method

    Safely “creates” an instance that is just another reference to me.

    Requires

    me is Self.

    Declaration

    Swift

    public init(aliasing me: FactoryBase)
  • init(unsafelyBitCasting:) Extension method

    “Creates” an instance that is just another reference to me, regardless of the dynamic type of “me”.

    Warning

    do not use this initializer unless you’re really, absolutely, sure you know what you’re doing; it breaks type safety.

    Declaration

    Swift

    public init(unsafelyBitCasting me: FactoryBase)