Pretty sure Array conforms to hashable and ==.

> On Jan 10, 2017, at 23:43, David Sweeris <daveswee...@mac.com> wrote:
> 
> 
> On Jan 11, 2017, at 01:29, David Sweeris via swift-evolution 
> <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
> 
>> 
>> On Jan 11, 2017, at 01:11, Freak Show <freaksho...@mac.com 
>> <mailto:freaksho...@mac.com>> wrote:
>> 
>>> 
>>>> On Jan 7, 2017, at 23:37, Derrick Ho <wh1pch...@gmail.com 
>>>> <mailto:wh1pch...@gmail.com>> wrote:
>>>> 
>>>> I think pattern matching is the most compelling reason to keep tuples.  
>>>> 
>>>> If they were gone, how would we replace the following?
>>>> 
>>>> switch (a, b) {
>>>> case (value1, value2):
>>>> case (value3, value4):
>>>> }
>>> 
>>> I meant to mention this:  Smalltalk - Objective C's mother language - has 
>>> no switch statement (or 'if' or loops either).  The language is incredibly 
>>> malleable because it only does one thing - send messages to objects and all 
>>> the language constructs are in the library.  It would take very little time 
>>> to add one.  Off and on someone does it as an exercise but it never sticks.
>>> 
>>> Instead, you just use a dictionary of closures.  An Objective C equivalent 
>>> might be:
>>> 
>>> NSDictionary* switch = @{
>>>     @[@0,@1]: ^{ NSLog(@"zero one"); },     
>>>     @[@1,@1]: ^{ NSLog(@"one one"); }
>>> };
>>> 
>>> NSArray* pair = @[@3, @5];
>>> 
>>> (switch at:pair ifAbsent:^{})();  //where at:ifAbsent: is added in the 
>>> Smalltalk style as an extension.
>>> 
>>> The Smalltalk equivalent (much less ugly because of the lack of @'s) is
>>> 
>>>  switch := {
>>>   #(0 1)   -> [ Transcript nextPutAll: 'zero one' ] .
>>>   #(1 1)   -> [ Transcript nextPutAll: 'one one' ] . 
>>>   #(1 2) -> [ Transcript nextPutAll: 'one two' ] .
>>> } asDictionary.
>>> 
>>> (switch at: pair ifAbsent:[ [] ]) value.
>>> 
>>> So its not like this is some kind of key feature.  Switch's vs dictionaries 
>>> of closures - pretty much the same thing as pattern matching goes.  The 
>>> only thing you have to do is put an object at key that identifies itself as 
>>> equal to the pattern you will throw at it.
>> 
>> I suspect that's a fair bit slower than a switch  statement. I'm not in 
>> front of my computer, so I can't prove it, though.
> 
> Also, Swift's dictionary requires its keys to conform to `Hashable`, which 
> would mean we'd have to create a formal struct *and* the "==" method for it 
> would have to act as a pattern matched instead of checking equality. I 
> suspect this could break a great many things that depend on "Equatable" 
> meaning what it says.
> 
> - Dave Sweeris (again)

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to