<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>I am not following why it is not good to add map to basic types. Aren't we mapping values?</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">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 id="AppleMailSignature"><br></div><div id="AppleMailSignature"><br><br>Sent from my iPhone</div><div><br>On Jan 19, 2016, at 10:55 AM, Craig Cruden via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8">Thorsten, <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><blockquote type="cite" class=""><div class="">On 2016-01-20, at 1:03:16, Thorsten Seitz <<a href="mailto:tseitz42@icloud.com" class="">tseitz42@icloud.com</a>> 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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>>:</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. 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. 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. 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=""> case .SomeCase(let x): x</font></div><div class=""><font face="Menlo" class=""> 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=""> case .SomeCase(let x): x</font></div><div class=""><font face="Menlo" class=""> 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 ?-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=""> <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=""> <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=""> <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 > <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=""> <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<T,U>(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> -> <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">U</span>) -> <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=""> <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>) -> <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=""> <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=""> <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=""> </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 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=""> <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=""> </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"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>