[swift-users] hashValue of enum cases (was Reducing Array<OptionSet> to OptionSet)

Erica Sadun erica at ericasadun.com
Fri Nov 4 16:04:09 CDT 2016


It is absolutely an implementation detail and one you should never rely upon! 

— Erica

Sent from my iPhone

> On Nov 4, 2016, at 2:17 PM, Fritz Anderson <fritza at manoverboard.org> wrote:
> 
>> On 3 Nov 2016, at 8:37 PM, Erica Sadun via swift-users <swift-users at swift.org> wrote:
>> 
>>     private enum StringEnum: String { case one, two, three }
>>     public init(strings: [String]) {
>>         var set = MyOptionSet()
>>         strings.flatMap({ StringEnum(rawValue: $0) })
>>             .flatMap({ MyOptionSet(rawValue: 1 << $0.hashValue) })
>>             .forEach { set.insert($0) }
>>         _rawValue = set.rawValue
>>     }
> 
> I’m curious about relying on the hash value of an enum case being its declaration-order index. A sage (http://ericasadun.com/2015/07/12/swift-enumerations-or-how-to-annoy-tom/) warns that this  is an implementation detail. I haven’t seen anything saying it is API. Has it been resolved?
> 
> It’s the most plausible implementation, but I’d think code that relies on case order would break silently (likely at widely-separated locations) if a case were inserted or removed. That suggests to me it’s not possible to regularize this behavior.
> 
> Folkloric API (like SEL ↔︎ char* in ObjC) makes me itch.
> 
> 	— F
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161104/741bd466/attachment.html>


More information about the swift-users mailing list