This table presents all sane possibilities how comparing some to none can work:
some>none some<none none>none none<none comment
false false false false // as Float.nan
true false false false // "something is more than nothing"
false true false false // "something is less than nothing"
nil nil nil nil // Optional<Boolean>
Float.nan
would be "compatible" with Float's nan
, "something is more/less and than nothing" would give some reasonable sort order, although strictly speaking it's not necessary. Optional<Boolean>
(returning nil) is perhaps the most "safe" option (although it won't meet Comparable
conformance that needs bare Bool
).
⮕ As Ben has pointed out above, "something is more/less than nothing" is the behaviour we have with "normal" enum types:
enum MyOptional1: Comparable {
case none
case some(Int)
}
enum MyOptional2: Comparable {
case some(Int)
case none
}
MyOptional1.some(0) > .none // true
MyOptional1.some(0) < .none // false
MyOptional1.none > .none // false
MyOptional1.none < .none // false
MyOptional2.some(0) > .none // false
MyOptional2.some(0) < .none // true
MyOptional2.none > .none // false
MyOptional2.none < .none // false