[Accepted with Revision] SE-0207 Add a containsOnly algorithm to Sequence

Summary

The review of SE-0207: Add a containsOnly Algorithm To Sequence ended April 13th. The core team accepted one of the two proposed Sequence extensions, with a different name than was proposed:

extension Sequence {
  /// Returns a Boolean value indicating whether every element of the sequence
  /// satisfies the given predicate.
  func allSatisfy(_ predicate: (Element) throws -> Bool) rethrows -> Bool
}

Thanks to all who participated for making Swift a better language!

Details

Consensus among core team members was that:

  • The name containsOnly
    • is too easily misread at the use-site as “contains one instance equal to.”
    • is especially confusing when applied to empty sequences, considering that the result must be true.
    • Does not fully address the cognitive difficulty that comes from trying to express these semantics in terms of contains.
  • The name allSatisfy makes usage completely clear.
  • Breaking the base name into all(satisfy:)
    • is not a precedented pattern in our names
    • is not supported by anything in the API guidelines
    • was considered by some to result in unclear usage with trailing closures, e.g. x.all { $0 > 5 }
  • The form of containsOnly that checks all elements of the Sequence for equality to its argument, e.g. x.containsOnly(5)
    • is rarely useful in practice
    • can easily be written as x.allSatisfy { $0 == 5 }
    • can always be added later as x.allEqual(5)
12 Likes
Terms of Service

Privacy Policy

Cookie Policy