<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Toni Suter&apos;s Blog</title><description>My personal blog about programming and other topics.</description><link>https://tonisuter.com/</link><language>en-us</language><item><title>Understanding Copy-on-write Value Types in Swift</title><link>https://tonisuter.com/blog/2020/01/understanding-copy-on-write-value-types-swift/</link><guid isPermaLink="true">https://tonisuter.com/blog/2020/01/understanding-copy-on-write-value-types-swift/</guid><pubDate>Sun, 19 Jan 2020 13:30:00 GMT</pubDate><content:encoded>&lt;p&gt;In Swift, the standard collection types like &lt;code&gt;Array&amp;lt;Element&amp;gt;&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt; are implemented as structs and have value semantics. So when you
assign an array to a new variable or pass it as an argument to a function, a copy of the array is created. Of course instances of these
collection types can be very large and contain thousands or even millions of elements. Thus, it would not be very efficient to perform a deep
copy of all the elements every time an array or a string is passed as an argument to a function. Swift uses &lt;a href=&quot;https://en.wikipedia.org/wiki/Copy-on-write&quot;&gt;copy-on-write&lt;/a&gt;
to decide when a copy is really necessary in order to maintain value semantics.&lt;/p&gt;
&lt;p&gt;The way this works is that each array instance internally has a reference that points to a buffer which contains the actual elements. When an
array is assigned to a new variable, Swift will only perform a shallow copy at first. At this point both instances of the array share a reference to the same
buffer. Once you start mutating one of the arrays (e.g., by appending or removing an element) Swift performs a deep copy and makes sure that both instances have
a separate buffer.&lt;/p&gt;
&lt;p&gt;This blog post shows how Swift’s copy-on-write mechanism works in detail by implementing a &lt;code&gt;Stack&amp;lt;Element&amp;gt;&lt;/code&gt; value type. For the sake of simplicity, the type
has some serious limitations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Instances of &lt;code&gt;Stack&amp;lt;Element&amp;gt;&lt;/code&gt; have a fixed capacity&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Stack&amp;lt;Element&amp;gt;&lt;/code&gt; doesn’t conform to any of Swift’s collection protocols&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Stack&amp;lt;Element&amp;gt;&lt;/code&gt; doesn’t have a conditional conformance to &lt;code&gt;Equatable&lt;/code&gt; or &lt;code&gt;Hashable&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Thus, this code should not be used in production. If you ever want or need to create a stack type, you should probably just wrap
a regular Swift array (or use the &lt;code&gt;Array&amp;lt;Element&amp;gt;&lt;/code&gt; directly).&lt;/p&gt;
&lt;p&gt;That being said, let’s now have a look at how we can use the stack type.&lt;/p&gt;
&lt;h3 id=&quot;step-1-creating-an-instance-of-stackint&quot;&gt;Step 1: Creating an instance of &lt;code&gt;Stack&amp;lt;Int&amp;gt;&lt;/code&gt;&lt;/h3&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; Stack&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; stack1 &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; Stack&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;Int&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;capacity&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;stack1.&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;push&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;stack1.&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;push&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;stack1.&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;push&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;30&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;stack1: &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, stack1&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;       // stack1:  [10, 20, 30]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The following image shows what &lt;code&gt;stack1&lt;/code&gt; looks like in memory. It has a pointer to an instance of the &lt;code&gt;FixedSizeBuffer&amp;lt;Int&amp;gt;&lt;/code&gt; class which
manages a raw buffer via an &lt;code&gt;UnsafeMutablePointer&amp;lt;Int&amp;gt;&lt;/code&gt;. As you can see, the fixed-size buffer currently has a reference count of 1:&lt;/p&gt;
&lt;img src=&quot;https://tonisuter.com/_astro/copy-on-write-step1.hv4AGAaR_Z1YyPzn.webp&quot; alt=&quot;Step 1: Creating a stack instance&quot; title=&quot;Step 1: Creating a stack instance&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1154&quot; height=&quot;191&quot;&gt;
&lt;br&gt;
&lt;h3 id=&quot;step-2-creating-a-copy-of-the-stack-instance&quot;&gt;Step 2: Creating a copy of the stack instance&lt;/h3&gt;
&lt;p&gt;We can create a copy of &lt;code&gt;stack1&lt;/code&gt; by assigning it to a new variable:&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; stack2 &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; stack1           &lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;// At this point, both stacks share the same buffer.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;stack1: &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, stack1&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;     // stack1:  [10, 20, 30]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;stack2: &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, stack2&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;     // stack2:  [10, 20, 30]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As shown in the following image, both stacks now reference the same fixed-size buffer which therefore has a reference count of 2:&lt;/p&gt;
&lt;img src=&quot;https://tonisuter.com/_astro/copy-on-write-step2.BX2EXMX7_2wmV5G.webp&quot; alt=&quot;Step 2: Creating a copy of the stack instance&quot; title=&quot;Step 2: Creating a copy of the stack instance&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1122&quot; height=&quot;369&quot;&gt;
&lt;br&gt;
&lt;h3 id=&quot;step-3-mutating-one-of-the-copies&quot;&gt;Step 3: Mutating one of the copies&lt;/h3&gt;
&lt;p&gt;Finally, we pop an element from &lt;code&gt;stack2&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;stack2.&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;pop&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;                  // To maintain value semantics, stack2 clones the&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;                              // shared buffer before performing the pop() operation.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;stack1: &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, stack1&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;     // stack1:  [10, 20, 30]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;stack2: &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, stack2&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;     // stack2:  [10, 20]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;Stack&amp;lt;Element&amp;gt;&lt;/code&gt; type recognized that we are mutating a stack instance which shares its internal buffer with another stack instance.
Therefore, in order to preserve value semantics, it creates a deep copy of the fixed-size buffer before performing the mutation:&lt;/p&gt;
&lt;img src=&quot;https://tonisuter.com/_astro/copy-on-write-step3.BOk-fqns_27LIa5.webp&quot; alt=&quot;Step 3: Mutating one of the copies&quot; title=&quot;Step 3: Mutating one of the copies&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1122&quot; height=&quot;451&quot;&gt;
&lt;br&gt;
&lt;h3 id=&quot;implementation&quot;&gt;Implementation&lt;/h3&gt;
&lt;p&gt;Let’s look at the implementation of the &lt;code&gt;Stack&amp;lt;Element&amp;gt;&lt;/code&gt; type. The key section of this code is the computed property &lt;code&gt;bufferForWriting&lt;/code&gt;.
Whenever we want to mutate the stack, we access its internal fixed-size buffer via this property. It uses the built-in function
&lt;a href=&quot;https://developer.apple.com/documentation/swift/2429905-isknownuniquelyreferenced&quot;&gt;&lt;code&gt;isKnownUniquelyReferenced()&lt;/code&gt;&lt;/a&gt; to test whether there
is only one stack instance that references the buffer. If the buffer is shared with another stack, a new copy of the buffer is created:&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; Stack&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;Element&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; CustomStringConvertible &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; buffer: FixedSizeBuffer&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;Element&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt; init&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF;font-style:italic&quot;&gt;capacity&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;Int&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;        self.buffer &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; FixedSizeBuffer&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;capacity&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; capacity&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; bufferForWriting: FixedSizeBuffer&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;Element&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C792EA&quot;&gt;        mutating&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt; get&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;isKnownUniquelyReferenced&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&amp;amp;&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;buffer&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;                buffer &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; FixedSizeBuffer&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;cloning&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; buffer&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; buffer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; count: &lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;Int&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; buffer.count &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; capacity: &lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;Int&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; buffer.capacity &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; isEmpty: &lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;Bool&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; count &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt; mutating&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt; func&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; push&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;color:#BABED8;font-style:italic&quot;&gt; element&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;Element&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;        precondition&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;count &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; capacity, &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;Can&amp;#39;t push element onto a full stack&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;        bufferForWriting.&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;element&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt;discardableResult&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt; mutating&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt; func&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; pop&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; Element&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;        precondition&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(!&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;isEmpty, &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;Can&amp;#39;t pop last element from an empty stack&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; bufferForWriting.&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;removeLast&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt; func&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; peek&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; Element&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;        guard&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;isEmpty &lt;/span&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; nil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; buffer&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;count &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; description: &lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;        var&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; result &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;        result &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;..&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;                    .&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;reflecting&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; buffer&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;$0&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;])&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;                    .&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;joined&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;separator&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;        result &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;FixedSizeBuffer&amp;lt;Element&amp;gt;&lt;/code&gt; class is not shown here, but you can find the full source code plus a few unit tests on GitHub at the following
URL: &lt;a href=&quot;https://github.com/tonisuter/COW-Stack&quot;&gt;https://github.com/tonisuter/COW-Stack&lt;/a&gt;.&lt;/p&gt;</content:encoded><author>Toni Suter</author></item><item><title>Unbounded Ranges in Swift 4</title><link>https://tonisuter.com/blog/2017/08/unbounded-ranges-swift-4/</link><guid isPermaLink="true">https://tonisuter.com/blog/2017/08/unbounded-ranges-swift-4/</guid><pubDate>Mon, 21 Aug 2017 20:30:00 GMT</pubDate><content:encoded>&lt;p&gt;In Swift, the expression &lt;code&gt;1...10&lt;/code&gt; represents a closed range from 1 through 10. The &lt;code&gt;...&lt;/code&gt; is an infix operator
that is declared in the standard library and the expression &lt;code&gt;1...10&lt;/code&gt; applies the corresponding &lt;code&gt;...&lt;/code&gt; operator
function to the operands 1 and 10. The value that results from this expression is an instance of the type
&lt;code&gt;CountableClosedRange&amp;lt;Int&amp;gt;&lt;/code&gt; which is also declared in the standard library. Additionally, Swift 4 adds one-sided
ranges to the standard library as described in proposal &lt;a href=&quot;https://github.com/apple/swift-evolution/blob/master/proposals/0172-one-sided-ranges.md&quot;&gt;SE-172&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;However, recently I discovered that there is yet another kind of range in the Swift 4 standard library: the so-called &lt;em&gt;unbounded range&lt;/em&gt;.
Here’s how you can use unbounded ranges:&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; str1 &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;Example&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; str2 &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; str1&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;[...]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;str1, &lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; str1&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;    // Example String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;str2, &lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; str2&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;    // Example Substring&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; arr1 &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; arr2 &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; arr1&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;[...]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;arr1, &lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; arr1&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;    // [1, 2, 3] Array&amp;lt;Int&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;arr2, &lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; arr2&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;    // [1, 2, 3] ArraySlice&amp;lt;Int&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When you pass an unbounded range to the subscript of any collection, you get back a subsequence that contains all elements of the original collection. Apparently, this is a useful thing to do in a few rare cases, but I am going to ignore that here, because I am more interested
in how this is implemented.&lt;/p&gt;
&lt;p&gt;At first I thought that the Swift team must have added support for this in the Swift compiler, since it is not possible to declare
an operator that doesn’t take any operands. But after a little bit of digging, I realized that this can be implemented with Swift’s existing language features and doesn’t require any changes to the compiler. Here’s how it works.&lt;/p&gt;
&lt;p&gt;In contrast to the other ranges, the expression &lt;code&gt;...&lt;/code&gt; doesn’t actually call an operator function and doesn’t create an instance of some kind of range type. Instead it is just a reference to an operator function in the standard library. Here’s the declaration of that operator function (see &lt;a href=&quot;https://github.com/apple/swift/blob/61ad3c07ef94bb9280b90833baefa95228cf031b/stdlib/public/core/Range.swift.gyb#L1170-L1174&quot;&gt;Range.swift.gyb&lt;/a&gt;):&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt; enum&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; UnboundedRange_&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt; postfix&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt; func&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; ...&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#82AAFF;font-style:italic&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;: UnboundedRange_&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; ()&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;        fatalError&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;uncallable&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see, the operator function is declared as a static postfix operator function inside an enum type. However, those details don’t
really matter, because the function is uncallable. This is because, the enum &lt;code&gt;UnboundedRange_&lt;/code&gt; is a so-called &lt;em&gt;uninhabited type&lt;/em&gt;, which means that it is a type that doesn’t have any values. Therefore, we can’t create an instance of &lt;code&gt;UnboundedRange_&lt;/code&gt; which would be required in order to call the &lt;code&gt;...&lt;/code&gt; operator function. Instead, we can only reference the function. This function reference is then directly passed as an argument to the subscript. The following code shows how this subscript is implemented for types that adopt the &lt;code&gt;_Indexable&lt;/code&gt; protocol (see &lt;a href=&quot;https://github.com/apple/swift/blob/61ad3c07ef94bb9280b90833baefa95228cf031b/stdlib/public/core/Range.swift.gyb#L1175-L1188&quot;&gt;Range.swift.gyb&lt;/a&gt;):&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; typealias&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; UnboundedRange&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;UnboundedRange_&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)-&amp;gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C792EA&quot;&gt;extension&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; _Indexable&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;    // ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt;_inlineable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:#C792EA&quot;&gt; subscript&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF;font-style:italic&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;: UnboundedRange&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; SubSequence&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;startIndex&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;...]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is a typealias called &lt;code&gt;UnboundedRange&lt;/code&gt; which makes it easier to declare methods and subscripts that take an unbounded range as an argument. In the example above, there is not much we can do with the parameter &lt;code&gt;x&lt;/code&gt; inside the body of the subscript since &lt;code&gt;x&lt;/code&gt; is an uncallable function and doesn’t have any properties or methods. Thus, the implementation just delegates to another subscript, passing a one-sided range that covers the entire collection.&lt;/p&gt;</content:encoded><author>Toni Suter</author></item><item><title>Bi-directional Type Inference in Swift</title><link>https://tonisuter.com/blog/2017/08/bi-directional-type-inference-swift/</link><guid isPermaLink="true">https://tonisuter.com/blog/2017/08/bi-directional-type-inference-swift/</guid><pubDate>Sun, 06 Aug 2017 14:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A few months ago, I noticed the following paragraph while reading &lt;a href=&quot;https://github.com/apple/swift/blob/master/docs/TypeChecker.rst&quot;&gt;Swift’s Type Checker documentation&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Swift’s type inference allows type information to flow in two directions. As in most mainstream languages, type information can flow from the leaves of the expression tree up to the root. However, Swift also allows type information to flow from the context at the root of the expression tree down to the leaves. This bi-directional type inference is common in languages that use ML-like type systems, but is not present in mainstream languages like C++, Java, C#, or Objective-C.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;At that time, I had never heard the term &lt;em&gt;bi-directional type inference&lt;/em&gt; before. While I did know about certain language features (e.g., closures, implicit member expressions) that rely on this kind of type inference, I didn’t realize that it also impacts things like overload resolution.&lt;/p&gt;
&lt;p&gt;In this blog post, I show a few examples of bottom-up type inference and top-down type inference which can be subsumed under the term bi-directional type inference. Knowing how Swift uses bi-directional type inference can give you a better understanding of its type system.&lt;/p&gt;
&lt;h3 id=&quot;bottom-up-type-inference&quot;&gt;Bottom-up Type Inference&lt;/h3&gt;
&lt;p&gt;Bottom-up type inference is the kind of type inference where the type information flows from the bottom of the abstract syntax tree (AST) to the top. It is the more common kind of type inference and is used in many statically-typed
programming languages. The following code snippet shows a few examples of bottom-up type inference in Swift:&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; age &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; 26&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;              // age is of type Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; name &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;Toni&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;         // name is of type String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; pair &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;age, name&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;    // pair is of type (Int, String)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The type of &lt;code&gt;pair&lt;/code&gt; is inferred from its initial value &lt;code&gt;(age, name)&lt;/code&gt;. The following picture shows the AST for that expression:&lt;/p&gt;
&lt;img src=&quot;https://tonisuter.com/_astro/bottom-up-type-inference-ast.gFkWa2dr_ZXtsFl.webp&quot; alt=&quot;AST for expression (age, name)&quot; title=&quot;AST for expression (age, name)&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1194&quot; height=&quot;762&quot;&gt;
&lt;p&gt;The type of each identifier expression is determined by performing a name lookup. For example, for the name &lt;code&gt;age&lt;/code&gt;, the type checker finds a constant that has the type &lt;code&gt;Int&lt;/code&gt;. Similarly, for the name &lt;code&gt;name&lt;/code&gt; it finds a constant of type &lt;code&gt;String&lt;/code&gt;. Finally, the type of the tuple expression at the root of the AST is a tuple type that is composed of the types of the two tuple elements, resulting in the type &lt;code&gt;(Int, String)&lt;/code&gt;. Thus, the type information flows from the leaves of the tree up to the root.&lt;/p&gt;
&lt;p&gt;With bottom-up type inference, leaf nodes have an intrinsic type. For example, integer literals default to the type &lt;code&gt;Int&lt;/code&gt; and string literals default to the type &lt;code&gt;String&lt;/code&gt; which is what happens when the compiler infers the types of the constants &lt;code&gt;age&lt;/code&gt; and &lt;code&gt;name&lt;/code&gt;. Similarly, an identifier expression references some entity (e.g., a variable or a function) that also has a type.&lt;/p&gt;
&lt;h3 id=&quot;top-down-type-inference&quot;&gt;Top-down Type Inference&lt;/h3&gt;
&lt;p&gt;As mentioned above, Swift also allows type information to flow in the other direction; from top to bottom. This can be seen with various kinds of expressions such as literals, implicit member expressions, closures and function call expressions.&lt;/p&gt;
&lt;h4 id=&quot;literals&quot;&gt;Literals&lt;/h4&gt;
&lt;p&gt;First, let’s look at Swift’s literals:&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; x &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;             // x is of type Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; y: &lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;Double&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;     // y is of type Double&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This code illustrates that integer literals can be used to initialize constants / variables of type &lt;code&gt;Int&lt;/code&gt; as well as ones that are of type &lt;code&gt;Double&lt;/code&gt;. By default, an integer literal is of type &lt;code&gt;Int&lt;/code&gt;. With an explicit type annotation we can change it to be of type &lt;code&gt;Double&lt;/code&gt; instead.&lt;/p&gt;
&lt;p&gt;The type annotation in a constant / variable declaration acts as a contextual type for the initial value. There are other language constructs where there is a contextual type for an expression, but I will talk about that in a different blog post. Integer literals can be used wherever the contextual type conforms to the &lt;code&gt;ExpressibleByIntegerLiteral&lt;/code&gt; protocol. Types such as &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;UInt&lt;/code&gt;, &lt;code&gt;Double&lt;/code&gt; and &lt;code&gt;Float&lt;/code&gt; all conform to
this protocol.&lt;/p&gt;
&lt;p&gt;Note that the above example only works, because an integer literal is not the same as an instance of type &lt;code&gt;Int&lt;/code&gt;. In contrast, the following code does not compile, because &lt;code&gt;x&lt;/code&gt; is an instance of type &lt;code&gt;Int&lt;/code&gt; and there is no implicit conversion from &lt;code&gt;Int&lt;/code&gt; to &lt;code&gt;Double&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; x &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;             // x is of type Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; y: &lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;Double&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; x     &lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;// error: cannot convert value of type &amp;#39;Int&amp;#39; to specified type &amp;#39;Double&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we really wanted to convert &lt;code&gt;x&lt;/code&gt; into a &lt;code&gt;Double&lt;/code&gt;, we would have to write &lt;code&gt;let y = Double(x)&lt;/code&gt; in order to explicitly convert it by calling one of &lt;code&gt;Double&lt;/code&gt;’s initializers.&lt;/p&gt;
&lt;p&gt;Other literals work in the same vein. For example, array literals default to the type &lt;code&gt;Array&amp;lt;T&amp;gt;&lt;/code&gt; but they can also be used to initialize constants / variables of type &lt;code&gt;Set&amp;lt;T&amp;gt;&lt;/code&gt; because the type &lt;code&gt;Set&lt;/code&gt; conforms to the &lt;code&gt;ExpressibleByArrayLiteral&lt;/code&gt; protocol:&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; arr &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;]                 &lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;// arr is of type Array&amp;lt;Int&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; set: &lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;Set&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;Int&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;]       &lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;// set is of type Set&amp;lt;Int&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;implicit-member-expressions&quot;&gt;Implicit Member Expressions&lt;/h4&gt;
&lt;p&gt;Implicit member expressions are often used to abbreviate references to enum cases, when the enum type is known from the context:&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C792EA&quot;&gt;enum&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; Season&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C792EA&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; spring&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C792EA&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; summer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C792EA&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; fall&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C792EA&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; winter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; season &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; Season.summer          &lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;// season is of type Season&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;season &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; .fall&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, the variable &lt;code&gt;season&lt;/code&gt; is first initialized with the explicit member expression &lt;code&gt;Season.summer&lt;/code&gt;, which means that its inferred type is &lt;code&gt;Season&lt;/code&gt;. Since the variable &lt;code&gt;season&lt;/code&gt; now has a fixed type, we can use the implicit member expression &lt;code&gt;.fall&lt;/code&gt; to change its value. In other words, the type of the variable acts as a contextual type for the expression &lt;code&gt;.fall&lt;/code&gt;.&lt;/p&gt;
&lt;h4 id=&quot;closures&quot;&gt;Closures&lt;/h4&gt;
&lt;p&gt;Closures are another one of Swift’s language features that heavily relies on top-down type inference. The parameter types and return types of closures are often not specified explicitly but instead inferred from the closure’s context:&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; numbers &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;-2&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;]            &lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;// numbers is of type Array&amp;lt;Int&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; positiveNumbers &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; numbers.&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; n &lt;/span&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; n &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;positiveNumbers&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;              // [1, 3]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, the &lt;code&gt;filter()&lt;/code&gt; method expects a single argument of type &lt;code&gt;(Int) -&amp;gt; Bool&lt;/code&gt;. This is also the contextual type for the closure that is passed as argument, which in turn means that the inferred type for &lt;code&gt;n&lt;/code&gt; is &lt;code&gt;Int&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Let’s change this example a little bit by using floating point literals instead of integer literals in the initial value of the constant &lt;code&gt;numbers&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; numbers &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;-2.0&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;3.0&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;]      &lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;// numbers is of type Array&amp;lt;Double&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; positiveNumbers &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; numbers.&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; n &lt;/span&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; n &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;positiveNumbers&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;              // [1.0, 3.0]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now the inferred type of &lt;code&gt;numbers&lt;/code&gt; is &lt;code&gt;Array&amp;lt;Double&amp;gt;&lt;/code&gt; instead of &lt;code&gt;Array&amp;lt;Int&amp;gt;&lt;/code&gt; which also means that the &lt;code&gt;filter()&lt;/code&gt; method now expects an argument of type &lt;code&gt;(Double) -&amp;gt; Bool&lt;/code&gt;. Additionally, the type of &lt;code&gt;n&lt;/code&gt; has changed to &lt;code&gt;Double&lt;/code&gt; as well.&lt;/p&gt;
&lt;h4 id=&quot;overload-resolution&quot;&gt;Overload Resolution&lt;/h4&gt;
&lt;p&gt;In Swift, functions can be overloaded. At compile time, the type checker needs to determine, which of the overloads is called in a particular function call. In contrast to many other programming languages, overload resolution in Swift not only depends on the argument types but also on the contextual type of a function call:&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C792EA&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; Int&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C792EA&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; x &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;                   // error: ambiguous use of &amp;#39;f()&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; y: &lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;Int&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;              // Overload Resolution picks f: () -&amp;gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; z: &lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;           // Overload Resolution picks f: () -&amp;gt; String&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the declaration of &lt;code&gt;x&lt;/code&gt; there is no contextual type, which makes the function call &lt;code&gt;f()&lt;/code&gt; ambiguous, because the type checker doesn’t know which overload to pick. In the declarations of &lt;code&gt;y&lt;/code&gt; and &lt;code&gt;z&lt;/code&gt; there is an explicit type annotation, which allows the type checker to choose the correct overload with the matching return type. Note that in many programming languages it is not possible to declare two functions that only differ in their return type.&lt;/p&gt;
&lt;p&gt;This kind of top-down type inference works through multiple levels of indirection as shown in the following example:&lt;/p&gt;
&lt;pre class=&quot;astro-code material-theme-palenight&quot; style=&quot;background-color:#292D3E;color:#babed8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;swift&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C792EA&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; Int&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C792EA&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3E88D&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C792EA&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;color:#BABED8;font-style:italic&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt;: T&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; T &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF;font-style:italic&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; x &lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;()))&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;           // error: ambiguous use of &amp;#39;f()&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; y: &lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;Int&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;()))&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;      // Overload Resolution picks f: () -&amp;gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F78C6C&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:#BABED8&quot;&gt; z: &lt;/span&gt;&lt;span style=&quot;color:#FFCB6B&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#82AAFF&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#89DDFF&quot;&gt;()))&lt;/span&gt;&lt;span style=&quot;color:#676E95;font-style:italic&quot;&gt;   // Overload Resolution picks f: () -&amp;gt; String&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here the result of the function call &lt;code&gt;f()&lt;/code&gt; is passed through two invocations of the generic identity function &lt;code&gt;id()&lt;/code&gt;. The declaration of &lt;code&gt;x&lt;/code&gt; is still ambiguous, since there is no explicit type annotation. For the constants &lt;code&gt;y&lt;/code&gt; and &lt;code&gt;z&lt;/code&gt; the explicit type annotation determines the type that should be used as the argument for the generic parameter &lt;code&gt;T&lt;/code&gt; of the outer &lt;code&gt;id()&lt;/code&gt; call. The generic parameter &lt;code&gt;T&lt;/code&gt; of the outer &lt;code&gt;id()&lt;/code&gt; call is then used as the argument for the generic parameter &lt;code&gt;T&lt;/code&gt; of the inner &lt;code&gt;id()&lt;/code&gt; call. Finally, the generic parameter &lt;code&gt;T&lt;/code&gt; of the inner &lt;code&gt;id()&lt;/code&gt; call constrains the call to &lt;code&gt;f()&lt;/code&gt; and allows the type checker to choose the correct overload with the matching return type.&lt;/p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;This blog post has shown several examples of bottom-up and top-down type inference. Swift’s type checker uses a constraint-based system to implement this kind of bi-directional type inference. I will write more about these implementation details in future blog posts.&lt;/p&gt;</content:encoded><author>Toni Suter</author></item></channel></rss>