[swift-evolution] [Pitch] Eliminate tuples - unify member access syntax
Freak Show
freakshow42 at mac.com
Wed Jan 11 01:56:22 CST 2017
Pretty sure Array conforms to hashable and ==.
> On Jan 10, 2017, at 23:43, David Sweeris <davesweeris at mac.com> wrote:
>
>
> On Jan 11, 2017, at 01:29, David Sweeris via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>
>>
>> On Jan 11, 2017, at 01:11, Freak Show <freakshow42 at mac.com <mailto:freakshow42 at mac.com>> wrote:
>>
>>>
>>>> On Jan 7, 2017, at 23:37, Derrick Ho <wh1pch81n at gmail.com <mailto:wh1pch81n at 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, at 1]: ^{ NSLog(@"zero one"); },
>>> @[@1, at 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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170110/c2c41b3c/attachment.html>
More information about the swift-evolution
mailing list