Swift: Make RawOptionSetType Hashable

Originator:wetzeal
Number:rdar://20017776 Date Originated:02-Mar-2015 11:13 PM
Status:Duplicate/15032042/Open Resolved:
Product:Developer Tools Product Version:Xcode-beta (6D532l)
Classification:Enhancement Reproducible:Always
 
Summary:
RawOptionSetType is primarily used to represent NS_OPTIONS enums from Objective-C. These options sets use a UInt as their underlying data type. UInt is hashable. RawOptionSetType, however, is not Hashable, nor is the type definition of its RawValue property, despite (as far as I can tell) all extant RawOptionSetTypes having Hashable RawValues.

Making RawOptionSetType Hashable would greatly improve interoperating between Cocoa libraries that treat NS_OPTIONS as sets of configuration parameters by allowing Swift libraries to collect options in Sets and Dictionaries.

Steps to Reproduce:
Attempt to make a Set of members of a RawOptionSetType type:
let options: Set<UIViewAnimationOptions> = [.LayoutSubviews, .TransitionFlipFromLeft]

Expected Results:
A set is constructed containing the specified values

Actual Results:
error: type 'UIViewAnimationOptions' does not conform to protocol 'Hashable'

Notes:
As Swift protocols cannot be extended, it is impossible for external developers to add Hashable to RawOptionSetType. Hashable can be implemented on a case-by-case basis, but this is not a viable long-term solution:
extension UIViewAnimationOptions: Hashable {
    public var hashValue: Int { return rawValue.hashValue }
}

Comments


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!