Protocols
The following protocols are available globally.
-
A type that represents a measurement that can be expressed in various units.
See
Anglefor an example.Requires
AMeasurement’s units must be definable as ratios of one another. (For example,Measurementcan 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 }
Declaration
Swift
public protocol Measurement : Addable, Comparable, Equatable, Negatable, NumericAdditiveArithmetic
-
A
Comparabletype that can be used with+(_:_:)and−(_:_:)in conjunction with an associatedVectortype.Note
For multi‐dimensional points, seePointType.Conformance Requirements:
PointTypeComparable
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:
VectorTypestatic func ÷ (lhs: Self, rhs: Self) -> Scalar
Declaration
Swift
public protocol OneDimensionalVector : VectorType
-
A type that can be used with
+(_:_:)and−(_:_:)in conjunction with an associatedVectortype.Note
UnlikeStrideable,PointTypes do not need to conform toComparable, allowing conformance by two‐dimensional points, etc.Conformance Requirements:
Equatablestatic func += (lhs: inout Self, rhs: Vector)static func − (lhs: Self, rhs: Self) -> Vector
Declaration
Swift
public protocol PointType : Equatable
-
An value that can be used with ×(:) and ÷(::) in conjunction with a scalar.
Conformance Requirements:
AdditiveArithmeticstatic func ×= (lhs: inout Self, rhs: Scalar)static func ÷= (lhs: inout Self, rhs: Scalar)
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
Randomizerthe 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
Randomizerand use its interface anyway.Conformance Requirements:
func randomNumber() -> UInt64
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)
Declaration
Swift
public protocol Addable
-
A value that can be added and subtracted.
Note
UnlikeSignedNumber,AdditiveArithmetictypes do not need to conform toComparable, allowing conformance by two‐dimensional vectors, etc. For additional behaviour specific to one‐dimensional types, seeNumericAdditiveArithmetic.Conformance Requirements:
EquatableSubtractableExpressibleByIntegerLiteralorstatic var additiveIdentity: Self { get }
Declaration
Swift
public protocol AdditiveArithmetic : Equatable, Subtractable
-
Declaration
Swift
public protocol IntegerType : IntegralArithmetic
-
A type that can be additively inverted.
Note
UnlikeSignedNumber,Negatabletypes do not need to conform toComparable, allowing conformance by two‐dimensional vectors, etc.Conformance Requirements:
See moreDeclaration
Swift
public protocol Negatable : AdditiveArithmetic
-
An one‐dimensional value that can be added and subtracted.
Note
UnlikeWholeArithmetic,NumericAdditiveArithmeticdoes not need a defined scale, allowing conformance by measurements that can use multiple units.Conformance Requirements:
AdditiveArithmeticComparableNegatable,WholeNumberTypeormutating func formAbsoluteValue()
Declaration
Swift
public protocol NumericAdditiveArithmetic : AdditiveArithmetic, Comparable
-
A type that can be used for rational arithmetic.
Conformance Requirements:
IntegralArithmeticExpressibleByFloatLiteralstatic func ÷= (lhs: inout Self, rhs: Self)
Declaration
Swift
public protocol RationalArithmetic : ExpressibleByFloatLiteral, IntegralArithmetic
-
Declaration
Swift
public protocol RationalNumberType : RationalArithmetic
-
A type that can be used for real arithmetic.
Conformance Requirements:
RationalArithmeticstatic var π: Self { get }static var e: Self { get }mutating func formLogarithm(toBase base: Self)static func sin(_ angle: Angle<Self>) -> Selfstatic func arctan(_ tangent: Self) -> Angle<Self>
Declaration
Swift
public protocol RealArithmetic : RationalArithmetic
View on GitHub
Protocols Reference