<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.0">Jekyll</generator><link href="https://www.drawingyourownconclusions.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://www.drawingyourownconclusions.com/" rel="alternate" type="text/html" /><updated>2021-05-18T13:55:30+00:00</updated><id>https://www.drawingyourownconclusions.com/feed.xml</id><title type="html">Drawing Your Own Conclusions</title><subtitle>Daniel (and sometimes guests) try to come up with sketches that make difficult concepts more clear. We invite contributions from the community as well.</subtitle><entry><title type="html">Functions</title><link href="https://www.drawingyourownconclusions.com/2020/08/31/Functions.html" rel="alternate" type="text/html" title="Functions" /><published>2020-08-31T00:00:00+00:00</published><updated>2020-08-31T00:00:00+00:00</updated><id>https://www.drawingyourownconclusions.com/2020/08/31/Functions</id><content type="html" xml:base="https://www.drawingyourownconclusions.com/2020/08/31/Functions.html">&lt;p&gt;I’m sharing the September post a day early.&lt;/p&gt;

&lt;p&gt;There’s nothing really profound in this one - I’m just continuing to search for visual ways to describe functions.&lt;/p&gt;

&lt;p&gt;This one will be included in the free update (well free if you own it already - if not you can buy it now and get the free update once it ships) of &lt;a href=&quot;https://gum.co/swift-kickstart&quot;&gt;A Swift Kickstart&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This book is for folks new to Swift and so I gently start with the classic Hello World function. I’ve implemented it as “Hi World” because it fits better in comic form.&lt;/p&gt;

&lt;p&gt;I know that this daring decision might throw you but it really isn’t to challenge years of tradition so much as to fit in large font in small rectangles.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hi, World!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Although this is a good and quick example in Xcode playgrounds, Swift playgrounds does not have a console which drives us to quickly include a function that returns a String rather than print it to the console.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;s&quot;&gt;&quot;Hi, World!&quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;By the way, if you haven’t been keeping up with Swift, this might look funny as we don’t need to include the return in a function with only a single expression. In other words we don’t need the following&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// the return in the following line is superfluous&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hi, World!&quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;“But Daniel,” you say, “it looks funny without the return.”&lt;/p&gt;

&lt;p&gt;At first it does. Just as it looked funny, for those of us coming from Objective-C to see the return type at the end of the function signature or to not have semicolons at the end of a line.&lt;/p&gt;

&lt;p&gt;It will grow on you.&lt;/p&gt;

&lt;p&gt;In fact, you’ll wonder why return isn’t just assumed for the last expression of a function that is supposed to return something no matter how many lines it is.&lt;/p&gt;

&lt;p&gt;i.e. it feels silly that this is fine:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;s&quot;&gt;&quot;Hi, World!&quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;but this doesn’t compile&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;This doesn't compile - the next line needs a return&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;s&quot;&gt;&quot;Hi, World!&quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Anyway, back to the point. We really want functions that take input and produce output. That is the final form of this silly example.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;s&quot;&gt;&quot;Hi, &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;!&quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here it is in comic form.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/08/31/Functions.png&quot; alt=&quot;Functions&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As always, let me know what you think.&lt;/p&gt;</content><author><name></name></author><summary type="html">I’m sharing the September post a day early.</summary></entry><entry><title type="html">Parse</title><link href="https://www.drawingyourownconclusions.com/2020/08/03/Parse.html" rel="alternate" type="text/html" title="Parse" /><published>2020-08-03T00:00:00+00:00</published><updated>2020-08-03T00:00:00+00:00</updated><id>https://www.drawingyourownconclusions.com/2020/08/03/Parse</id><content type="html" xml:base="https://www.drawingyourownconclusions.com/2020/08/03/Parse.html">&lt;p&gt;I had one of those moments last week.&lt;/p&gt;

&lt;p&gt;One of those “how did I never see this before” moments.&lt;/p&gt;

&lt;p&gt;Although I have a suspicion based on a talk I gave a couple of years ago at NSSpain that it was more of a “I understood this at one time, how did I forget it” moments.&lt;/p&gt;

&lt;p&gt;Anyway, I’d reread a part of Paul Chiusano and Runar Bjarnason’s book &lt;a href=&quot;https://www.manning.com/books/functional-programming-in-scala&quot;&gt;Functional Programming in Scala&lt;/a&gt;. I like and recommend this book even for those of us who don’t code in Scala.&lt;/p&gt;

&lt;p&gt;I was implementing an example of a Parser in Swift. Something like this:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Parser&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Substring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Substring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;They have an example of a Random Number Generator in an earlier chapter. You create an RNG that returns a random Int. Then you use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map()&lt;/code&gt; to transform it into an RNG that returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Double&lt;/code&gt;s or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bool&lt;/code&gt;s. Then you learn to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flatMap()&lt;/code&gt; to return the next five random somethings or the first random something that meets a condition. This is a common example for State patterns.&lt;/p&gt;

&lt;p&gt;Later they are looking at Parser combinators and I translated it to Swift as something like the above.&lt;/p&gt;

&lt;p&gt;Then I stopped.&lt;/p&gt;

&lt;p&gt;This looks a lot like State.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;State&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In fact for Random Number Generators we defined a type named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RNG&lt;/code&gt; and created our Random number generator that returned &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt;s as being a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;typealias Random&amp;amp;lt;A&amp;amp;gt; = State&amp;amp;lt;RNG, A&amp;amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Parser&lt;/code&gt; could be seen as essentially &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;typealias Parser&amp;amp;lt;A&amp;amp;gt; = State&amp;amp;lt;Substring, A&amp;amp;gt;&lt;/code&gt; where we replace &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;run&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parse&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;All of a sudden Parser Combinators felt more like something that belonged to a family and not some new concept.&lt;/p&gt;

&lt;p&gt;So that’s why we struggle with these abstractions - at some point we look at a new concept and say, “wait, that looks just like this other thing.”&lt;/p&gt;

&lt;p&gt;This month’s comic is not very visual. Just the “mind-blown” emoji because I couldn’t find the “repeatedly smacking myself in the head  for not seeing it sooner” emoji.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/08/03/ParseComic.png&quot; alt=&quot;Parser&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I love that our understanding of these notions deepens over time.&lt;/p&gt;</content><author><name></name></author><summary type="html">I had one of those moments last week.</summary></entry><entry><title type="html">State</title><link href="https://www.drawingyourownconclusions.com/2020/07/06/State.html" rel="alternate" type="text/html" title="State" /><published>2020-07-06T00:00:00+00:00</published><updated>2020-07-06T00:00:00+00:00</updated><id>https://www.drawingyourownconclusions.com/2020/07/06/State</id><content type="html" xml:base="https://www.drawingyourownconclusions.com/2020/07/06/State.html">&lt;p&gt;I loved this year’s WWDC session &lt;a href=&quot;https://developer.apple.com/videos/play/wwdc2020/10040/&quot;&gt;Data Essentials in SwiftUI&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It’s an example of the Newton’s Method analogy of how we learn. We have a first impression and use that to get closer and closer to the truth. (I think I first saw this description in Massey’s book on Topology.)&lt;/p&gt;

&lt;p&gt;Last year I thought I understood how &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@State&lt;/code&gt; worked.&lt;/p&gt;

&lt;p&gt;It’s not that I was wrong - it was more that my understanding was incomplete.&lt;/p&gt;

&lt;p&gt;The magic of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@State&lt;/code&gt; is that it takes a property that is a value type contained in a struct and moves the storage outside of that struct. The struct now contains a reference to that property. This allows a button action to mutate that property without mutating the struct. The value type now behaves like a reference type.&lt;/p&gt;

&lt;p&gt;This year’s session showed that it’s even more magical than that.&lt;/p&gt;

&lt;p&gt;These structs that conform to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;View&lt;/code&gt; protocol are very lightweight and might be discarded once they are rendered on screen.&lt;/p&gt;

&lt;p&gt;Imagine I have a simple struct like this.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;MyView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;@State&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  
  &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;VStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;description&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Tap here&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Suppose the instance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MyView&lt;/code&gt; is rendered and discarded. The user now taps the button. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt; changes and so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MyView&lt;/code&gt; must be instantiated again so that it can be updated on the screen.&lt;/p&gt;

&lt;p&gt;When the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MyView&lt;/code&gt; instance is discarded, something needs to hold on to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt; reference or we’ll start at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; again.&lt;/p&gt;

&lt;p&gt;SwiftUI holds onto it for us and connects this reference to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt; to the new instance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MyView&lt;/code&gt;. It feels as if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MyView&lt;/code&gt; never went away.&lt;/p&gt;

&lt;p&gt;I’ve tried to capture this magic in the following comic which I’ll be including in the update to my book &lt;a href=&quot;https://gumroad.com/l/swiftuikickstart&quot;&gt;A SwiftUI Kickstart&lt;/a&gt; which I’ll be updating for free this summer.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/07/06/State.png&quot; alt=&quot;State&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I love that our understanding of these notions deepens over time.&lt;/p&gt;</content><author><name></name></author><summary type="html">I loved this year’s WWDC session Data Essentials in SwiftUI.</summary></entry><entry><title type="html">More Methods and Functions</title><link href="https://www.drawingyourownconclusions.com/2020/06/01/methods.html" rel="alternate" type="text/html" title="More Methods and Functions" /><published>2020-06-01T00:00:00+00:00</published><updated>2020-06-01T00:00:00+00:00</updated><id>https://www.drawingyourownconclusions.com/2020/06/01/methods</id><content type="html" xml:base="https://www.drawingyourownconclusions.com/2020/06/01/methods.html">&lt;p&gt;In the &lt;a href=&quot;https://www.drawingyourownconclusions.com/2020/04/06/methods-and-functions.html&quot;&gt;previous post&lt;/a&gt;  I looked at traditional ways of representing functions and asked how we might appropriately represent methods.&lt;/p&gt;

&lt;p&gt;With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map()&lt;/code&gt;, for example, Swift used to implement it as a free function. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map()&lt;/code&gt; on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt; had to accept both the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt; it was acting on as well as the function it was applying to every element of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;. The signature was something like this.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Output&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; 
                        &lt;span class=&quot;n&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We would call it like this.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myArrayOfStrings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is the rectangle shape with the funnel at the top where we dump in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt; of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt;s and the function and with the funnel at the bottom that returns an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt; of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt;s. So …&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Where&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;are&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;the&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;drawings&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]){&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[5, 3, 3, 8]&lt;/code&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;In Swift 2 we got protocol extensions and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map()&lt;/code&gt; for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;s was implemented in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sequence&lt;/code&gt;. Forget about the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;throws&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rethrows&lt;/code&gt;, this signature was essentially the following.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Output&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We no longer needed to specify the generic type of the input &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt; as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map()&lt;/code&gt; is a member and knows the type is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Element&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We also no longer needed to pass in the starting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt; as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map()&lt;/code&gt; has access to everything in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self&lt;/code&gt;. So here’s how we call this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map()&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Where&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;are&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;the&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;drawings&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I experimented with many ways of representing methods and asked for suggestions&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;Jordan Rose suggested two. One was cartoon style conversation bubbles - which I love. I include these in my talks to allow the code to talk back to me.&lt;/p&gt;

&lt;p&gt;His other suggestion (included with his permission) looks like this.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/06/01/Jordan.png&quot; alt=&quot;Jordans Method&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I really like this idea. It feels like the dot notation and communicates input from two directions: one from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self&lt;/code&gt; and one from the parameters being passed. It also separates these information origins nicely and allows us to chain methods.&lt;/p&gt;

&lt;p&gt;This chain was emphasized by the second drawing I was sent.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;Lars Christiansen sent me this image of a train. (Also included with permission)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/06/01/Lars.png&quot; alt=&quot;Lars Method&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In the second car we see a method that receives its input from the car before and takes no additional parameters. In the next car we see a method that requires both the car before it and additional information for the amount we are increasing by.&lt;/p&gt;

&lt;p&gt;Again I love this idea. It’s a simple way to communicate what’s going on.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;It also fits nicely with a train-track approach that Scott Wlaschin uses in his wonderful F# talks and writing that you can find on &lt;a href=&quot;https://fsharpforfunandprofit.com&quot;&gt;fsharpforfunandprofit.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Scott gives engaging talks on Functional Programming and uses train tracks to communicate concepts such as map and flatMap.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;So I’ve been thinking about drawing comics to illustrate some programming concepts.&lt;/p&gt;

&lt;p&gt;Just another medium for communication.&lt;/p&gt;

&lt;p&gt;So I’ve been adding ABDCE’s to my &lt;a href=&quot;https://gum.co/functionalKickstart&quot;&gt;Functional Programming Kickstart&lt;/a&gt; where ABDCE is “A Badly Drawn Comic Explanation”.&lt;/p&gt;

&lt;p&gt;For now I’m using Comic Life 3 and essentially drawing with their basic shapes. Here’s my attempt at functions and methods.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/06/01/Functions.png&quot; alt=&quot;Functions and Methods&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I’m also taking some sketching classes and cartooning classes and hope to replace these with even more poorly drawn comics.&lt;/p&gt;

&lt;p&gt;We’ll see how it goes.&lt;/p&gt;</content><author><name></name></author><summary type="html">In the previous post I looked at traditional ways of representing functions and asked how we might appropriately represent methods.</summary></entry><entry><title type="html">Methods and Functions</title><link href="https://www.drawingyourownconclusions.com/2020/04/06/methods-and-functions.html" rel="alternate" type="text/html" title="Methods and Functions" /><published>2020-04-06T00:00:00+00:00</published><updated>2020-04-06T00:00:00+00:00</updated><id>https://www.drawingyourownconclusions.com/2020/04/06/methods-and-functions</id><content type="html" xml:base="https://www.drawingyourownconclusions.com/2020/04/06/methods-and-functions.html">&lt;p&gt;This month I’m inviting you to play along.&lt;/p&gt;

&lt;p&gt;I’ve been working on a book about functional programming in Swift. To me that means “and methods too” but I’ve found that’s not obvious.&lt;/p&gt;

&lt;p&gt;So in this brief post I want to look at methods vs functions both in who they are and in how we talk to them.&lt;/p&gt;

&lt;p&gt;I know the classic image for a function but I’m not sure how to represent a method - particularly when it’s time to chain the methods. Your challenge is included at the end of this post.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;Suppose I have a simple function that takes a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt; and returns an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;wordCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then we call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wordCount()&lt;/code&gt; like this:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;wordCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Generally, we use a “function machine” to represent this. I used to teach High School Mathematics, and this is what we drew on the board to represent functions.&lt;/p&gt;

&lt;p&gt;“Drew on the board?” you ask.&lt;/p&gt;

&lt;p&gt;Yes. Search the internet for “blackboard” and “chalk” or ask an older person.&lt;/p&gt;

&lt;p&gt;Anyway, the picture looked like this.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/04/06/FunctionMachine.png&quot; alt=&quot;Function Machine&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Let’s represent composition next. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wordCount()&lt;/code&gt; was a function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(String) -&amp;gt; Int&lt;/code&gt; so the next function must take an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As a silly example, suppose, because I live in the US, I want to convert my &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wordCount()&lt;/code&gt; result from Celsius to Fahrenheit. The conversion from C -&amp;gt; F is given by&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;inFahrenheit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So to calculate my word count in Fahrenheit, we have to compose these functions&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;inFahrenheit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;wordCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This works nicely in an image.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/04/06/Compose.png&quot; alt=&quot;Function Composition&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;So that works nicely for functions that take a single input. But what about functions that take two inputs.&lt;/p&gt;

&lt;p&gt;For example, here’s a function that increases an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt; by another &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt; amount. We could call it &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add()&lt;/code&gt;, I’ll leave it like this&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;increase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;increment&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I’m less interested in the implementation than I am with the picture. But first let’s call the function.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;increase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And here’s how we compose it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wordCount()&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;increase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;wordCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The code is now harder to parse but the image still isn’t so bad.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/04/06/TwoArgs.png&quot; alt=&quot;Function With Two Arguments&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The first parameter falls through from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wordCount()&lt;/code&gt; and we pass in the additional parameter&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;The code is cleaner and the methods are more discoverable if we use methods instead of functions.&lt;/p&gt;

&lt;p&gt;Add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wordCount()&lt;/code&gt; as a method in an extension to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;wordCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We call it like this.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;wordCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The argument of the function moves out front as the receiver of the message.&lt;/p&gt;

&lt;p&gt;I know that “receiver” is very Objective-C of me but it feels more natural for me to think of it that way then the more formal “instance on which the method is called”.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/04/06/FunctionVsMethod.png&quot; alt=&quot;Function vs Method&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Similarly, we can define our other two functions as methods in an extension of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;inFahrenheit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;increase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;increment&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We call them as methods like this.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;inFahrenheit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;increase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice in this implementation that one of function’s parameters has come inside our method as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In other words if we use a function not a method then we have to explicitly pass the information contained in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self&lt;/code&gt; in the method to the function.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/04/06/ImpliedSelf.png&quot; alt=&quot;The implied self&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;So here’s your challenge.&lt;/p&gt;

&lt;p&gt;It is easy to chain methods in code than to compose functions (we’re not using custom operators here).&lt;/p&gt;

&lt;p&gt;Here’s what chaining looks like.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello&quot;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;wordCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;inFahrenheit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;s&quot;&gt;&quot;Hello&quot;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;wordCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;increase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;How should we represent methods and method chaining pictorially? We could just use the function diagrams and include the implied &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self&lt;/code&gt; but that doesn’t feel satisfying.&lt;/p&gt;

&lt;p&gt;If you have an idea, please tweet with hashtag #drawingYOCmethods in the next couple of weeks.&lt;/p&gt;

&lt;p&gt;Thanks.&lt;/p&gt;</content><author><name></name></author><summary type="html">This month I’m inviting you to play along.</summary></entry><entry><title type="html">References</title><link href="https://www.drawingyourownconclusions.com/2020/03/02/references.html" rel="alternate" type="text/html" title="References" /><published>2020-03-02T00:00:00+00:00</published><updated>2020-03-02T00:00:00+00:00</updated><id>https://www.drawingyourownconclusions.com/2020/03/02/references</id><content type="html" xml:base="https://www.drawingyourownconclusions.com/2020/03/02/references.html">&lt;p&gt;My guest for this first episode is Mikey Ward, a great teacher, presenter, and author and one of the nicest people you’d ever want to meet.&lt;/p&gt;

&lt;p&gt;Mikey Ward is an instructor at Big Nerd Ranch, and you can find him on Twitter at @wookiee.&lt;/p&gt;

&lt;p&gt;He was incredibly kind and patient to be my first guest in this new venture.&lt;/p&gt;

&lt;p&gt;So Mikey, what programming concept are we talking about today?&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;em&gt;(Mikey)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I think with your permission I’d like to start with something near to my heart, as it’s something I see both experienced and new programmers struggle with. Object references, and how we use and misuse them.&lt;/p&gt;

&lt;p&gt;I suppose that could be an unfairly broad topic, since it really encompasses everything from value vs. reference semantics in Swift to strong reference cycle leaks, but it’s one where I think a little artful discussion could go a long way for folks.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;em&gt;(Daniel)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Great!&lt;/p&gt;

&lt;p&gt;Oh wait “artful discussion” - nice.&lt;/p&gt;

&lt;p&gt;So you and I are old enough that we remember Objective-C before ARC where he had to manage memory ourselves, in the snow, walking uphill back and forth to the compiler.&lt;/p&gt;

&lt;p&gt;In Objective-C if we created a new object with ownership, it’s reference count would be 1.&lt;/p&gt;

&lt;p&gt;Now if I’m interested in that object I would have to say how interested. Do I increase its reference count or not. Is my reference strong, weak, unsafe-unretained?&lt;/p&gt;

&lt;p&gt;I’m not nostalgic for the old days where we did this work ourself with retain, release, and autorelease, but those who just joined us for Swift where the reference count is handled for us may be missing some of the underlying ideas.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;em&gt;(Mikey)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Exactly! And I think some of the newer Swift developers are leaning on those with lots of experience to help them understand what’s going on, but I feel like there’s a lot of advice out there grounded in the pre-ARC way of thinking.&lt;/p&gt;

&lt;p&gt;I think updating our model for ARC’s and Swift’s optimizations is important.&lt;/p&gt;

&lt;p&gt;Which leads us to the the trailhead, I suppose. Swift’s Value and Reference types.&lt;/p&gt;

&lt;p&gt;There’s a lot that could be said about the stack and the heap but it’s easy to overthink, so let’s step back.&lt;/p&gt;

&lt;p&gt;The question is, “when I declare a variable and put a value in it, where in memory does the value actually live?”&lt;/p&gt;

&lt;p&gt;For value types such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt;, that value lives inside the variable itself. If I pass a the variable into a function as an argument, the variable (and its contained value) is copied.&lt;/p&gt;

&lt;p&gt;For reference types like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIViewController&lt;/code&gt;, the variable you create doesn’t keep the entire object inside it. Instead, the object is created somewhere else in memory, and your variable instead only contains knowledge of where in memory the real object is. Specifically, the object’s memory address.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/03/02/ValueVSReference.png&quot; alt=&quot;Value vs Reference&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;em&gt;(Daniel)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I like that you’re thinking conceptually of the difference between value and reference types.&lt;/p&gt;

&lt;p&gt;This split is clear in Objective-C and types like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIViewController&lt;/code&gt; that is implemented in Objective-C that we may create and interact with in Swift.&lt;/p&gt;

&lt;p&gt;But some of the Swift value types also end up using the heap as well. Our contract though is simple.&lt;/p&gt;

&lt;p&gt;How’s this for an example. I walk in and you’re watching your AppleTV. You are controlling it with your iPhone. In other words, you hold a pointer to the AppleTV.
&lt;img src=&quot;/assets/images/2020/03/02/TVRemote.png&quot; alt=&quot;TV Remote&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I sit down next to you and take out my iPhone and connect it to the AppleTV as well.&lt;/p&gt;

&lt;p&gt;We each have a reference to the same tv. 
If you pause the program using your remote, the program I’m watching pauses as well. If I hit the play button then the program you’re watching starts playing again.&lt;/p&gt;

&lt;p&gt;Our remotes are talking to the same AppleTV. We have references to the same memory location.&lt;/p&gt;

&lt;p&gt;Now, I go to the kitchen and make a sandwich. You say, “that looks good, I’d like one too.”&lt;/p&gt;

&lt;p&gt;This time it’s a physical object. I don’t want us connected to the same sandwich. We need our own instances.&lt;/p&gt;

&lt;p&gt;I make you a copy of my sandwich and hand it to you.&lt;/p&gt;

&lt;p&gt;You take a bite out of your sandwich.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/03/02/SharedSandwich.png&quot; alt=&quot;Shared Sandwich&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It would creep me out if that meant a bite disappeared from my sandwich. The sandwich is a value type. We each have our own. Changes I make to mine don’t effect yours and vice versa.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;em&gt;(Mikey)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Exactly! Thinking about the stack and the heap is likely to get in the way for most people. What’s more important is thinking about whether my variable contains the entire value (a value type) or just a reference to it (a reference type).&lt;/p&gt;

&lt;p&gt;Which simplifies the next step a bit: when I copy a variable of a value type, its entire contents are duplicated, as with “answer” and “fact” in my diagram, or the sandwiches in your example.&lt;/p&gt;

&lt;p&gt;But with a reference type, like the Apple TV, copying the remote just means I have two ways to control it.&lt;/p&gt;

&lt;p&gt;Copying a reference type variable only copies the reference. Which increases the number of references to the object in question.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/03/02/ReferenceTypes.png&quot; alt=&quot;Reference Types&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So the happy path is that an object stays alive as long as there are references to it. If the reference count of an object reaches zero, the object self-destructs and gives its memory back to the system. I don’t want my AppleTV to explode if both remotes are unpaired from it, but here we are, those are the rules.&lt;/p&gt;

&lt;p&gt;The Automatic Reference Counting done by the compiler, and the self-destruction of an object with a reference count of zero, means we rarely have to think about any of this.&lt;/p&gt;

&lt;p&gt;But there are strange and complex ways in which these seemingly simple rules can get us in trouble.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;em&gt;(Daniel)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For example, what happens if we have a value type that contains a property that’s a reference type.&lt;/p&gt;

&lt;p&gt;Going back to our AppleTV example, what if we don’t have iPhones with us - I know - that makes no sense, but work with me.&lt;/p&gt;

&lt;p&gt;So you have a really cool 3D printer that you use to print a remote for the AppleTV.&lt;/p&gt;

&lt;p&gt;I come into the room and see you watching television and using your remote and I ask you if I can have the remote.&lt;/p&gt;

&lt;p&gt;You think, “no, this is like the sandwich” and you make a copy of the remote and hand it to me. So it’s a different physical remote but it has this reference type inside - this property that, in your remote, points to the AppleTV.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/03/02/PrintedRemote.png&quot; alt=&quot;Reference and Value&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Will my remote point to the same AppleTV?&lt;/p&gt;

&lt;p&gt;If we have a value type that contains a reference type, where do these things live?&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;em&gt;(Mikey)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Your remote will absolutely point to the same AppleTV, and we’ll have a fun game of Menu Wars. When you copy a value type, you copy everything it contains. Remembering that the remote doesn’t contain the AppleTV itself (though that might be interesting to imagine), the remote only contains a reference to the AppleTV. When the remote is copied, any values and references it contains are copied. The objects at the other ends of those references are not.&lt;/p&gt;

&lt;p&gt;And now we get to the nut of it. Experienced developers might be tempted to apply terminology like “stack types” and “heap types” to Swift, but I strongly encourage folks not to try to force the comparison, and to embrace Swift’s terms of art, “value type” and “reference type”.&lt;/p&gt;

&lt;p&gt;Thinking about it strictly in those terms helps us to understand what’s happening under the hood of one of Swift’s more interesting design decisions.&lt;/p&gt;

&lt;p&gt;Let’s draw a chain of conclusions from what we know so far:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/03/02/Chain.png&quot; alt=&quot;Chain of Conclusions&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Reference type variables are very small, because they don’t contain any real data, they only contain a reference to somewhere else in memory where the data (which may be a mix of other references and values) actually lives.
This means that copying references and passing them into functions is inexpensive.
Value type variables contain all of their data (which may include a mix of other values and references)
This means that the computational and memory cost of copying and passing around value types is tied to the amount of data that they contain.&lt;/p&gt;

&lt;p&gt;So here’s the big question: since several of Swift’s value types like Strings and Arrays can become very big, do we need to be careful about how many times we pass them around and copy them? Would it be better if Strings and Arrays were reference types?&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;em&gt;(Daniel)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I agree that the right way to think about value types and reference types is their semantics, but to keep people from making assumptions about memory and performance it might be nice to spend a moment on stack and heap.&lt;/p&gt;

&lt;p&gt;A WWDC talk from 2016 titled &lt;a href=&quot;https://developer.apple.com/videos/play/wwdc2016/416/&quot;&gt;Understanding Swift Performance&lt;/a&gt; helped make this clear for me.&lt;/p&gt;

&lt;p&gt;In Objective-C and C based language, primitives are created on the stack while objects are created on the heap and their references are on the stack. (To be pedantic, the exception is blocks which are reference types created on the stack that need to be copied to the heap if they’re going to stay around a while.)&lt;/p&gt;

&lt;p&gt;Anyway, we explicitly do not have that clean separation in Swift. As you say, Strings and Arrays are value types and we should understand the contract that that implies. But we can’t reason that value types are kept on the stack and reference types on the heap.&lt;/p&gt;

&lt;p&gt;The session showed that Strings are created on the stack but for even fairly small Strings, the characters - i.e. the contents of the String - are stored on the heap. This mixture of getting the contract for value types while the efficiency of the heap is explained in Ben Cohen’s portion of the 2019 WWDC video &lt;a href=&quot;https://developer.apple.com/videos/play/wwdc2019/415/&quot;&gt;Modern Swift API Design&lt;/a&gt; and Johannes Weiss’ dotSwift talk &lt;a href=&quot;https://www.youtube.com/watch?v=iLDldae64xE&quot;&gt;High Performance systems in Swift&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, for me, the takeaway is that the library authors are aware of the costs of using value types while embracing the semantic benefits. It allows me to worry less about the performance and memory issues while adhering to your points on how we should think about value and reference types.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;em&gt;(Mikey)&lt;/em&gt;
Right! The videos’ points on the under-the-hood heap storage and copy-on-write features of some of Swift’s standard library types are fun and enlightening, but in the end, are just implementation details. In the end, even though it might feel wasteful to pass a large string or array up and down the stack, the Swift authors have gone to great lengths to make it work fast and efficiently.&lt;/p&gt;

&lt;p&gt;This really is a delightful language. Thank you for giving me the opportunity to chat with you about it!&lt;/p&gt;</content><author><name></name></author><summary type="html">My guest for this first episode is Mikey Ward, a great teacher, presenter, and author and one of the nicest people you’d ever want to meet.</summary></entry><entry><title type="html">The Plan</title><link href="https://www.drawingyourownconclusions.com/2020/01/10/the-plan.html" rel="alternate" type="text/html" title="The Plan" /><published>2020-01-10T00:00:00+00:00</published><updated>2020-01-10T00:00:00+00:00</updated><id>https://www.drawingyourownconclusions.com/2020/01/10/the-plan</id><content type="html" xml:base="https://www.drawingyourownconclusions.com/2020/01/10/the-plan.html">&lt;p&gt;There are times when I’m teaching, that code is not enough to explain what I’m trying to illustrate.&lt;/p&gt;

&lt;p&gt;It turns out that “illustrate” is exactly the word I’m looking for. I draw a picture.&lt;/p&gt;

&lt;p&gt;Now in my class it’s often true that the final sketch doesn’t mean much to anyone. It’s the discussion as I draw the picture.&lt;/p&gt;

&lt;p&gt;So that’s what I’m going to try to do here.&lt;/p&gt;

&lt;p&gt;I’m going to invite a guest to suggest a programming topic and we will have a conversation and try to draw sketches that might help. Then we’ll invite you to contribute your comments and sketches and then we’ll meet one more time to see where we are.&lt;/p&gt;

&lt;p&gt;At least that’s the plan for now. I’m sure as we start experimenting we’ll change our mind.&lt;/p&gt;

&lt;p&gt;So say we’ve developed pictures of functions and drawn a function machine where the input comes in the top left and the output comes out the bottom right.&lt;/p&gt;

&lt;p&gt;Then composition might look like this.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/01/10/ComposePre.png&quot; alt=&quot;Composing f and g&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And then maybe we’d talk about how this is equivalent to a single function that essentially wraps the inner workings.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020/01/10/Compose.png&quot; alt=&quot;The Composition&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Anyway, that’s the rough idea. We’ll see how it goes.&lt;/p&gt;</content><author><name></name></author><summary type="html">There are times when I’m teaching, that code is not enough to explain what I’m trying to illustrate.</summary></entry></feed>