[swift-evolution] [Pitch] Eliminate tuples - unify member access syntax

André Videla andre.videla at gmail.com
Wed Jan 11 02:04:18 CST 2017


> NSDictionary* switch = @{
> 	@[@0, at 1]: ^{ NSLog(@"zero one"); },	
> 	@[@1, at 1]: ^{ NSLog(@"one one"); }
> };


What you're describing here is only a narrow use of switch statements and doesn't handle the most powerful features of the switch statement: pattern matching. 

How would you go about 
Switch (optional, superclass, genericStruct) {
    Case (let value?, let sub as SubClass, Generic<ConcreteType>: // use all this stuff
    case _: // nothing matches
}

You don't seem familiar with switches and tuple deconstruction so here is a common everyday use case: prepare segues between view controllers:

func prepareForSegue(segue: UIStoryboardSegue, sender: Any?) {
    Switch (segue.identifier, segue.destination, sender) {
        Case ("Segue"?, let vc as FirstVC, .some(.enumType(let value)) ): // prepare the vc
        ... // other segues
    }
}

> So its not like this is some kind of key feature. 
Dictionaries require their keys to be hashable. That's a problem with exhausitveness checking. A hashable Type has infinite cardinality and as such, can't be exhaustive. Therefore the compiler will never be able to check the completeness of your switch statement, throwing away one the languages key feature: safety. 

I think we should steer this conversation back to tuple labels. 

Andre Videla 

> On 11 Jan 2017, at 08:11, Freak Show via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On Jan 7, 2017, at 23:37, Derrick Ho <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.
> 
> 
> 
> _______________________________________________
> 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/20170111/904acbf1/attachment.html>


More information about the swift-evolution mailing list