I've had trouble finding/understanding documentation on how to compare enums in Swift by their order of definition. Specifically when I create an enumeration such as
enum EnumType {
case First, Second, Third
}
Swift does not allow me to directly compare enums by order, such as
let type1 = EnumType.First
let type2 = EnumType.Second
if type1 < type2 {println("good")} // error
it generates the compile error "cannot invoke '<' with argument list of of type {EnumType, EnumType}. So the only solution I've found is to write my own comparison operators as overloads, such as
enum EnumType : Int {
case First = 0, Second, Third
}
func <(a: EnumType, b: EnumType) -> Bool {
return a.rawValue < b.rawValue
}
let type1 = EnumType.First
let type2 = EnumType.Second
if type1 < type2 {println("good")} // Returns "good"
This is all well and good for "heavy weight" enums that have a lot of use and value in my application, but overloading all the operators I might want to use seems excessively burdensome for 'lightweight" enums that I might define on the fly to bring order to some constants for a single small module.
Is there way to do this without writing lots of boilerplate overloading code for every enum type I define in my project? Even better, is there something I'm missing to make Swift automatically provide comparison operators for simple enums that don't have associated types, ie. that are untyped or typed as Int? Swift knows how to compare Ints, so why can't it compare enum Ints?