<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 Dec 20, 2017, at 2:12 PM, Ethan Diamond <<a href="mailto:ethanjdiamond@gmail.com" class="">ethanjdiamond@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Would that synthesize an isY() even though .Y has an associated value there?</div></div></blockquote><div><br class=""></div>I’m not aware of a concrete design for this idea. The details would definitely need to be figured out, but I don’t see why a double optional is itself a problem.</div><div><br class=""></div><div>-Chris</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""><div class="">enum E {</div><div class=""> case X</div><div class=""> case Y(Int?)</div><div class="">}</div><div class=""><br class=""></div><div class="">If I had to run that through getY() -> Int??, it still wouldn't be quite what I was looking for with regards to intent. If you are planning an doing an isY though, that would work for most cases where you're evaluating for a given enum and know what it is beforehand. Even so that wouldn't work for a case, for example, where I'm trying to see if two enums are the same case, and don't necessarily care if they're equal.</div></div><div class=""><br class=""></div><div class="">let value1 = E.Y(1)</div><div class="">let value2 = E.Y(2)<br class=""></div><div class=""><br class=""></div><div class="">value1 == value2 // false</div><div class="">value1 [is the same case as] value 2 // how do I get this?</div><div class=""><br class=""></div><div class="">This would be useful, say, if I was trying to generate a diff of two arrays of enums, which I occasionally do for table / collection views to figure out inserts/removals/updates.</div><div class=""><br class=""></div><div class="">I don't necessarily know if it's feasible, but it would be really great to have something like a Case metatype, the same way we have type(of: ). It would be great to have a case(of: ) that we can evaluate against the shorthand like we do in switch statements. </div><div class=""><br class=""></div><div class="">Ex: </div><div class=""><br class=""></div><div class="">case(of: value1) == .Y // true</div><div class="">case(of: value1) == .X // false</div><div class="">case(of: value1) == case(of: value2) // true</div><div class=""><br class=""></div><div class=""><br class=""></div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Wed, Dec 20, 2017 at 1:31 PM Chris Lattner <<a href="mailto:clattner@nondot.org" class="">clattner@nondot.org</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">In the past, we’ve discussed synthesizing predicate members onto enums. E.g. given:<div class=""><br class=""></div><div class="">enum E {</div><div class=""> case X</div><div class=""> case Y(Int)</div><div class="">}</div><div class=""><br class=""></div><div class="">you’d get something like:</div><div class=""><br class=""></div><div class="">extension E {</div><div class=""> func isX() -> Bool { return self == .X }</div><div class=""> func getY() -> Int? { … }</div><div class="">}</div><div class=""><br class=""></div><div class="">which would solve the client side of this nicely.</div><div class=""><br class=""></div><div class="">-Chris</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""><div class=""><blockquote type="cite" class=""></blockquote></div></div></div><div style="word-wrap:break-word" class=""><div class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 20, 2017, at 11:24 AM, Ethan Diamond via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:</div><br class="m_7271590183074468783Apple-interchange-newline"></blockquote></div></div></div><div style="word-wrap:break-word" class=""><div class=""><div class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class="">Sorry all for attaching the original post to the Non-Exhaustive enums thread. I"m moving it down to it's own thread. <div class=""><div class=""><br class=""></div><div class="">My understanding is I'm not allowed to write up a proposal unless I have the time to implement it. Is that still true? This is a major pain point for me to avoid having to write things like this:</div><div class=""><div class="">
<br class="m_7271590183074468783webkit-block-placeholder"></div><p class="m_7271590183074468783inbox-inbox-p1">if<span class="m_7271590183074468783inbox-inbox-s1"> </span>case<span class="m_7271590183074468783inbox-inbox-s1"> .</span><span class="m_7271590183074468783inbox-inbox-s2">search</span><span class="m_7271590183074468783inbox-inbox-s1"> = presenter.</span><span class="m_7271590183074468783inbox-inbox-s2">state</span><span class="m_7271590183074468783inbox-inbox-s1"> { </span>return<span class="m_7271590183074468783inbox-inbox-s1"> </span>true<span class="m_7271590183074468783inbox-inbox-s1"> } </span>else<span class="m_7271590183074468783inbox-inbox-s1"> { </span>return<span class="m_7271590183074468783inbox-inbox-s1"> </span>false<span class="m_7271590183074468783inbox-inbox-s1"> }</span></p></div><div class="">Side note: Thanks Kevin, didn't know you could nest enums in switches like that. Super helpful!<br class=""></div><div class=""><br class=""></div><div class="">------------------------------------------------------</div><div class=""><pre style="white-space:pre-wrap" class="">I thought I would add another case that isn’t possible with current syntax (so far as I’m aware). You can’t negate the comparison to do something for all cases except a particular case. You have to have an empty if block and use the else block, or have an empty case in a switch statement and use the default.
enum Enum {
case a(param: String)
case b(param: String)
case c(param: String)
}
let enumeration: Enum = .a(param: "Hi")
if !(case .a = enumeration) {
// Do something
}
— Charles
><i class=""> On Dec 20, 2017, at 9:55 AM, Kevin Nattinger via swift-evolution <<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">swift-evolution at swift.org</a>> wrote:
</i>><i class="">
</i>><i class=""> I agree this would be useful. At the moment I have to hack around it with things like `var isFoo: Bool { if case .foo = self …`* with cases I commonly need, but this is definitely a feature that has come up before and I support. It is potentially related to getting the values through an accessor, which has also come up several times.
</i>><i class="">
</i>><i class=""> Sidenote, your `switch` example is actually trivial with existing syntax:
</i>><i class="">
</i>><i class=""> switch enumeration {
</i>><i class=""> case .a(.c(let param)): // or just .a(.c) if you don't need the value
</i>><i class=""> print(param)
</i>><i class=""> default:
</i>><i class=""> break
</i>><i class=""> }
</i>><i class="">
</i>><i class=""> I use this from time to time switching over, e.g., optional enums.
</i>><i class="">
</i>><i class=""> *: ugliest syntax ever, and it can't even be used as a standalone expression.
</i>><i class="">
</i>><i class="">
</i>>><i class=""> On Dec 20, 2017, at 8:44 AM, Ethan Diamond via swift-evolution <<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">swift-evolution at swift.org</a> <mailto:<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">swift-evolution at swift.org</a>>> wrote:
</i>>><i class="">
</i>>><i class=""> Hello everyone,
</i>>><i class="">
</i>>><i 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:
</i>>><i class="">
</i>>><i class=""> enum Enum {
</i>>><i class=""> case a(param: String)
</i>>><i class=""> case b(param: String)
</i>>><i class=""> }
</i>>><i class="">
</i>>><i class=""> let enumeration: Enum = a(param: "Hi")
</i>>><i class=""> switch enumeration {
</i>>><i class=""> case a:
</i>>><i class=""> // Do something
</i>>><i class=""> case b:
</i>>><i class=""> // Do something
</i>>><i class=""> }
</i>>><i class="">
</i>>><i class=""> We'e been given the ability in the context of an if statement:
</i>>><i class="">
</i>>><i class=""> enum Enum {
</i>>><i class=""> case a(param: String)
</i>>><i class=""> case b(param: String)
</i>>><i class=""> }
</i>>><i class="">
</i>>><i class=""> let enumeration: Enum = a(param: "Hi")
</i>>><i class="">
</i>>><i class=""> if case .a = enumeration {
</i>>><i class=""> // Do something
</i>>><i class=""> }
</i>>><i class="">
</i>>><i 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:
</i>>><i class="">
</i>>><i class=""> Where statements:
</i>>><i class="">
</i>>><i class=""> enum Enum {
</i>>><i class=""> case a(param: Enum2)
</i>>><i class=""> case b(param: Enum2)
</i>>><i class=""> }
</i>>><i class="">
</i>>><i class=""> enum Enum2 {
</i>>><i class=""> case c(param: String)
</i>>><i class=""> case d(param: String)
</i>>><i class=""> }
</i>>><i class="">
</i>>><i class=""> let enumeration: Enum = a(param: "Hi")
</i>>><i class=""> switch enumeration {
</i>>><i class=""> case a(let inner) where [INNER CASE IS .c]
</i>>><i class=""> }
</i>>><i class="">
</i>>><i class=""> ---------
</i>>><i class="">
</i>>><i class=""> Filter an array for a certain case:
</i>>><i class="">
</i>>><i class=""> Expertly explained by Erica Sadun here: <a href="http://ericasadun.com/2017/01/31/challenge-filtering-associated-value-enumeration-arrays/" target="_blank" class="">http://ericasadun.com/2017/01/31/challenge-filtering-associated-value-enumeration-arrays/</a> <<a href="http://ericasadun.com/2017/01/31/challenge-filtering-associated-value-enumeration-arrays/" target="_blank" class="">http://ericasadun.com/2017/01/31/challenge-filtering-associated-value-enumeration-arrays/</a>>
</i>>><i class="">
</i>>><i class=""> ---------
</i>>><i class="">
</i>>><i class=""> Nicely set a UIButton to hidden if an enum is a certain case:
</i>>><i class="">
</i>>><i class=""> enum State {
</i>>><i class=""> case `default`
</i>>><i class=""> case searching(results: [Result])
</i>>><i class=""> }
</i>>><i class="">
</i>>><i class=""> myButton.isHidden = [STATE IS .searching]
</i>>><i class="">
</i>>><i class=""> ---------
</i>>><i class="">
</i>>><i 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:
</i>>><i class="">
</i>>><i class=""> enum Enum {
</i>>><i class=""> case a(param: String)
</i>>><i class=""> case b(param: String)
</i>>><i class=""> }
</i>>><i class="">
</i>>><i class=""> let enumeration: Enum = a(param: "Hi")
</i>>><i class="">
</i>>><i class=""> case .a = enumeration // Bool
</i>>><i class=""> case .a(let param) = enumeration // Bool, assigns "Hi" to "param"
</i>>><i class="">
</i>>><i class=""> Thanks!
</i>>><i class=""> Ethan
</i>>><i class="">
</i>>><i class=""> _______________________________________________
</i>>><i class=""> swift-evolution mailing list
</i>>><i class=""> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">swift-evolution at swift.org</a> <mailto:<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">swift-evolution at swift.org</a>>
</i>>><i class=""> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a>
</i>><i class="">
</i>><i class=""> _______________________________________________
</i>><i class=""> swift-evolution mailing list
</i>><i class=""> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">swift-evolution at swift.org</a>
</i>><i class=""> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></i></pre></div></div></div></div></blockquote></div></div></div><div style="word-wrap:break-word" class=""><div class=""><div class=""><blockquote type="cite" class=""><div class="">
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></blockquote></div>
</div></blockquote></div><br class=""></body></html>