OptionSetType and enums
Edit: I'm surprised at my former self for not saying this upfront at the time, but... instead of trying to force other value types into the OptionSet
protocol (Swift 3 removed Type
from the name), it's probably better to consider the API where you use those types and use Set
collections where appropriate.
OptionSet
types are weird. They are both collections and not collections — you can construct one from multiple flags, but the result is still a single value. (You can do some work to figure out a collection-of-single-flags equivalent to such a value, but depending on the possible values in the type it might not be unique.)
On the other hand, being able to have one something, or more than one unique somethings, can be important to the design of an API. Do you want users to say they have more than one favorite, or enforce that there's only one? Just how many "favorites" do you want to allow? If a user claims multiple favorites, should they be ranked in user-specific order? These are all questions that are hard to answer in an OptionSet
-style type, but much easier if you use a Set
type or other actual collection.
The rest of this answer a) is old, using Swift 2 names, and b) assumes that you're trying to implement OptionSet
anyway, even if it's a bad choice for your API...
See the docs for OptionSetType
:
Supplies convenient conformance to
SetAlgebraType
for any type whoseRawValue
is aBitwiseOperationsType
.
In other words, you can declare OptionSetType
conformance for any type that also adopts RawRepresentable
. However, you gain the magic set-algebra syntax support (via operators and ArrayLiteralConvertible
conformance) if and only if your associated raw value type is one that conforms to BitwiseOperationsType
.
So, if your raw value type is String
, you're out of luck — you don't gain the set algebra stuff because String
doesn't support bitwise operations. (The "fun" thing here, if you can call it that, is that you can extend String
to support BitwiseOperationsType
, and if your implementation satisfies the axioms, you can use strings as raw values for an option set.)
Your second syntax errors at runtime because you've created an infinite recursion — calling self.init(rawValue:)
from init(rawValue:)
keeps gong until it blows the stack.
It's arguably a bug (please file it) that you can even try that without a compile time error. Enums shouldn't be able to declare OptionSetType
conformance, because:
The semantic contract of an enum is that it's a closed set. By declaring your
ProgrammingLanguage
enum you're saying that a value of typeProgrammingLanguage
must be one ofSwift
,Scala
, orHaskell
, and not anything else. A value of "Swift and Scala" isn't in that set.The underlying implementation of an
OptionSetType
is based on integer bitfields. A "Swift and Haskell" value, ([.Swift, .Haskell]
) is really just.Swift.rawValue | .Haskell.rawValue
. This causes trouble if your set of raw values isn't bit-aligned. That is, if.Swift.rawValue == 1 == 0b01
, and.Haskell.rawValue == 2 == 0b10
, the bitwise-or of those is0b11 == 3
, which is the same as.Scala.rawValue
.
TLDR: if you want OptionSetType
conformance, declare a struct.
And use static let
to declare members of your type.
And pick your raw values such that members you want to be distinct from possible (bitwise-or) combinations of other members actually are.
struct ProgrammingLanguage: OptionSetType {
let rawValue: Int
// this initializer is required, but it's also automatically
// synthesized if `rawValue` is the only member, so writing it
// here is optional:
init(rawValue: Int) { self.rawValue = rawValue }
static let Swift = ProgrammingLanguage(rawValue: 0b001)
static let Haskell = ProgrammingLanguage(rawValue: 0b010)
static let Scala = ProgrammingLanguage(rawValue: 0b100)
}
Good ways to keep your values distinct: use binary-literal syntax as above, or declare your values with bit shifts of one, as below:
static let Swift = ProgrammingLanguage(rawValue: 1 << 0)
static let Haskell = ProgrammingLanguage(rawValue: 1 << 1)
static let Scala = ProgrammingLanguage(rawValue: 1 << 2)
I guess you could simply achieve it in the modern way {^_^}.
protocol Option: RawRepresentable, Hashable, CaseIterable {}
extension Set where Element: Option {
var rawValue: Int {
var rawValue = 0
for (index, element) in Element.allCases.enumerated() where contains(element) {
rawValue |= (1 << index)
}
return rawValue
}
}
... then
enum ProgrammingLanguage: String, Option {
case Swift, Haskell, Scala
}
typealias ProgrammingLanguages = Set<ProgrammingLanguage>
let programmingLanguages: ProgrammingLanguages = [.Swift, .Haskell]
Reference: https://nshipster.com/optionset/