<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rlemasquerier</title>
    <description>The latest articles on DEV Community by rlemasquerier (@rlemasquerier).</description>
    <link>https://dev.to/rlemasquerier</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F335094%2F768f0946-cb0e-4df2-9287-cc941d3baed3.jpg</url>
      <title>DEV Community: rlemasquerier</title>
      <link>https://dev.to/rlemasquerier</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rlemasquerier"/>
    <language>en</language>
    <item>
      <title>Learn Swift From the Standard Library: A Deep Dive Example</title>
      <dc:creator>rlemasquerier</dc:creator>
      <pubDate>Sat, 20 Jun 2020 11:03:15 +0000</pubDate>
      <link>https://dev.to/rlemasquerier/learn-swift-from-the-standard-library-a-deep-dive-example-3045</link>
      <guid>https://dev.to/rlemasquerier/learn-swift-from-the-standard-library-a-deep-dive-example-3045</guid>
      <description>&lt;p&gt;Swift is a very powerful language. It has a lot of nice built-in capabilities to improve both performances and efficiency that one get to discover along the way while becoming more familiar with the language.&lt;/p&gt;

&lt;p&gt;And a few days ago, I found out that one of the best way to discover new features is to explore an amazing showcase of Swift language characteristics, which is... The Swift Standard Library itself!&lt;/p&gt;

&lt;p&gt;To convince you, I'll show you in this article how the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator from the &lt;code&gt;Bool&lt;/code&gt; class is implemented, and how it helps us understand really well two very nice concepts, namely:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;rethrows&lt;/code&gt; declaration (I wish I had discovered this before)&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;@autoclosure&lt;/code&gt; attribute &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I learnt about this example in &lt;a href="https://www.youtube.com/watch?v=wB-bi8_rSLs"&gt;this awesome talk&lt;/a&gt; from Paul Hudson, which I recommend you to watch. &lt;/p&gt;

&lt;p&gt;I thought I could explain this from another angle, by trying to reimplement the operator from scratch by ourselves - so that we discover on the road how we come to actually need those interesting concepts.  &lt;/p&gt;

&lt;p&gt;So, let's reimplement the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator of the Swift Core library!&lt;/p&gt;

&lt;p&gt;If you come from the JavaScript world (or some other languages), be careful, &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; works only with actual &lt;code&gt;Bool&lt;/code&gt; types (&lt;code&gt;true &amp;amp;&amp;amp; "Hello"&lt;/code&gt; won't compile). Keep that in mind if you plan to give it a try by yourself. &lt;/p&gt;

&lt;p&gt;How would you implement such an operator ? Try to think about it, it will help you understand later why we would need such things like &lt;code&gt;rethrow&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To try it at home, open a playground in Xcode (File &amp;gt; New &amp;gt; Playground). In 30 seconds max, you have a sandbox ready to make your experiments.&lt;/p&gt;

&lt;p&gt;To make tests without conflicting with the existing operator, let's define a new operator, and name it for instance &lt;code&gt;&amp;amp;&amp;amp;&amp;amp;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;precedencegroup&lt;/span&gt; &lt;span class="kt"&gt;CustomAnd&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;infix&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CustomAnd&lt;/span&gt;

&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let's try to implement it. This is my first shot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nv"&gt;rhs&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This looks good, right ? We take two parameters, and we return true only if the left hand side &lt;code&gt;lhs&lt;/code&gt; is true as well a the right hand side &lt;code&gt;rhs&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's see if this works along with a small function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;canVote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;withAge&lt;/span&gt; &lt;span class="nv"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"verifying that user is allowed to vote..."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nf"&gt;canVote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;withAge&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It works, right ? We see following logs, and that's what we expected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; verifying that user is allowed to vote...
&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's try this example now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nf"&gt;canVote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;withAge&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; verifying that user is allowed to vote...
&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Oh, Crap! This is not what we expected. I mean OK, the final result is correct, but I didn't expect the first line here. In Swift, the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator is lazy, we don't want the right hand side to be evaluated if the left one is false! &lt;/p&gt;

&lt;p&gt;To handle this, what about considering the right hand side as a closure, that would be executed only when we need to ? Sounds like a good idea, let's do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;rhs&lt;/code&gt; is now a function, so we need to pass it a closure. Have a look at &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Closures.html"&gt;the doc about closures&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So now, we could use our operator like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;canVote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;withAge&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Wow bro, it solves the problem, but that's not cool at all.&lt;/p&gt;

&lt;p&gt;Yeah, sorry. We want the right hand side to keep looking as a an actual boolean, it's ugly having to explicitely pass a closure. &lt;/p&gt;

&lt;p&gt;Swift saves us here with the &lt;code&gt;@autoclosure&lt;/code&gt; attribute for our parameter to handle that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;@autoclosure&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The function signature is the same, it expects a function as a right hand side parameter. But this time, you can do it implicitely: Swift will create the closure returning the expression for us.&lt;/p&gt;

&lt;p&gt;So cool, it really solves the issue ! Now, if the left side is false, the right side won't be evaluated, and I can still use my operator easily without knowing that I'm using a closure here. &lt;/p&gt;

&lt;p&gt;I have a warning though: ⚠️ Don't overuse autoclosures in the everyday life. It's a syntaxic conveniance to omit braces when it makes sense, you should probably keep them in most cases to make it clear that it may not be executed. &lt;/p&gt;

&lt;p&gt;This kind Note in Swift doc state it well:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Overusing autoclosures can make your code hard to understand. The context and function name should make it clear that evaluation is being deferred.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So we found a solution, but did you notice the new problem it raises? What if we use an expression that can throw ? Before using an autoclosure, it was not a problem since the left-hand side and the right-hand side where evaluated before being passed to our function, so we could use a try statement as usual.&lt;/p&gt;

&lt;p&gt;For instance, let's consider this function which can throw if we feed it with an integer lower than 0:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;canVote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;withAge&lt;/span&gt; &lt;span class="nv"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="kt"&gt;CustomError&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;NegativeAgeError&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Before using &lt;code&gt;@autoclosure&lt;/code&gt;, following code could compile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nf"&gt;canVote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;withAge&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But now, Swift compiler gets angry because we specified that the second parameter is a closure &lt;strong&gt;that do not throws&lt;/strong&gt;, as clearly explained by the error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; Call can throw, but it is executed in a non-throwing autoclosure
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Well, I have to admit that the compiler is definitely right.&lt;/p&gt;

&lt;p&gt;If we want to be able to use our custom &lt;code&gt;&amp;amp;&amp;amp;&amp;amp;&lt;/code&gt; parameter with an expression that can throw (and we will!), we need to add the declaration to the rhs autoclosure and use a try before calling it. So let's rewrite it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;@autoclosure&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="nf"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Arf. It's still not compiling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; Errors thrown from here are not handled
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Well, that makes sense. Our operator should either handle errors or throw now. Let's go with the second solution, and add the declaration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;@autoclosure&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="nf"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Yay, it works. Now I can use a throwing expression on the right hand side as well.&lt;/p&gt;

&lt;p&gt;But do yo see the new problem here ? &lt;/p&gt;

&lt;p&gt;Yes that's right ! Now, our operator is a throwing function, so I &lt;strong&gt;have to&lt;/strong&gt; use a &lt;code&gt;try&lt;/code&gt; statement. Now we can't even compile a simple &lt;code&gt;false &amp;amp;&amp;amp; true&lt;/code&gt;! (We would get &lt;strong&gt;Operator can throw but expression is not marked with 'try'&lt;/strong&gt;)&lt;/p&gt;

&lt;p&gt;And that's where, if you are not already, should become a Swift enthusiast. &lt;/p&gt;

&lt;p&gt;We can use the &lt;code&gt;rethrows&lt;/code&gt; declaration: It means that our method throws only if it's used with a closure which throws. All that simple. &lt;/p&gt;

&lt;p&gt;If we come back to our problem, we now write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;@autoclosure&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;rethrows&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="nf"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And now we can write &lt;code&gt;true &amp;amp;&amp;amp; false&lt;/code&gt; without any issues, because the closure generated for the right hand side doesn't throw, so thanks to &lt;code&gt;rethrows&lt;/code&gt;, our operator is &lt;strong&gt;not&lt;/strong&gt; a throwing function, and thus, no need to use &lt;code&gt;try&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On another hand, if we use a throwing expression, then we need to add a try. From the developer point of view, the behaviour is exactly the same as if we were passing two boolean, as our first naive implementation.&lt;/p&gt;

&lt;p&gt;This is the actual implementation of the &amp;amp;&amp;amp; operator in the Swift Standard Library. You can double check it in &lt;a href="https://github.com/apple/swift/blob/master/stdlib/public/core/Bool.swift"&gt;swift source code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'm really amazed by the number of concepts such a basic feature contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;rethrows&lt;/code&gt; declaration, &lt;a href="https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID531"&gt;read the Swift doc&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;throws&lt;/code&gt; declaration&lt;/li&gt;
&lt;li&gt;An &lt;code&gt;autoclosure&lt;/code&gt;, &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Closures.html#ID543"&gt;read the Swift doc&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;A ternary condition&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're interested to go deeper, &lt;a href="https://www.youtube.com/watch?v=wB-bi8_rSLs"&gt;this video&lt;/a&gt; from Paul Hudson showcase the swift standard library with this particular example, as well as others even more interesting, focused on improving security and performance. &lt;/p&gt;

&lt;p&gt;The entire Swift core library is full of interesting patterns and optimisations. It's definitely worth to have look inside it, if you want to randomly discover new interesting concepts, or find an example of something you already know, but don't understand well.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>codequality</category>
    </item>
  </channel>
</rss>
