Why is Array not AdditiveArithmetic

Why is the Array not AdditiveArithmetic by default despite the fact that he has function?

public static func + (lhs: [Element], rhs: [Element]) -> [Element]

Why there is no way to use + with Collections?

func plusCollections<C: Collection>(lhs: C, rhs: C) {
    let z = lhs + rhs // Error
}

Why there is not allow by default to use plus with optional Arrays?

func optionalArraysAdd<Element>(lhs: [Element]?, rhs: [Element]?) {
    let z = lhs + rhs // Error
}

There's also a minus part (-), which would be very stretching to include in the Array APIs.

Not all collections can be concatenated, or even mutated. A CollectionOfOne is one good example that doesn't allow arbitrary concatenation.

Even then, chances are, you don't want/need both collections have the same type. We also have + on RangeReplaceableCollection.

In Swift, there's a distinction between not having an array (nil) and having one that is empty ([]). A good rule-of-thumb is that, if you do nothing when it's optional, you can just take non-optional value, and let the caller deal with optional.

2 Likes

Protocols in Swift have semantic meaning They are not just groups of syntax.

The documentation for AdditiveArithmetic states:

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.

Array is not a scalar nor, in general, a mathematical vector (although it may be used to implement vector values such as matrices or tensors). Adding two matrices is a different operation to concatenating two Arrays. A similar argument applies to String, which also has a concatenating + operator.

5 Likes

Yep. I forgot to scroll down the docs...

As another poster said, Swift protocols aren't just bags of syntax. Conforming to a protocol is a promise to maintain its semantics. The problem is that there are two forms of "+". One is a customization point for AdditiveArithmetic; the other is a set of extension methods for RangeReplaceableCollection. The Array type uses the ones from RRC, and said choice is incompatible with the operator in AA.

Even if the RRC versions weren't present in Array, what would an AA version do when the operands have different element counts? (This assumes that you want an AA Array to add corresponding elements together, and not concatenate the operands.)

1 Like

Thanks. So for unify the operator, RangeReplaceableCollection must inherits from AdditiveArithmetic?

What’s the point of unifying the + operator? They mean totally different in each context. And it’s not like we have a lot of things that can conform to both.

1 Like

But scalar and value are very similar term or not?

Yeah you’re right. What I meant to say is that Arrays don’t in general model mathematical vectors. So Array’s + operator doesn’t mean “addition”, it means “concatenation” - and they are very different things.

For example, addition is commutative, so a + b must give the same result as b + a. Array’s + operator doesn’t do that.

1 Like