<div dir="ltr">Just to clarify, Dave -<div><br></div><div>What happens there if one case has associated values and one has an associated value thats an optional? </div><div><br></div><div>Enum A {</div><div> case x(String?)</div><div> case y</div><div>}</div><div><br></div><div>let a = A.x(nil)</div><div><br></div><div>a.y // What's the result?</div><div>a.x // Would produce a double optional, which are clumsy to nil check</div><div><br></div><div>I'm not a fan of solving this via synthesis in general. We have metatypes for classes/structs/protocols, which are useful in all sorts of situations. Cases are essentially "types" of enums. Why not have case metatypes? They're useful for the same reasons class types are, and there's already precedence in the language for syntax.</div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Dec 21, 2017 at 7:14 AM Dave Abrahams <<a href="mailto:dabrahams@apple.com">dabrahams@apple.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><span style="background-color:rgba(255,255,255,0)">IIRC what we discussed was synthesizing members of type Optional<Payload> which could then be checked against nil. </span><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">if _ = x.failure { ... }</span></div><div><span style="background-color:rgba(255,255,255,0)">if x.failure != nil { ... }</span></div><div><span style="background-color:rgba(255,255,255,0)">if let r = x.success {...}</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">IMO synthesizing predicates would be a huge missed opportunity by comparison</span><br></div><br><div id="m_-7614202654132317787AppleMailSignature">Sent from my iPhone</div></div><div dir="auto"><div><br>On Dec 20, 2017, at 1:31 PM, Chris Lattner via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div>In the past, we’ve discussed synthesizing predicate members onto enums. E.g. given:<div><br></div><div>enum E {</div><div> case X</div><div> case Y(Int)</div><div>}</div><div><br></div><div>you’d get something like:</div><div><br></div><div>extension E {</div><div> func isX() -> Bool { return self == .X }</div><div> func getY() -> Int? { … }</div><div>}</div><div><br></div><div>which would solve the client side of this nicely.</div><div><br></div><div>-Chris</div><div><br></div><div><br></div><div><br><div><blockquote type="cite"><div>On Dec 20, 2017, at 11:24 AM, Ethan Diamond via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br class="m_-7614202654132317787Apple-interchange-newline"><div><div dir="ltr">Sorry all for attaching the original post to the Non-Exhaustive enums thread. I"m moving it down to it's own thread. <div><div><br></div><div>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><div>
<br class="m_-7614202654132317787webkit-block-placeholder"></div><p class="m_-7614202654132317787inbox-inbox-p1">if<span class="m_-7614202654132317787inbox-inbox-s1"> </span>case<span class="m_-7614202654132317787inbox-inbox-s1"> .</span><span class="m_-7614202654132317787inbox-inbox-s2">search</span><span class="m_-7614202654132317787inbox-inbox-s1"> = presenter.</span><span class="m_-7614202654132317787inbox-inbox-s2">state</span><span class="m_-7614202654132317787inbox-inbox-s1"> { </span>return<span class="m_-7614202654132317787inbox-inbox-s1"> </span>true<span class="m_-7614202654132317787inbox-inbox-s1"> } </span>else<span class="m_-7614202654132317787inbox-inbox-s1"> { </span>return<span class="m_-7614202654132317787inbox-inbox-s1"> </span>false<span class="m_-7614202654132317787inbox-inbox-s1"> }</span></p></div><div>Side note: Thanks Kevin, didn't know you could nest enums in switches like that. Super helpful!<br></div><div><br></div><div>------------------------------------------------------</div><div><pre style="white-space:pre-wrap">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> 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">swift-evolution at swift.org</a>> wrote:
</i>><i>
</i>><i> 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>
</i>><i> Sidenote, your `switch` example is actually trivial with existing syntax:
</i>><i>
</i>><i> switch enumeration {
</i>><i> case .a(.c(let param)): // or just .a(.c) if you don't need the value
</i>><i> print(param)
</i>><i> default:
</i>><i> break
</i>><i> }
</i>><i>
</i>><i> I use this from time to time switching over, e.g., optional enums.
</i>><i>
</i>><i> *: ugliest syntax ever, and it can't even be used as a standalone expression.
</i>><i>
</i>><i>
</i>>><i> 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">swift-evolution at swift.org</a> <mailto:<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">swift-evolution at swift.org</a>>> wrote:
</i>>><i>
</i>>><i> Hello everyone,
</i>>><i>
</i>>><i> 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>
</i>>><i> enum Enum {
</i>>><i> case a(param: String)
</i>>><i> case b(param: String)
</i>>><i> }
</i>>><i>
</i>>><i> let enumeration: Enum = a(param: "Hi")
</i>>><i> switch enumeration {
</i>>><i> case a:
</i>>><i> // Do something
</i>>><i> case b:
</i>>><i> // Do something
</i>>><i> }
</i>>><i>
</i>>><i> We'e been given the ability in the context of an if statement:
</i>>><i>
</i>>><i> enum Enum {
</i>>><i> case a(param: String)
</i>>><i> case b(param: String)
</i>>><i> }
</i>>><i>
</i>>><i> let enumeration: Enum = a(param: "Hi")
</i>>><i>
</i>>><i> if case .a = enumeration {
</i>>><i> // Do something
</i>>><i> }
</i>>><i>
</i>>><i> 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>
</i>>><i> Where statements:
</i>>><i>
</i>>><i> enum Enum {
</i>>><i> case a(param: Enum2)
</i>>><i> case b(param: Enum2)
</i>>><i> }
</i>>><i>
</i>>><i> enum Enum2 {
</i>>><i> case c(param: String)
</i>>><i> case d(param: String)
</i>>><i> }
</i>>><i>
</i>>><i> let enumeration: Enum = a(param: "Hi")
</i>>><i> switch enumeration {
</i>>><i> case a(let inner) where [INNER CASE IS .c]
</i>>><i> }
</i>>><i>
</i>>><i> ---------
</i>>><i>
</i>>><i> Filter an array for a certain case:
</i>>><i>
</i>>><i> Expertly explained by Erica Sadun here: <a href="http://ericasadun.com/2017/01/31/challenge-filtering-associated-value-enumeration-arrays/" target="_blank">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">http://ericasadun.com/2017/01/31/challenge-filtering-associated-value-enumeration-arrays/</a>>
</i>>><i>
</i>>><i> ---------
</i>>><i>
</i>>><i> Nicely set a UIButton to hidden if an enum is a certain case:
</i>>><i>
</i>>><i> enum State {
</i>>><i> case `default`
</i>>><i> case searching(results: [Result])
</i>>><i> }
</i>>><i>
</i>>><i> myButton.isHidden = [STATE IS .searching]
</i>>><i>
</i>>><i> ---------
</i>>><i>
</i>>><i> 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>
</i>>><i> enum Enum {
</i>>><i> case a(param: String)
</i>>><i> case b(param: String)
</i>>><i> }
</i>>><i>
</i>>><i> let enumeration: Enum = a(param: "Hi")
</i>>><i>
</i>>><i> case .a = enumeration // Bool
</i>>><i> case .a(let param) = enumeration // Bool, assigns "Hi" to "param"
</i>>><i>
</i>>><i> Thanks!
</i>>><i> Ethan
</i>>><i>
</i>>><i> _______________________________________________
</i>>><i> swift-evolution mailing list
</i>>><i> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">swift-evolution at swift.org</a> <mailto:<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">swift-evolution at swift.org</a>>
</i>>><i> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a>
</i>><i>
</i>><i> _______________________________________________
</i>><i> swift-evolution mailing list
</i>><i> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">swift-evolution at swift.org</a>
</i>><i> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></i></pre></div></div></div>
_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></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" target="_blank">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></blockquote></div>