<div dir="ltr"><div><div><div><div><div><div>I know I&#39;m a bit late to the party but I have just found this thread and decided to put my 2 cents, in the hope that it will resurrect the discussion and eventually something gets done.<br><br></div>In the thread (quoted below), everybody expressed their desire to be able to easily test enum cases of enums with (and without) associated values, one way or another. What I haven&#39;t seen among the suggestions is the liberation of pattern matching. <br></div><div><br></div><div>Currently we have 3 constructs that support pattern matching: if, guard, and switch. The problem with all of them is that they are control statements, so anytime we need a boolean expression (whether X matches a pattern or not) we need to wrap the statement and somehow get the boolean result out of it. This seems unnecessary; pattern matching could as well exist on its own as an expression that evaluates to a boolean, e.g.:</div><div><br></div><div>enum Result&lt;T&gt; {</div><div>    case success(T)</div><div>    case failure<br></div><div>}</div><div><br></div><div>let r = Result.success(42)</div><div>let rs: [Result&lt;Int&gt;] = [.success(2), .success(3), .failure, .success(5), .failure]<br></div><div><br></div><div>let simpleTest: Bool = (case .success = r)<br></div><div>let simpleFiltered: [Result&lt;Int&gt;] = rs.filter { case .success = $0 }<br></div></div><div><br></div><div>Now, we could take this even further and allow matching for subparts:</div><div><br></div><div>let subpartTest: Bool = (case .success(let v) = r, v &lt; 100) <br></div><div>// --&gt; true<br></div>let subpartFiltered: [Result&lt;Int&gt;] = rs.filter { case .success(let v) = $0, v % 2 == 1 }</div><div>// --&gt; [.success(3), .success(5)]<br><br></div>or perhaps with a more verbose syntax:<br><br><div>let subpartTest2: Bool = (case .success(let v) = r where v &lt; 100)</div>let subpartFiltered2: [Result&lt;Int&gt;] = rs.filter { case .success(let v) = $0 where v % 2 == 1 }<br><br></div>I&#39;m not familiar with the parsing challenges of the above suggestion but these expressions don&#39;t look wildly different from what we already see in currently supported pattern matching constructs.<br></div><div><br></div>So, any concerns about this? Am I overlooking something?<br><div><div><div><br></div></div><div><br></div><div>Tamas<br><div><br><div><div><div><br>&gt; Le 18 janv. 2017 à 05:51, Andy Chou via swift-evolution &lt;swift-evolution at <a href="http://swift.org" target="_blank">swift.org</a>&gt; a écrit :<br>&gt;<br>&gt; I see your point about conformances. In my example, AuthenticationResponse isn&#39;t a generic type, so the conformances spec won&#39;t apply.<br>&gt;<br>&gt; I&#39;ll go out on a limb and say that 80%+ of the use cases for equality will be to distinguish enum cases, not the associated values.<br>&gt;<br>&gt; I do like your proposal though. In the thread talking about it someone mentioned a &#39;derived&#39; keyword to specify conformance and derivation simultaneously. I like the idea of being able to say:<br>&gt;<br>&gt;     enum AuthenticationResponse: derived Equatable { ... }<br>&gt;<br>&gt; Still, even with derived conformance, it can still be very useful to be able to test against specific enum cases when the associated values aren&#39;t Equatable. Maybe this example would make it clearer:<br>&gt;<br>&gt; ```<br>&gt; enum OpaqueResponse {<br>&gt;     case success(OpaqueResult)<br>&gt;     case failure<br>&gt; }<br>&gt; ```<br>&gt;<br>&gt; If OpaqueResult is from a library module and its implementation uses private variables, it may not be easy to make it conform to Equatable. Yet, it would be nice to be able to say:<br>&gt;<br>&gt; ```<br>&gt; let result: OpaqueResponse = request()<br>&gt; if request == .failure  { ... }<br>&gt; ```<br>&gt;<br>&gt; A more realistic example comes from the world of Rx, where the original issue I have came from:<br>&gt;<br>&gt; ```<br>&gt; let result: Observable&lt;OpaqueResponse&gt; = request()<br>&gt;<br>&gt; result.filter { $0 == .failure } ...<br>&gt; result.filter { $0 == .success } ...<br>&gt;<br>&gt; // The best we can do today looks more like:<br>&gt; result.filter { if case .failure = $0 { return true} else { return false } }<br>&gt; result.filter { $0.isFailure }    // but we have to define isFailure ourselves<br>&gt;<br>&gt; // There&#39;s a similar issue with assertions<br>&gt; if case .failure = result { assert(false) }<br>&gt; assert({ if case .failure = result { return true } else { return false } }())<br>&gt; ```<br>&gt;<br>&gt; I agree it&#39;s less of an issue with test cases. The issue arises when we want a Bool valued expression...<br>&gt;<br>&gt; Andy<br>&gt;<br>&gt;&gt; On Jan 17, 2017, at 6:38 PM, Tony Allevato &lt;tony.allevato at <a href="http://gmail.com" target="_blank">gmail.com</a>&gt; wrote:<br>&gt;&gt;<br>&gt;&gt; Conditional conformances doesn&#39;t solve enum equality though, because there likely won&#39;t be a way to utter &quot;enum Foo : Equatable where &lt;all types across all associated value payloads are Equatable&gt;&quot; with the generics syntax that&#39;s available, and conformance alone wouldn&#39;t be able to derive the implementation. It&#39;ll require some work on the compiler&#39;s part to generate the right implementation—I mentioned a draft proposal I wrote a while back for auto-equality of enums and structs where all the components are equatable &lt;<a href="https://gist.github.com/allevato/2fd10290bfa84accfbe977d8ac07daad" target="_blank">https://gist.github.com/<wbr>allevato/<wbr>2fd10290bfa84accfbe977d8ac07da<wbr>ad</a>&gt; in another thread, but as Robert mentioned, the missing piece is how users opt in/out of it.<br>&gt;&gt;<br>&gt;&gt; If you just want to check the case of an enum in a test, what about this?<br>&gt;&gt;<br>&gt;&gt;     enum Foo {<br>&gt;&gt;       case bar<br>&gt;&gt;       case baz(Int)<br>&gt;&gt;     }<br>&gt;&gt;     let foo = Foo.baz(5)<br>&gt;&gt;     guard case .baz = foo else { XCTFail(&quot;expected baz&quot;); return }<br>&gt;&gt;<br>&gt;&gt; The &quot;return&quot; being required isn&#39;t ideal because XCTFail doesn&#39;t return Never, but other than that it&#39;s not *horrible*. You can do whatever pattern matching you need to use or ignore associated values as part of your comparison.<br>&gt;&gt;<br>&gt;&gt;<br>&gt;&gt;&gt; On Tue, Jan 17, 2017 at 5:59 PM Andy Chou via swift-evolution &lt;swift-evolution at <a href="http://swift.org" target="_blank">swift.org</a>&gt; wrote:<br>&gt;&gt;&gt; Yes, here&#39;s a reference to the conditional conformance proposal which was accepted:<br>&gt;&gt;&gt;<br>&gt;&gt;&gt; <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0143-conditional-conformances.md" target="_blank">https://github.com/apple/<wbr>swift-evolution/blob/master/<wbr>proposals/0143-conditional-<wbr>conformances.md</a><br>&gt;&gt;&gt;<br>&gt;&gt;&gt; But as I mention in my post, there are cases it doesn&#39;t handle. Specifically, when the associated types for an enum aren&#39;t equatable themselves, it&#39;s still possible to define equality on the enum cases without associated values.<br>&gt;&gt;&gt;<br>&gt;&gt;&gt; Andy<br>&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt; On Jan 17, 2017, at 5:45 PM, Robert Widmann &lt;devteam.codafi at <a href="http://gmail.com" target="_blank">gmail.com</a>&gt; wrote:<br>&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt; Automatic “equatability” of aggregates that contain equatable members has been discussed on this list quite a few times.  (I think I had a branch at one point that was exploring this kind of deriving mechanism… It seems to be lost to the sands of time).  Everybody seems to agree that it’s a worthwhile feature, but there needs to be thought put into how it is exposed to the end user.  e.g. Should we continue with silently implementing these protocols if we can, or should there be some kind of annotation to tell the compiler to only synthesize what the user wants?<br>&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; On Jan 17, 2017, at 7:15 PM, Andy Chou via swift-evolution &lt;swift-evolution at <a href="http://swift.org" target="_blank">swift.org</a>&gt; wrote:<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; Enums with associated values can be very useful in Swift, but once you add associated values you lose some properties, especially equality:<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; ```<br>&gt;&gt;&gt;&gt;&gt; enum AuthenticationResponse {<br>&gt;&gt;&gt;&gt;&gt;   case success<br>&gt;&gt;&gt;&gt;&gt;   case alert(Alert)<br>&gt;&gt;&gt;&gt;&gt;   case reauthenticate<br>&gt;&gt;&gt;&gt;&gt; }<br>&gt;&gt;&gt;&gt;&gt; ```<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; Testing for a specific case requires a switch statement or the if pattern match syntax:<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;     if case .success = response { … }<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; But while this works well for control flow, it doesn’t work well for cases where we want a Bool, such as assert(). There are also common situations with lists and libraries like RxSwift where a filtering function uses a Bool valued closure. In these situations the best we can do is write functions like:<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; ```<br>&gt;&gt;&gt;&gt;&gt; enum AuthenticationResponse {<br>&gt;&gt;&gt;&gt;&gt;   case success<br>&gt;&gt;&gt;&gt;&gt;   case alert(Alert)<br>&gt;&gt;&gt;&gt;&gt;   case reauthenticate<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;   var isSuccess: Bool {<br>&gt;&gt;&gt;&gt;&gt;       if case .success = self {<br>&gt;&gt;&gt;&gt;&gt;           return true<br>&gt;&gt;&gt;&gt;&gt;       } else {<br>&gt;&gt;&gt;&gt;&gt;           return false<br>&gt;&gt;&gt;&gt;&gt;       }<br>&gt;&gt;&gt;&gt;&gt;   }<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;   var isReauthenticate: Bool {<br>&gt;&gt;&gt;&gt;&gt;       if case .reauthenticate = self {<br>&gt;&gt;&gt;&gt;&gt;           return true<br>&gt;&gt;&gt;&gt;&gt;       } else {<br>&gt;&gt;&gt;&gt;&gt;           return false<br>&gt;&gt;&gt;&gt;&gt;       }<br>&gt;&gt;&gt;&gt;&gt;   }<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;   var isAlert: Bool {<br>&gt;&gt;&gt;&gt;&gt;       if case .alert(_) = self {<br>&gt;&gt;&gt;&gt;&gt;           return true<br>&gt;&gt;&gt;&gt;&gt;       } else {<br>&gt;&gt;&gt;&gt;&gt;           return false<br>&gt;&gt;&gt;&gt;&gt;       }<br>&gt;&gt;&gt;&gt;&gt;   }<br>&gt;&gt;&gt;&gt;&gt; }<br>&gt;&gt;&gt;&gt;&gt; ```<br>&gt;&gt;&gt;&gt;&gt; Any suggestions better than writing out each of these functions explicitly?<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; The conditional conformances proposal coming in Swift 4 solves some of this issue, but not completely. If Alert isn’t Equatable, it is still useful to test whether the result is .success.  For example:<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;     assert(response == .success)<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; This is perfectly intelligible and I would argue that equality should be defined for enums with associated values omitted:<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;     assert(response == .alert)<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; Here we are ignoring the associated values, and merely checking if the enum case is the same.<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; Andy<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; ______________________________<wbr>_________________<br>&gt;&gt;&gt;&gt;&gt; swift-evolution mailing list<br>&gt;&gt;&gt;&gt;&gt; swift-evolution at <a href="http://swift.org" target="_blank">swift.org</a><br>&gt;&gt;&gt;&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;<br>&gt;&gt;&gt; ______________________________<wbr>_________________<br>&gt;&gt;&gt; swift-evolution mailing list<br>&gt;&gt;&gt; swift-evolution at <a href="http://swift.org" target="_blank">swift.org</a><br>&gt;&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>&gt;<br>&gt; ______________________________<wbr>_________________<br>&gt; swift-evolution mailing list<br>&gt; swift-evolution at <a href="http://swift.org" target="_blank">swift.org</a><br>&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a></div></div></div></div></div></div></div>