<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="">re - squash for pull request.<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 2016-01-20, at 8:58:22, Craig Cruden <<a href="mailto:ccruden@novafore.com" class="">ccruden@novafore.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="">Updated the introduction<div class=""><br class=""></div><div class=""><a href="https://github.com/cacruden/swift-evolution/blob/master/proposals/0023-Pattern-Matching-Partial-Function.md" class="">https://github.com/cacruden/swift-evolution/blob/master/proposals/0023-Pattern-Matching-Partial-Function.md</a></div><div class=""><br class=""></div><div class="">You also solved an outstanding issue in my own mind, that of how to implement `collect` like function while defining exhaustive cases. Where filter would filter existing types, collect would be able to do a similar function… by expecting the closure to return an Optional.</div><div class=""><br class=""></div><div class="">Something like:</div><div class=""><br class=""></div><div class="">let arr : [Any] = [5, ‘cats’, 3, ‘goldfish’]</div><div class=""><br class=""></div><div class="">let arrStr : [String] = arr.collect { </div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case let s as String: .Some(s)</div><div class=""> default: .None</div><div class="">}</div><div class=""><br class=""></div><div class="">resulting in [‘cats’, ‘goldfish’]</div><div class=""><br class=""></div><div class=""><div class=""><blockquote type="cite" class=""><div class="">On 2016-01-20, at 6:10:59, 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="">Craig,<div class=""><br class=""></div><div class="">thanks! I’m glad you think likewise.</div><div class=""><br class=""></div><div class="">Some first thoughts: what about dropping the second sentence (the one about map) and slightly modifying the third one (dropping the restriction to basic types)? Furthermore I swapped and slightly modified the last two sentences for clarity (I hope):</div><div class=""><br class=""></div><div class="">"Provide the ability for defining partial functions using familiar case/default pattern matching syntax. A new global match function would use this to provide switch-expression functionality for all types. This function would accept a value and a closure containing the case/default partial functions. Combining case/default partial functions in a closure must always be exhaustive providing a total function. Functions on arrays, dictionaries or other collections such as reduce or filter, or rather all higher order functions taking a unary function as argument, would accept these closures as they are just normal closures.“</div><div class=""><br class=""></div><div class="">Actually I’m not sure about introducing the term "partial closure“ because the proposal does not make use of their partiality in any way (like Scala does with the method isDefinedAt() or the chaining methods) and on the contrary requires their combination to be a total function (which is a good thing IMO). As I already wrote in another mail a couple of days before I think in Swift partial functions can simply be formulated by making the return type an optional which I think fits Swift nicely because of its lightweight syntax of optionals.</div><div class="">So I don’t think there is a need for partial functions in Swift and therefore I probably would not introduce that term but instead propose a new lightweight syntax for defining unary closures where</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">{ x in </font></div><div class=""><font face="Menlo" class=""> switch x {</font></div><div class=""><font face="Menlo" class=""> case pattern: return expr</font></div><div class=""><font face="Menlo" class=""> default: return expr</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class="">can simply be written as</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">{ </font></div><div class=""><div class=""><font face="Menlo" class=""> case pattern: expr</font></div><div class=""><font face="Menlo" class=""> default: expr</font></div><div class=""><font face="Menlo" class="">}</font></div></div><div class=""><br class=""></div><div class="">Does that make sense?</div><div class=""><br class=""></div><div class="">-Thorsten</div><div class=""><br class=""></div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">Am 19.01.2016 um 19:55 schrieb Craig Cruden <<a href="mailto:ccruden@novafore.com" class="">ccruden@novafore.com</a>>:</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="">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 class=""><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></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></body></html>