-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Make NSDate conform to Comparable protocol #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
In order to provide more “Swifty” API for comparing instances of ‘NSDate’, all methods declared in ‘Comparable’ protocol are implemented for ’NSDate’.
👏 |
} | ||
|
||
public func >=(lhs: NSDate, rhs: NSDate) -> Bool { | ||
return lhs > rhs || lhs == rhs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @tomaskraina. Great, simple and readable solution 👍
I'd like to suggest a small performance optimisation here: if lhs
and rhs
are equal you actually call the compare method 2 times on the same pair. Once inside the >
method and then once again in ==
. Instead, you can save the result of compare
and use a switch
statement to accept the valid cases or even use some pattern matching magic :-)
Since this is the foundation library, IMO we need to be conscious about performance related decisions, even if they're small.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tomaskraina, isn't implementing ==
and <
enough to make it conform to Comparable
? Anyway, this is the same as pull request #19.
…erived automatically.
…dation # Conflicts: # Foundation.xcodeproj/project.pbxproj # TestFoundation/main.swift
@lorentey good point! Updated. |
This pull request contains an API change, so let’s follow the Swift evolution process, starting with discussing the idea on the |
[build-system] Various minor refactoring and cleanups
[REPL] Test RPATH is set correctly on linux.
[pull] swiftwasm from master
It would be nice to provide more natural way to compare instances of
NSDate
-- using<
,<=
,>
, and>=
operators. This PR implements all methods declared inComparable
protocol forNSDate
class.