<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="">On 23 Apr 2017, at 15:55, Ionuț G. Stan via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Hi all,<br class=""><br class="">I'm unsure if this is the appropriate venue to discuss this, so apologies if it's not.<br class=""><br class="">I was wondering if the issue of supporting as-patterns in switch statements has been brought up before or not, and what was the outcome of that.<br class=""><br class="">By as-patterns I mean the ability to bind sub-patterns to identifiers. See this question of mine on StackOverflow for an example: <a href="http://stackoverflow.com/questions/43566117/haskell-like-as-patterns-in-swift/43567041#43567041" class="">http://stackoverflow.com/questions/43566117/haskell-like-as-patterns-in-swift/43567041#43567041</a><br class=""><br class="">Maybe an equal sign could be used to denote that, so my example above would become:<br class=""><br class=""> &nbsp;&nbsp;&nbsp;switch self {<br class=""> &nbsp;&nbsp;&nbsp;case .Int: return "int"<br class=""> &nbsp;&nbsp;&nbsp;case .Fun(let p = .Fun, let r): return "(\(p)) -&gt; \(r)"<br class=""> &nbsp;&nbsp;&nbsp;case .Fun(let p, let r): return "(\(p) -&gt; \(r))"<br class=""> &nbsp;&nbsp;&nbsp;}<br class=""><br class="">Note the `let p = .Fun` part.<br class=""><br class="">Alternatively, enhancing the `where` clause of a `case` to support the same features as an `if case` expression, would be an acceptable choice, too?<br class=""><br class=""> &nbsp;&nbsp;&nbsp;case .Fun(let p, let r) where case .Fun = p:<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "(\(p)) -&gt; \(r)"<br class=""><br class="">Thanks for reading.<br class=""><br class="">-- <br class="">Ionuț G. Stan &nbsp;| &nbsp;<a href="http://igstan.ro" class="">http://igstan.ro</a> &nbsp;| &nbsp;<a href="http://bucharestfp.ro" class="">http://bucharestfp.ro</a><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><div class=""><br class=""></div><div class="">I think the second (using where clauses to provide a more specific constraint) is probably more consistent with the rest of the language.</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""></blockquote><font face="Courier" class="">&nbsp; &nbsp;switch self {<br class=""></font><blockquote type="cite" class=""></blockquote><font face="Courier" class="">&nbsp; &nbsp;case .Int:</font></div><div class=""><font face="Courier" class="">&nbsp; &nbsp; &nbsp;...</font></div><div class=""><div class=""><font face="Courier" class="">&nbsp; &nbsp;case .Fun(let outer_p, let r) where case .Fun(let inner_p,_) = outer_p, case .Int = inner_p:&nbsp;</font></div><div class=""><font face="Courier" class="">&nbsp; &nbsp; &nbsp;...</font></div><blockquote type="cite" class=""></blockquote><font face="Courier" class="">&nbsp; &nbsp;case .Fun(let p, let r) where case .Fun(_,_) = p:&nbsp;</font></div><div class=""><font face="Courier" class="">&nbsp; &nbsp; &nbsp;...<br class=""></font><blockquote type="cite" class=""></blockquote><font face="Courier" class="">&nbsp; &nbsp;case .Fun(let p, let r):</font></div><div class=""><font face="Courier" class="">&nbsp; &nbsp; &nbsp;...<br class="">&nbsp;&nbsp;&nbsp;}</font><br class=""></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class="">Looks pretty good IMO, although I do wonder if we couldn’t drop the&nbsp;second&nbsp;“case”.</div><div class=""><br class=""></div><div class="">- Karl</div><div class=""><br class=""></div></body></html>