A type with values that support addition and subtraction.


protocol AdditiveArithmetic


The AdditiveArithmetic protocol provides a suitable basis for additive arithmetic on scalar values, such as integers and floating-point numbers, or vectors. You can write generic methods that operate on any numeric type in the standard library by using the AdditiveArithmetic protocol as a generic constraint.

The following code declares a method that calculates the total of any sequence with AdditiveArithmetic elements.

extension Sequence where Element: AdditiveArithmetic {
    func sum() -> Element {
        return reduce(.zero, +)

The sum() method is now available on any sequence with values that conform to AdditiveArithmetic, whether it is an array of Double or a range of Int.

let arraySum = [1.1, 2.2, 3.3, 4.4, 5.5].sum()
// arraySum == 16.5

let rangeSum = (1..<10).sum()
// rangeSum == 45

Conforming to the AdditiveArithmetic Protocol

To add AdditiveArithmetic protocol conformance to your own custom type, implement the required operators, and provide a static zero property using a type that can represent the magnitude of any value of your custom type.


Type Properties

static var zero: Self

The zero value.

Required. Default implementation provided.

Operator Functions

static func + (Self) -> Self

Returns the given number unchanged.

static func + (Self, Self) -> Self

Adds two values and produces their sum.


static func += (inout Self, Self)

Adds two values and stores the result in the left-hand-side variable.

Required. Default implementation provided.

static func - (Self, Self) -> Self

Subtracts one value from another and produces their difference.


static func -= (inout Self, Self)

Subtracts the second value from the first and stores the difference in the left-hand-side variable.

Required. Default implementation provided.


Inherits From

See Also

Basic Arithmetic

protocol Numeric

A type with values that support multiplication.

protocol SignedNumeric

A type that can represent both positive and negative values.

protocol Strideable

A type representing continuous, one-dimensional values that can be offset and measured.