- Proposal: SE-NNNN
- Author(s): Chris Amanse
- Status: Draft
- Review manager: TBD
Conform NSDate
to the Comparable
protocol so that developers could simply use comparison operators on NSDate
s instead of using compare(_:) -> NSComparisonResult
method.
Currently, in comparing protocols, developers usually have to extend NSDate
to make it to conform to Comparable
, or use compare(_:) -> NSComparisonResult
method of NSDate
. Using comparison operators on NSDate
s will make the code more readable, such as someDate < today
, rather than someDate.compare(today) == .OrderedAscending
.
To implement this, it is as easy as conforming NSDate
to Comparable
and implementing the global functions <(lhs: NSDate, rhs: NSDate) -> Bool
and ==(lhs: NSDate, rhs: NSDate) -> Bool
.
extension NSDate: Comparable {}
public func ==(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.timeIntervalSinceReferenceDate == rhs.timeIntervalSinceReferenceDate
}
public func <(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate
}
Swift projects may use comparison operators on NSDate
s, however, if they have already extended NSDate
to conform to Comparable
, the code will not compile since it will result to a redundant conformance.
An alternative would be to extend NSDate every time in a Swift project instead of fully integrating NSDate
conformance to Comparable
in Foundation.