<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">Am 19.01.2016 um 20:50 schrieb Paul Ossenbruggen &lt;<a href="mailto:possen@gmail.com" class="">possen@gmail.com</a>&gt;:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class="">I am not following why it is not good to add map to basic types. Aren't we mapping values?</div></div></div></blockquote><div><br class=""></div>We do, but for the pattern matching case we want to map all types not just basic types and in case of collections we would want to match the whole collection, e.g. against a range.</div><div><br class=""></div><div>match(collection) {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>case [1,2,3]: ...</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>default: …</div><div>}</div><div><br class=""></div><div>is something else than</div><div><br class=""></div><div>collection.map {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>case 1: …</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>default: …</div><div>}</div><div><br class=""></div><div>where we match each element and return a collection.</div><div><br class=""></div><div>I’d rather make the matching as „switch-expression“ stand out a little bit.</div><div><br class=""></div><div>-Thorsten</div><div><br class=""></div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class=""><br class=""></div><div class="">Also, Craig I was thinking I might have a few tweaks do you mind if I do a pull request or would you prefer email feedback?</div><div class=""><br class=""></div><div class=""><br class=""><br class="">Sent from my iPhone</div><div class=""><br class="">On Jan 19, 2016, at 10:55 AM, Craig Cruden via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class="">Thorsten,&nbsp;<div class=""><br class=""></div><div class="">I made the changes you suggested with regards to match</div><div class=""><br class=""></div><div class="">Can you double check to make sure I did not make any stupid mistakes?</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div class=""><blockquote type="cite" class=""><div class="">On 2016-01-20, at 1:03:16, Thorsten Seitz &lt;<a href="mailto:tseitz42@icloud.com" class="">tseitz42@icloud.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">Am 19.01.2016 um 06:28 schrieb Chris Lattner via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt;:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">If you extend the same analogy to switch, then the most important cases are when the pattern being matched and the values being processed are lexically small, and have few cases. &nbsp;We have a lot of syntactic sugar for processing optionals (e.g. if/let, the ?? operator, etc), but ?? for example doesn’t apply to general pattern matching. &nbsp;With the expression above, for example, you could match on an enum analogously to the ?? operator like this:</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><font face="Menlo" class="">result = someEnum ? case .SomeCase(let x): x, default: y</font></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">If you compare that to a switch statement, I can see how that could be compelling. &nbsp;OTOH, the larger the expression (the more cases) and the more complex the patterns, the better a switch statement starts to look.</div></div></blockquote></div><br class=""><div class="">For me the killer feature is not being able to write it inline, it is making it clear that an assignment happens, i.e.</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">let result = someEnum ?</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; case .SomeCase(let x): x</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; default: y</font></div><div class=""><br class=""></div><div class="">is IMO much more readable (= allowing to instantly see what’s going on) than</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">let result</font></div><div class=""><font face="Menlo" class="">switch someEnum {</font></div><div class=""><font face="Menlo" class="">case .SomeCase(let x): result = x</font></div><div class=""><font face="Menlo" class="">default: result = y</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class="">The first one makes the variable which is assigned stand out because the rest is indented and the equals sign is prominently visible.</div><div class="">The latter version is just a large heap of tokens hiding some assignments :-)</div><div class=""><br class=""></div><div class="">For the same reason</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">let result = match(someEnum) {</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; case .SomeCase(let x): x</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; default: y</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class="">would be just as appealing to me. For using this as a switch-expression I’m strongly against introducing a special map method for basic types, though and rather propose to use a global function „match“ (more on that further below).</div><div class=""><br class=""></div><div class=""><div class="">The inline version (if desired) is not too different from the&nbsp;?-based switch-expression:</div><div class=""><br class=""></div><div class="">let result = match(someEnum) { case .SomeCase(let x): x; default: y }</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">The big advantage of the partial function proposal would be that it is much more general as it is usable everywhere a unary function argument is used, i.e. for map etc.</div><div class="">With the ?-based switch-expression I would have to write</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> result: [<span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Int</span>] = collection.map { element <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">in</span> element ?</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">case</span> .SomeCase(<span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> x): x</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">default</span>: y</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">}</div></div><div class=""><br class=""></div><div class="">That is not too bad but requires the boilerplate of having to introduce a name which is instantly consumed and not used anywhere else.</div><div class="">Admittedly there might be cases (no pun intended) where this might be useful, though, like</div><div class=""><br class=""></div><div class=""><div class=""><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> result: [<span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">SomeEnum</span>] = collection.map { element <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">in</span> element ?</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">case</span> .SomeCase(<span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> x) <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">where</span> x &gt; <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">5</span>: element</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">default</span>: .OtherCase</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">}</div></div></div><div class=""><br class=""></div><div class="">Hmmm. Would this use case be common enough to make this version using a ?-based switch-expression be preferable over the partial function syntax?</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">As I already said above I’m strongly against introducing a map function for basic types. This brings confusion to the notion of a partial function and to what a map function is and is not needed IMO.</div><div class=""><br class=""></div><div class="">Just define the following global function:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">func</span> match&lt;T,U&gt;(x: <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">T</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">@noescape</span> mapping: <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">T</span> -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">U</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">U</span> {</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">return</span> mapping(x)</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">}</div></div><div class=""><br class=""></div><div class="">That’s all that is needed. Except for the case-expression shorthand, of course :-)</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Without that proposed shorthand („partial function“) I currently have to write</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> result = <span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">match</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">someEnum</span>) { (arg: <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Foo</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Int</span> <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">in</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">switch</span> arg {</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">case</span> .SomeCase(<span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> x): <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">return</span> x</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(133, 153, 1);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #839496" class="">&nbsp; &nbsp; </span>default<span style="font-variant-ligatures: no-common-ligatures; color: #839496" class="">: </span>return<span style="font-variant-ligatures: no-common-ligatures; color: #839496" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">42</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">}</div></div><div class=""><br class=""></div><div class="">instead of the equivalent</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> result = <span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">match</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">someEnum</span>) {</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">case</span> .SomeCase(<span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> x): <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">return</span> x</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(133, 153, 1);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #839496" class="">&nbsp; &nbsp; </span>default<span style="font-variant-ligatures: no-common-ligatures; color: #839496" class="">: </span>return<span style="font-variant-ligatures: no-common-ligatures; color: #839496" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">42</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">}</div></div><div class=""><br class=""></div><div class="">as proposed (with the added change of using match instead of map).</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">-Thorsten</div><div class=""><br class=""></div><div class=""><br class=""></div></div></div></blockquote></div><br class=""></div></div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></div></div></blockquote></div><br class=""></body></html>