[swift-users] hashValue of enum cases (was Reducing Array<OptionSet> to OptionSet)
Fritz Anderson
fritza at manoverboard.org
Fri Nov 4 15:17:39 CDT 2016
On 3 Nov 2016, at 8:37 PM, Erica Sadun via swift-users <swift-users at swift.org <mailto: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/ <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/32d14870/attachment.html>
More information about the swift-users
mailing list