<div dir="ltr"><div>Moving the location of the ! will cause more readability problems, particularly with confusion with the unwrap operator, and the fact that it hides the negation operator in the middle of an expression. </div><div>A simple, very easily implemented solution needs not language change for use with a guard statement: use a function against() that returns the negated version of the express handed to it:  </div><div><br></div><div>   func against(b:Bool)-&gt;Bool { return !b }. </div><div><br></div><div>For example:</div><div><br></div><div>   guard against( xyz == nil ) else { return }</div><div><br></div><div>This only partially works with the example:</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">guard !parameters.contains(where: { !validValueRange.contains($0) }) else …<br></blockquote><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">“Make sure parameters does not contain an element such that validValueRange does not contain this element.”<br></blockquote><div><br></div><div>But after much thought, the author actually means &quot;Guard against parameters where an element is not in the validValueRange&quot;</div><div><br></div><div>If we add a trivial function</div><div><br></div><div>   func isOutOfRange(element) { return !validValueRange.contains(element) }<br></div></div><div><br></div><div>then we can write a clearly readable and understandable piece of code:</div><div><br></div><div>   guard against( parameters.contains(where:  isOutOfRange($0) ) ) else { return }<br></div><div><br></div><div>which reads: &quot;guard against parameters containing out of range elements.&quot; Problem solved.</div><div><br></div><div>Here two helper functions-one general, one specific to this code-simplify the code, without having to add to the complexity of the Swift Language. </div><div><br></div><div><br></div><div><br></div><div><br></div>From: Anton Zhilin &lt;<a href="mailto:antonyzhilin@gmail.com" target="_blank">antonyzhilin@gmail.com</a>&gt;<br>To: Darren Mo &lt;<a href="mailto:darren.mo@me.com" target="_blank">darren.mo@me.com</a>&gt;<br>Cc: swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;<br>Date: Sat, 6 Aug 2016 21:32:34 +0300<br>Subject: Re: [swift-evolution] Location of ! in Boolean negation expressions<br><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2016-08-06 10:37 GMT+03:00 Darren Mo via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Consider code like<br><br>guard !parameters.contains(where: { !validValueRange.contains($0) }) else …<br><br>Oftentimes I need to write negation expressions like this. The location of the exclamation marks really bugs me when writing and reading this code. The natural English ordering would be something like<br><br>“Make sure parameters does not contain an element such that validValueRange does not contain this element.”<br></blockquote><div>against a parameter that has a validValueRange that does not contain this element <br></div><div>guard against( parameters.contains(where: { !validValueRange.contains($)} ) ) else ..</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><br>But the programming-language-imposed ordering is<br><br>“Make sure NOT parameters contains an element such that NOT validValueRange contains this element.”</blockquote><div><br></div>One solution to this problem would be to add negative method versions wherever possible. For example: &#39;all&#39;, &#39;any&#39;, &#39;some&#39;, &#39;none&#39; methods instead of just &#39;contains(where:)&#39;.<div>Plus, we could add &#39;unless&#39; alongside &#39;guard&#39;. But these features were postponed to Stage 2.</div><div><br></div><div><span style="font-size:13px">On Sat, Aug 6, 2016 at 2:37 AM, Darren Mo via swift-evolution </span><span dir="ltr" style="font-size:13px">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span><span style="font-size:13px"> wrote:</span><br style="font-size:13px"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex;font-size:13px">Consider code like<br><br>guard !parameters.contains(where: { !validValueRange.contains($0) }) else …<br><br>Oftentimes I need to write negation expressions like this. The location of the exclamation marks really bugs me when writing and reading this code. The natural English ordering would be something like<br><br>“Make sure parameters does not contain an element such that validValueRange does not contain this element.”<br><br>But the programming-language-imposed ordering is<br><br>“Make sure NOT parameters contains an element such that NOT validValueRange contains this element.”<br><br>See how much harder the programming language version is to understand? Most of the time I write the positive version first because it comes out so naturally, and then I add the exclamation marks afterwards. It really burdens my mind every time I need to write code like this. Let’s come up with a solution to address this!<br><br>Here’s my zero-thought solution:<br><br>guard parameters.!contains(where: { validValueRange.!contains($0) }) else …<br><br>I’d love to hear alternate solutions and whether other people are having this problem too!<br><br>Darren<br>_______________________________________________<br></blockquote></div></div></div></div></div>