[swift-evolution] [swift-corelibs-dev] Proposal: Conforming NSDate to Comparable

Chris Amanse christopheramanse at gmail.com
Thu Dec 10 02:12:40 CST 2015


In addition to what Stephen said, it seems that it would be good to use
comparison operators on objects that can be represented or visualized as a
value like NSDate, NSIndexPath, and NSNumber. Although, using comparison
operators on NSString or Strings would not be much clear to a reader of the
code since there are lots of ways that we can compare strings, such as
character count, or alphabetical order while being case insensitive, etc.

Chris

On Mon, Dec 7, 2015 at 1:48 AM Stephen Celis <stephen.celis at gmail.com>
wrote:

> Seems to me that anything with `-compare:` could conform to Comparable.
> The list, however, is surprisingly short, and half bridge over to native
> Swift types that are already Comparable:
>
> - [NSDate compare:]
> - [NSIndexPath compare:]
> - [NSNumber compare:]
> - [NSString compare:]
>
> Looks like NSIndexPath may be the only other contender.
>
> Stephen
>
> On Dec 6, 2015, at 11:47 AM, Tony Parker via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> Hi Chris,
>
> Thanks for bringing this idea to the list.
>
> Is NSDate really the only thing that we want to conform to comparable, or
> is there a set of classes which should do so? I’d like to think about the
> larger set of consequences here (and if possible, deal with one coherent
> answer for all of Foundation rather than do these classes one at a time).
>
> - Tony
>
> On Dec 5, 2015, at 3:36 PM, Chris Amanse via swift-corelibs-dev <
> swift-corelibs-dev at swift.org> wrote:
>
> Hello Swift Developers,
>
> I think it's a good idea to conform NSDate to the Comparable protocol, so
> instead of using:
>
> if someDate.compare(today) == .OrderedAscending { }
>
> Developers can easily compare using compare dates using comparison
> operators:
>
> if someDate < today { }
>
> In my opinion, the code is still readable if developers use comparison
> operators on NSDates.
>
> Here's a quick implementation from my swift-corelibs-foundation fork:
> https://github.com/chrisamanse/swift-corelibs-foundation/commit/3c4eff643c5271de5bec2461798051347be13916
>
> Thank you.
>  _______________________________________________
> swift-corelibs-dev mailing list
> swift-corelibs-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev
>
>
>  _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151210/89b221e4/attachment.html>


More information about the swift-evolution mailing list