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

Freak Show freakshow42 at mac.com
Wed Jan 11 01:11:56 CST 2017


> 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.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170110/285033ef/attachment.html>


More information about the swift-evolution mailing list