Swift Enumeration type with associated value does not easily conform to Comparable
| Originator: | GriotSpeak | ||
| Number: | rdar://17497529 | Date Originated: | 2014-06-28 |
| Status: | Closed as duplicate | Resolved: | |
| Product: | Developer Tools | Product Version: | |
| Classification: | Reproducible: |
Summary:
The current restriction that enumerations with associated values cannot have a backing type uneccessarily complicates conforming to Comparable which is desirable for Enums.
One way to enable this is to make enums that have associated values backed by Int with fixed values decided strictly by order of declaration starting at 0. This would allow access to toRaw() which would simplify comparison of discriminated type enums.
a new method, toTuple() could return a tuple for discriminated types. This would allow simple unpacking for cases where the raw value is not enough to determine order. this method could, optionally, include the raw value as the first member of the tuple.
Steps to Reproduce:
Try to create a succinct comparison implementation for an enumeration with associated values.
Expected Results:
enum Position {
case Valid(Int, String, Int, Int)
case None
case Builtin
case Internal
}
@infix func <= (lhs:Position, rhs:Position) -> Bool {
let left = lhs.toRaw()
let right = rhs.toRaw()
if left <= right {
return true
} else if (left > right) || (left != Position.Valid.toRaw()) {
return false
}
let (lFoo, lBar, lBaz, lQux) = lhs.toTuple()
let (rFoo, rBar, rBaz, rQux) = rhs.toTuple()
// figure out complex comparison
return ...
}
Actual Results:
This functionality is unavailable.
Version:
Notes:
Configuration:
Attachments:
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!