<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">This would be easily solved if pattern matching was available as an expression, such as in Haskell, OCaml / Standard ML, and Scala / Kotlin. :-)</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Dec 20, 2017, at 11:44 AM, Ethan Diamond via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hello everyone,<div class=""><br class=""></div><div class="">One major pain point I've run into with Swift is the inability to evaluate the case of an enum that has associated values in a way that just returns a bool. We've been given the ability in a switch statement:</div><div class=""><br class=""></div><div class="">enum Enum {</div><div class=""> case a(param: String)</div><div class=""> case b(param: String)</div><div class="">}</div><div class=""><br class=""></div><div class="">let enumeration: Enum = a(param: "Hi")</div><div class="">switch enumeration {</div><div class=""> case a:</div><div class=""> // Do something</div><div class=""> case b:</div><div class=""> // Do something</div><div class="">}</div><div class=""><br class=""></div><div class="">We'e been given the ability in the context of an if statement:</div><div class=""><br class=""></div><div class=""><div class="">enum Enum {</div><div class=""> case a(param: String)</div><div class=""> case b(param: String)</div><div class="">}</div></div><div class=""><br class=""></div><div class="">let enumeration: Enum = a(param: "Hi")<br class=""></div><div class=""><br class=""></div><div class="">if case .a = enumeration { </div><div class=""> // Do something</div><div class="">}</div><div class=""><br class=""></div><div class="">But without a basic was of getting a bool for if an enum is a given case, here's a list of things I can't do:</div><div class=""><br class=""></div><div class=""><b class="">Where statements:</b></div><div class=""><br class=""></div><div class=""><div class="">enum Enum {</div><div class=""> case a(param: Enum2)</div><div class=""> case b(param: Enum2)</div><div class="">}</div></div><div class=""><br class=""></div><div class="">enum Enum2 {</div><div class=""> case c(param: String)</div><div class=""> case d(param: String)<br class=""></div><div class="">}</div><div class=""><br class=""></div><div class="">let enumeration: Enum = a(param: "Hi")</div><div class="">switch enumeration {</div><div class=""> case a(let inner) where [INNER CASE IS .c]</div><div class="">}</div><div class=""><br class=""></div><div class="">---------<br class=""></div><div class=""><br class=""></div><div class=""><b class="">Filter an array for a certain case:</b></div><div class=""><br class=""></div><div class="">Expertly explained by Erica Sadun here: <a href="http://ericasadun.com/2017/01/31/challenge-filtering-associated-value-enumeration-arrays/" class="">http://ericasadun.com/2017/01/31/challenge-filtering-associated-value-enumeration-arrays/</a></div><div class=""><br class=""></div><div class="">---------<br class=""></div><div class=""><br class=""></div><div class=""><b class="">Nicely set a UIButton to hidden if an enum is a certain case:</b></div><div class=""><b class=""><br class=""></b></div><div class="">enum State {</div><div class=""> case `default`</div><div class=""> case searching(results: [Result])</div><div class="">}</div><div class=""><br class=""></div><div class="">myButton.isHidden = [STATE IS .searching]</div><div class=""><br class=""></div><div class="">---------</div><div class=""><br class=""></div><div class="">I've run into this issue a ton of times because I tend to represent my views a State enums. I haven't seen anything on the board for plans for solving this issue, thought. Has there been any discussion about addressing it? Ideally I'd be able to do this:</div><div class=""><br class=""></div><div class=""><div class="">enum Enum {</div><div class=""> case a(param: String)</div><div class=""> case b(param: String)</div><div class="">}</div></div><div class=""><br class=""></div><div class="">let enumeration: Enum = a(param: "Hi")<br class=""></div><div class=""><br class=""></div><div class="">case .a = enumeration // Bool</div><div class="">case .a(let param) = enumeration // Bool, assigns "Hi" to "param"</div><div class=""><br class=""></div><div class="">Thanks!</div><div class="">Ethan</div><div class=""><br class=""></div></div>
_______________________________________________<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></blockquote></div><br class=""></body></html>