Protocols

The following protocols are available globally.

  • A type that represents a measurement that can be expressed in various units.

    See Angle for an example.

    Requires

    A Measurement’s units must be definable as ratios of one another. (For example, Measurement can describe angles as radians, degrees and gradians, but not temperature as Kelvins, Celsius and Fahrenheit.)

    Conformance Requirements:

    • init(rawValue: Scalar)
    • var rawValue: Scalar { get set }
    See more

    Declaration

    Swift

    public protocol Measurement : Addable, Comparable, Equatable, Negatable, NumericAdditiveArithmetic
  • A Comparable type that can be used with +(_:_:) and −(_:_:) in conjunction with an associated Vector type.

    Note

    For multi‐dimensional points, see PointType.

    Conformance Requirements:

    See more

    Declaration

    Swift

    public protocol OneDimensionalPoint : Comparable, PointType, Strideable
  • A one‐dimensional value that can be used with ×(:) and ÷(::) in conjunction with a scalar.

    Conformance Requirements:

    • VectorType
    • static func ÷ (lhs: Self, rhs: Self) -> Scalar
    See more

    Declaration

    Swift

    public protocol OneDimensionalVector : VectorType
  • A type that can be used with +(_:_:) and −(_:_:) in conjunction with an associated Vector type.

    Note

    Unlike Strideable, PointTypes do not need to conform to Comparable, allowing conformance by two‐dimensional points, etc.

    Conformance Requirements:

    • Equatable
    • static func += (lhs: inout Self, rhs: Vector)
    • static func − (lhs: Self, rhs: Self) -> Vector
    See more

    Declaration

    Swift

    public protocol PointType : Equatable
  • An value that can be used with ×(:) and ÷(::) in conjunction with a scalar.

    Conformance Requirements:

    • AdditiveArithmetic
    • static func ×= (lhs: inout Self, rhs: Scalar)
    • static func ÷= (lhs: inout Self, rhs: Scalar)
    See more

    Declaration

    Swift

    public protocol VectorType : AdditiveArithmetic
  • A class that generates random numbers.

    The definition of “random” is up to the particular conforming class. For example, instead of actual random numbers, a class could return a cyclical pattern or a single repeating value. This makes it easy to swap one class out for another, such as for testing or for distinct game modes.

    For every SDG class that conforms to Randomizer, two independent instances that have been initialized the same way will return the same sequence of values.

    For example, in a networked game, as long as each device initializes their instance of the Randomizer the same, each device can safely rely on its local instance to return the same values as are being returned on the other devices with no risk of the game states diverging.

    Warning

    The above guarantee of deterministic behaviour does not apply between differing module versions.

    Note

    Nothing in SDG relies on the above guarantee. If you make your own conforming class and do not need or want it to be deterministic, you can safely conform to Randomizer and use its interface anyway.

    Conformance Requirements:

    • func randomNumber() -> UInt64
    See more

    Declaration

    Swift

    public protocol Randomizer : class
  • A type that can be used with +(_:_:).

    The precise behaviour of + depends on the conforming type. It may be arithmetic addition, string concatenation, etc.

    Conformance Requirements:

    • static func += (lhs: inout Self, rhs: Self)
    See more

    Declaration

    Swift

    public protocol Addable
  • A value that can be added and subtracted.

    Note

    Unlike SignedNumber, AdditiveArithmetic types do not need to conform to Comparable, allowing conformance by two‐dimensional vectors, etc. For additional behaviour specific to one‐dimensional types, see NumericAdditiveArithmetic.

    Conformance Requirements:

    • Equatable
    • Subtractable
    • ExpressibleByIntegerLiteral or static var additiveIdentity: Self { get }
    See more

    Declaration

    Swift

    public protocol AdditiveArithmetic : Equatable, Subtractable
  • A type that can be used for integral arithmetic.

    Conformance Requirements:

    • WholeArithmetic
    • Negatable
    • init(_ int: IntMax)
    See more

    Declaration

    Swift

    public protocol IntegralArithmetic : AbsoluteValuable /* requires negatability */, Negatable, SignedNumber, WholeArithmetic
  • A type that can be additively inverted.

    Note

    Unlike SignedNumber, Negatable types do not need to conform to Comparable, allowing conformance by two‐dimensional vectors, etc.

    Conformance Requirements:

    See more

    Declaration

    Swift

    public protocol Negatable : AdditiveArithmetic
  • An one‐dimensional value that can be added and subtracted.

    Note

    Unlike WholeArithmetic, NumericAdditiveArithmetic does not need a defined scale, allowing conformance by measurements that can use multiple units.

    Conformance Requirements:

    See more

    Declaration

    Swift

    public protocol NumericAdditiveArithmetic : AdditiveArithmetic, Comparable
  • A type that can be used for real arithmetic.

    Conformance Requirements:

    • RationalArithmetic
    • static var π: Self { get }
    • static var e: Self { get }
    • mutating func formLogarithm(toBase base: Self)
    • static func sin(_ angle: Angle<Self>) -> Self
    • static func arctan(_ tangent: Self) -> Angle<Self>
    See more

    Declaration

    Swift

    public protocol RealArithmetic : RationalArithmetic