SetAlgebraType.union should have an operator

Originator:robnapier
Number:rdar://23516367 Date Originated:12-Nov-2015 10:15 AM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode 7.2 beta (&c46t)
Classification:Enhancement Reproducible:Not Applicable
 
SetAlgebraType, especially when used as OptionSetType, is excessively verbose when creating a union of values. This is an very common operation, and basically the whole point of OptionSetType.

For example, UIBezierPath(roundedRect:byRoundingCorners:cornerRadii), currently has to be written as:

let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 100, height: 100),
    byRoundingCorners: UIRectCorner.TopLeft.union(.TopRight).union(.BottomRight),
    cornerRadii: CGSize(width: 5, height: 5))

This buries what’s going on, and can’t even type-infer UIRectCorner.

With an operator, this is much easier to read, matches ObjC practice, and gives us type-inference back:

public func |<T: SetAlgebraType>(lhs: T, rhs: T) -> T {
    return lhs.union(rhs)
}

let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 100, height: 100),
    byRoundingCorners: .TopLeft | .TopRight | .BottomRight,
    cornerRadii: CGSize(width: 5, height: 5))


For completeness: 

@warn_unused_result
public func |=<T : SetAlgebraType>(inout lhs: T, rhs: T) {
    lhs.unionInPlace(rhs)
}

Comments

This isn't really necessary. The better technique is [.TopLeft, .TopRight, .BottomRight], which is fine syntax.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!