Poly

public protocol Poly

Poly is a protocol to which types that are polymorphic belong to.

Specifically, Poly1, Poly2, Poly3, etc. types conform to the Poly protocol. These types allow typesafe grouping of a number of disparate types under one roof.

Access

You can access the value of a Poly type in one of four different ways.

  1. You can switch over its cases

    switch poly2Value {
    case .a(let value):
        // value is of type `A`
    case .b(let value):
        // value is of type `B`
    }
    
  2. You can ask for a value by accessor

    let value1 = poly2Value.a // value1 is of type `A?`
    let value2 = poly2Value.b // value2 is of type `B?`
    
  3. You can ask for a value by type

    let value1 = poly2Value[A.self] // value1 is of type `A?`
    let value2 = poly2Value[B.self] // value2 is of type `B?`
    
  4. You can ask for a type-erased value

    let value = poly2Value.value // value is of type `Any`
    
  • Get a type-erased value.

    Declaration

    Swift

    var value: Any { get }
  • Get a list of all types this Poly supports.

    Declaration

    Swift

    static var allTypes: [Any.Type] { get }