<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>journal.stuffwithstuff.com</title>
  <link href="http://journal.stuffwithstuff.com/"/>
  <link type="application/atom+xml" rel="self" href="http://journal.stuffwithstuff.com/atom.xml"/>
  <updated>2013-04-23T23:40:00-07:00</updated>
  <id>http://journal.stuffwithstuff.com/</id>
  <author>
    <name>Robert Nystrom</name>
    <email>robert@stuffwithstuff.com</email>
  </author>

  
  <entry>
    <id>http://journal.stuffwithstuff.com/2013/04/23/playing-with-generics-in-typescript-0.9.0</id>
    <link type="text/html" rel="alternate" href="http://journal.stuffwithstuff.com/2013/04/23/playing-with-generics-in-typescript-0.9.0"/>
    <title>Playing with Generics in TypeScript 0.9.0</title>
    <published>2013-04-23T00:00:00-07:00</published>
    <updated>2013-04-23T00:00:00-07:00</updated>
    <author>
      <name>Robert Nystrom</name>
      <uri>http://journal.stuffwithstuff.com/</uri>
    </author>
    <content type="html">&lt;p&gt;This was just going to be a comment on &lt;a href=&quot;http://www.reddit.com/r/programming/comments/1cyij4/typescript_09_early_previews_with_support_for/&quot;&gt;this reddit thread&lt;/a&gt;, but then it seemed to take on a life of its own, so I figured I may as well &lt;s&gt;milk it for all it's worth&lt;/s&gt; make a nice post out of it.&lt;/p&gt;

&lt;p&gt;Yesterday, the TypeScript guys &lt;a href=&quot;http://blogs.msdn.com/b/typescript/archive/2013/04/22/announcing-0-9-early-previews.aspx&quot;&gt;announced a preview of the new 0.9.0 version&lt;/a&gt; of the language featuring generics. I, like a lot of people, was really curious to see what approach they&amp;#8217;d take with them. Retrofitting a type system onto a dynamic language is &lt;em&gt;hard&lt;/em&gt; and generics are one of the places where that new suit of armor can really chafe the squishy flesh underneath.&lt;/p&gt;

&lt;p&gt;This is a topic strangely near to my heart for another reason too. I&amp;#8217;m on the &lt;a href=&quot;http://www.dartlang.org/&quot;&gt;Dart team&lt;/a&gt; and when Dart was announced, we were widely criticized for its type system. Generics are covariant in Dart, which is a mortal sin to many.&lt;/p&gt;

&lt;p&gt;Now when any new type system comes out, my first thought is, &amp;#8220;I wonder if it&amp;#8217;s got covariant generics?&amp;#8221; (My second thought is, &amp;#8220;God, I need a hobby that doesn&amp;#8217;t involve programming languages.&amp;#8221;)&lt;/p&gt;

&lt;p&gt;Some more caveats before I get going:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;I literally spent five minutes poking at this, so I may have things wrong.&lt;/li&gt;
  &lt;li&gt;Compiling programs with a compiler that isn&amp;#8217;t 1.0 yet and claiming that says something about the language specification is asking for trouble.&lt;/li&gt;
  &lt;li&gt;I, despite my current occupation, am surprisingly bad at reading language specs.&lt;/li&gt;
  &lt;li&gt;I am currently drinking a &lt;a href=&quot;http://www.ratebeer.com/beer/elysian-bete-blanche-belgian-tripel-2011-and-later/138973/&quot;&gt;fairly strong beer&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;OK, party time! After playing around with it a bit, as far as I can tell, &lt;em&gt;TypeScript&amp;#8217;s subtype relations are more permissive than I expected&lt;/em&gt;. This isn&amp;#8217;t necessarily bad, just surprising. As a preamble, let&amp;#8217;s define a supertype and subtype:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Base&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Derived&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now consider:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&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;constructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;value:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&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;p&gt;This is the simplest possible generic type.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Base&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;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Derived&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;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;These are both fine, as you would expect.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&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;s2&quot;&gt;&amp;quot;not num&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This gives:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/generics.ts(5,0): error TS2081: Supplied parameters do not match any signature of call target.
/generics.ts(5,0): error TS2085: Could not select overload for 'new' expression.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Looks about right. Now let&amp;#8217;s try covariance:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Derived&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;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;No errors. Contravariance?&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Base&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;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Still no errors. This, I think, makes its type system looser than arrays in Java, and more permissive than Dart. Generics are &lt;em&gt;bi&lt;/em&gt;variant in TypeScript.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&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;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As a sanity check, this &lt;em&gt;does&lt;/em&gt; give an error.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/generics.ts(73,4): error TS2012: Cannot convert 'Box&amp;lt;string&amp;gt;' to 'Box&amp;lt;number&amp;gt;':
Types of property 'value' of types 'Box&amp;lt;string&amp;gt;' and 'Box&amp;lt;number&amp;gt;' are incompatible.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So it doesn&amp;#8217;t just &lt;em&gt;ignore&lt;/em&gt; the type parameters, it really is bivariant: it will allow either a subtype or supertype relation for the type parameters, but not no relation at all.&lt;/p&gt;

&lt;p&gt;Part of this may be because TypeScript&amp;#8217;s type system is structural (neat!). For example:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Base&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;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Derived&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;p&gt;Here we have two unrelated types that happen to have the same shape (method names and signatures). Perhaps surprisingly, there&amp;#8217;s no error here:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This is perhaps extra surprising because &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; don&amp;#8217;t have the &lt;em&gt;exact&lt;/em&gt; same signatures: their parameter types for &lt;code&gt;arg&lt;/code&gt; are different. So the type system is both structural and allows either supertype or subtypes on parameters.&lt;/p&gt;

&lt;p&gt;If the type system is structural, maybe the &lt;code&gt;Box&amp;lt;T&amp;gt;&lt;/code&gt; examples only worked then because it had no methods (aside from the &lt;code&gt;value&lt;/code&gt; property) that used the type parameter. What if we make sure &lt;code&gt;T&lt;/code&gt; shows up in parameter and return positions?&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&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;constructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;value:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&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;n&quot;&gt;takeParam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;arg:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&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;n&quot;&gt;returnType&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;T&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&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;p&gt;Nope, this makes no difference. Still same behavior as before.&lt;/p&gt;

&lt;p&gt;I believe the relevant bits of the spec are:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;3.6.2.1 Type Arguments&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;A type reference to a generic type G designates a type wherein all
occurrences of G’s type parameters have been replaced with the actual type
arguments supplied in the type reference.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I think this basically says that generics are structurally typed and the type relation is determined based on the &lt;em&gt;expanded&lt;/em&gt; type where type arguments have been applied. In other words, generic types don&amp;#8217;t have type relations, just generic type &lt;em&gt;applications&lt;/em&gt;. For example:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Generic&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&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;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;arg:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&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;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;NotGeneric&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;arg:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&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;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NotGeneric&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Generic&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&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;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This is fine in TypeScript unlike most nominally-typed languages. Pretty neat!&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;3.8.2 Subtypes and Supertypes&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;S is a subtype of a type T, and T is a supertype of S, if one of the
following is true:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;
      &lt;p&gt;S’ and T are object types and, for each member M in T, one of the
following is true:&lt;/p&gt;

      &lt;ul&gt;
        &lt;li&gt;
          &lt;p&gt;M is a non-specialized call or construct signature and S’ contains a
call or construct signature N where, when substituting ‘Object’ for
all type parameters declared by M and N (if any),&lt;/p&gt;

          &lt;ul&gt;
            &lt;li&gt;
              &lt;p&gt;for parameter positions that are present in both signatures,
&lt;strong&gt;each parameter type in N is a subtype or supertype of the corresponding parameter type in M&lt;/strong&gt;,&lt;/p&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;p&gt;the result type of M is Void, or the result type of N is a
subtype of that of M&lt;/p&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;p&gt;&amp;#8230;&lt;/p&gt;
            &lt;/li&gt;
          &lt;/ul&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;&amp;#8230;&lt;/p&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;p&gt;&amp;#8230;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;My emphasis added. I believe this basically says that to compare two types, you walk their members and compare their types. For method parameters, &amp;#8220;subtype or supertype&amp;#8221; means bivariance: types go both ways. This is looser than the normal function typing rule which is contravariance for parameters and covariance for returns.&lt;/p&gt;

&lt;p&gt;All in all, I find this pretty interesting. TypeScript has both a structural and prototypal type system, so it&amp;#8217;s already pretty fascinating from a language design perspective. Allowing bivariance of parameter types is a pretty bold extension of that.&lt;/p&gt;

&lt;p&gt;Oh, if you&amp;#8217;d like to see this for yourself, here&amp;#8217;s how:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git clone https://git01.codeplex.com/typescript
$ cd typescript
$ git checkout develop
$ jake local
$ chmod +x bin/tsc
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then you can run &lt;code&gt;bin/tsc&lt;/code&gt; to compile stuff with the bleeding edge compiler. The latest spec is under &lt;code&gt;doc/TypeScript Language Specification.pdf&lt;/code&gt;.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <id>http://journal.stuffwithstuff.com/2013/04/17/well-done</id>
    <link type="text/html" rel="alternate" href="http://journal.stuffwithstuff.com/2013/04/17/well-done"/>
    <title>Well Done: A Sentinel Value</title>
    <published>2013-04-17T00:00:00-07:00</published>
    <updated>2013-04-17T00:00:00-07:00</updated>
    <author>
      <name>Robert Nystrom</name>
      <uri>http://journal.stuffwithstuff.com/</uri>
    </author>
    <content type="html">&lt;p&gt;&lt;em&gt;This is kinda like part three of the Iteration Inside and Out posts, so you
may want to check out &lt;a href=&quot;/2013/01/13/iteration-inside-and-out/&quot;&gt;part one&lt;/a&gt; and &lt;a href=&quot;/2013/02/24/iteration-inside-and-out-part-2/&quot;&gt;part two&lt;/a&gt; too. But you don&amp;#8217;t have to.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most programming languages have one or two singleton values floating around.
These are special built-in objects that only have a single instance. &lt;code&gt;null&lt;/code&gt; or
&lt;code&gt;nil&lt;/code&gt; are common. Some languages put &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; in the same bucket.
JavaScript, ever the hipster, ironically defines one called
&lt;a href=&quot;http://javascriptweblog.wordpress.com/2010/08/16/understanding-undefined-and-preventing-referenceerrors/&quot;&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I wanted to talk a little bit about one I just added to my little language
&lt;a href=&quot;http://magpie-lang.org/&quot;&gt;Magpie&lt;/a&gt;: a &lt;a href=&quot;http://en.wikipedia.org/wiki/Sentinel_value&quot;&gt;sentinel value&lt;/a&gt; called &lt;code&gt;done&lt;/code&gt;. I&amp;#8217;m still not certain it&amp;#8217;s a great
idea, but I&amp;#8217;ll walk you through my thoughts. If nothing else, maybe it will
serve as a cautionary tale for other, smarter language designers.&lt;/p&gt;

&lt;h2 id=&quot;channels&quot;&gt;Channels&lt;/h2&gt;

&lt;p&gt;I ran into the problem that ultimately led to &lt;code&gt;done&lt;/code&gt; when I started working on
Magpie&amp;#8217;s concurrency story. Its model is based on &lt;a href=&quot;http://en.wikipedia.org/wiki/Green_threads&quot;&gt;fibers&lt;/a&gt; and &lt;a href=&quot;http://golang.org/doc/effective_go.html#channels&quot;&gt;channels&lt;/a&gt;,
much like &lt;a href=&quot;http://golang.org/&quot;&gt;Go&lt;/a&gt; or other &lt;a href=&quot;http://en.wikipedia.org/wiki/Communicating_sequential_processes&quot;&gt;CSP&lt;/a&gt;-inspired languages. You can spin up a new
fiber using the &lt;code&gt;async&lt;/code&gt; keyword:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;I&amp;#39;m in another fiber!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Fibers are cooperatively scheduled so they only yield to other fibers when they
do something that requires waiting. Usually that means IO. So if you do the
above, you won&amp;#8217;t see that message get printed until something causes the main
fiber to yield. For example:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;I&amp;#39;m in another fiber!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Before&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;After&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This program will create a second fiber (but not switch to it). Then it gets to
&lt;code&gt;print(&quot;Before&quot;)&lt;/code&gt; in the main fiber. Since printing is IO, that switches to the
second fiber. That in turn queues up &lt;em&gt;its&lt;/em&gt; &lt;code&gt;print&lt;/code&gt; and suspends again, so the
main fiber resumes. Ultimately, it prints:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Before
I'm in another fiber!
After
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can spin up lots and lots of fibers because they don&amp;#8217;t use OS threads, just
some memory in the interpreter. This is swell for decoupling stuff and running
things concurrently. But sometimes you do need to coordinate fibers with each
other. For that, we&amp;#8217;ve got channels.&lt;/p&gt;

&lt;p&gt;A channel is a simple object that you can send objects into and then receive
them from another channel. You can create one like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To send a value along it, just do:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;My value&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And you can receive it like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;receive&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The fun bit is that when a fiber sends a value along a channel, it causes that
fiber to suspend until some &lt;em&gt;other&lt;/em&gt; fiber shows up to receive the value. A send
doesn&amp;#8217;t complete until the object has been received. So channels let you not
just communicate but also &lt;em&gt;synchronize.&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;shows-over-folks&quot;&gt;Show&amp;#8217;s Over Folks&lt;/h2&gt;

&lt;p&gt;There&amp;#8217;s one other thing you can do with a channel, you can close it:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That puts the channel out of commission and tells everyone that they will
receive no more values from it. The question I ran into was, &amp;#8220;what happens to
fibers that are waiting to receive on a channel when it gets closed?&amp;#8221; For
example:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Wait for a value and print it.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;receive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// From the main fiber, close the channel.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Here, the second fiber is sitting there, relaxing, maybe having a cocktail while
it waits for the channel to spew something forth. But it doesn&amp;#8217;t, it gets
closed. The fiber shouldn&amp;#8217;t just stay suspended &lt;em&gt;forever&lt;/em&gt;, so how should it get
notified?&lt;/p&gt;

&lt;p&gt;One option would be have &lt;code&gt;receive&lt;/code&gt; &lt;a href=&quot;http://magpie-lang.org/error-handling.html&quot;&gt;throw an error&lt;/a&gt; when the channel gets
closed. So you&amp;#8217;d handle it something like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;receive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ChannelClosedError&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Sorry, no value for you&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Actually, any &lt;a href=&quot;http://magpie-lang.org/blocks.html&quot;&gt;block&lt;/a&gt; can have a &lt;code&gt;catch&lt;/code&gt; clause in Magpie, so the explicit
&lt;code&gt;do&lt;/code&gt; here is redundant. You&amp;#8217;d really just do:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;receive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ChannelClosedError&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Sorry, no value for you&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That&amp;#8217;s not too bad, but consider something a little more fleshed out. In many
cases, you&amp;#8217;ll receive something from a channel and do different stuff based on
what you get. Channels are often used for &lt;em&gt;messages&lt;/em&gt; and the specific message
will often cause different behavior. You can imagine something like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&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;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;receive&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;inc&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&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;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;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;dec&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&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;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;k&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Here we&amp;#8217;ve got a little concurrent counter that you can send messages to in
order to increment and decrement the value. If it also needs to handle the
channel closing, you&amp;#8217;d need to do:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&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;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;receive&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;inc&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&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;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;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;dec&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&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;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;k&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ChannelClosedError&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Closed&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That feels redundant to me. Catching errors is basically &lt;a href=&quot;http://magpie-lang.org/patterns.html&quot;&gt;pattern-matching&lt;/a&gt;
(in fact, in Magpie it uses the exact same syntax and semantics as a
&lt;a href=&quot;http://magpie-lang.org/pattern-matching.html&quot;&gt;&lt;code&gt;match&lt;/code&gt;&lt;/a&gt; expression), so it feels dumb to have to have &lt;em&gt;two&lt;/em&gt; matches,
one for the set of normal values and then this extra &lt;code&gt;catch&lt;/code&gt; for the one special
&amp;#8220;no more values&amp;#8221; signal.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s not just redundant either. Even if you don&amp;#8217;t care about the channel
closing, you &lt;em&gt;have&lt;/em&gt; to add the &lt;code&gt;catch&lt;/code&gt; here. Otherwise, since it&amp;#8217;s a &lt;em&gt;thrown&lt;/em&gt;
error, it will unwind the fiber&amp;#8217;s callstack on you.&lt;/p&gt;

&lt;p&gt;Both of those are no fun, so I figured, why not just make &lt;code&gt;receive&lt;/code&gt; &lt;em&gt;return&lt;/em&gt; a
value when the channel is closed instead of &lt;em&gt;throwing&lt;/em&gt; one. Magpie already has
a &lt;code&gt;null&lt;/code&gt;-like sentinel value called &lt;a href=&quot;http://magpie-lang.org/primitives.html#nothing&quot;&gt;&lt;code&gt;nothing&lt;/code&gt;&lt;/a&gt;, so I could just say
&lt;code&gt;receive&lt;/code&gt; returns &lt;code&gt;nothing&lt;/code&gt; when the channel is closed.&lt;/p&gt;

&lt;p&gt;But that&amp;#8217;s a bit lame. While &lt;code&gt;nothing&lt;/code&gt; doesn&amp;#8217;t appear as often as &lt;code&gt;null&lt;/code&gt; does
in other languages, it&amp;#8217;s still a general-purpose &amp;#8220;no value here&amp;#8221; object that
users need to be free to use anywhere. If &lt;code&gt;receive&lt;/code&gt; returns &lt;code&gt;nothing&lt;/code&gt; when the
channel is closed, there&amp;#8217;d be no way to explicitly send &lt;code&gt;nothing&lt;/code&gt; along an open
channel. Receivers wouldn&amp;#8217;t be able to tell the difference.&lt;/p&gt;

&lt;h2 id=&quot;are-we-done&quot;&gt;Are We Done?&lt;/h2&gt;

&lt;p&gt;So I made another singleton value: &lt;code&gt;done&lt;/code&gt;. It exists solely to represent &amp;#8220;no
more values&amp;#8221;. With this, the above code is like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&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;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;receive&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;inc&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&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;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;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;dec&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&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;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;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Closed&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;It may seem weird to have a method return values of different types, but this
is natural in Magpie. The language is dynamically typed, and revolves heavily
around pattern-matching. In, say, JavaScript, if you have a function that can
return a number or a string and callers will want to handle those cases
differently you get this ugly code:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;number&amp;#39;&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;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;number&amp;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;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;string&amp;#39;&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;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;string&amp;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;p&gt;In Magpie, that&amp;#8217;s:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Num&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;number&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;string&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Or, since the language is expression-oriented, you could even do:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Num&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;number&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;string&amp;quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In fact, because Magpie also has &lt;a href=&quot;http://magpie-lang.org/multimethods.html&quot;&gt;multimethods&lt;/a&gt;, even this would do the right
thing:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;showType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;number&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;showType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;string&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;showType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Where was I? Oh, right. So it&amp;#8217;s totally kosher to have methods return values of
different types and have callers dispatch on them. That was enough to talk
myself into being OK with &lt;code&gt;receive&lt;/code&gt; having a sort of ad-hoc variant-like return
value.&lt;/p&gt;

&lt;h2 id=&quot;the-next-iteration&quot;&gt;The Next Iteration&lt;/h2&gt;

&lt;p&gt;Once I had that, I started fleshing out channels a bit more. One thing I noticed
is that you often read from a channel repeatedly. See that &lt;code&gt;while true do&lt;/code&gt; loop
up there? That seemed a bit silly. Magpie has &lt;code&gt;for&lt;/code&gt; loops. Like &lt;a href=&quot;http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html&quot;&gt;most&lt;/a&gt;
&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/78dfe2yb.aspx&quot;&gt;languages&lt;/a&gt;, those are syntactic sugar for an
&lt;a href=&quot;http://docs.python.org/2/c-api/iter.html&quot;&gt;iteration protocol&lt;/a&gt;. If I made channels support that directly, you could do:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&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;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;receive&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;inc&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&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;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;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;dec&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&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;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;k&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Closed&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This protocol has two levels: iter&lt;em&gt;able&lt;/em&gt; and iter&lt;em&gt;ator&lt;/em&gt;. The iterable protocol
is for an object that can be iterated: lists, collections, etc. There&amp;#8217;s just one
method:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Iterable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;An iterable type specializes &lt;code&gt;iterate&lt;/code&gt; to return a new iterator for the object.
This lets you iterate over objects without modifying the original object. It
would be pretty weird if you could only use a list in one loop at a time.&lt;/p&gt;

&lt;p&gt;In the case of channels, though, iterating it &lt;em&gt;is&lt;/em&gt; mutating the original object,
so &lt;code&gt;iterate&lt;/code&gt; on a channel just returns the same object. Channels are both
iterables &lt;em&gt;and&lt;/em&gt; iterators. The iterat&lt;em&gt;or&lt;/em&gt; protocol was two methods:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Iterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;advance&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This tries to advance the iterator to the next item and returns &lt;code&gt;true&lt;/code&gt; if it
found another item.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Iterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This returns the current item. So there was a two method protocol here. Most
other languages have something similar. You need two methods because one crank
of the iterator needs to return two bits of data: whether or not you&amp;#8217;re done
iterating, and the next value if you aren&amp;#8217;t.&lt;/p&gt;

&lt;p&gt;I say &amp;#8220;&lt;em&gt;was&lt;/em&gt; a two method protocol&amp;#8221; because now that I had this &lt;code&gt;done&lt;/code&gt; value, I
realized I could simplify it. All I need is &lt;code&gt;advance&lt;/code&gt;. It returns the next item
if there is one, or &lt;code&gt;done&lt;/code&gt; if there isn&amp;#8217;t. The only thing you lose is the
ability to iterate over a collection that actually contains &lt;code&gt;done&lt;/code&gt; as a value.
But I&amp;#8217;m OK with that. That seems like a small thing to give up to get a simpler
protocol.&lt;/p&gt;

&lt;p&gt;Now, not only can you use channels any place you work with sequences, but even
implementing your own sequences is a little easier.&lt;/p&gt;

&lt;h2 id=&quot;your-favorite-functional-friends&quot;&gt;Your Favorite Functional Friends&lt;/h2&gt;

&lt;p&gt;When I say &amp;#8220;any place you work with sequences&amp;#8221; much of what I&amp;#8217;m thinking of is
those workhorse methods that transform sequences. Good old &lt;a href=&quot;http://en.wikipedia.org/wiki/Map_(higher-order_function)&quot;&gt;&lt;code&gt;map&lt;/code&gt;&lt;/a&gt;,
&lt;a href=&quot;http://en.wikipedia.org/wiki/Filter_(higher-order_function)&quot;&gt;&lt;code&gt;filter&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;http://en.wikipedia.org/wiki/Fold_(higher-order_function)&quot;&gt;&lt;code&gt;fold&lt;/code&gt;&lt;/a&gt;, etc. This gives you a feel for what they
look like in Magpie:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&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;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;list&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;k&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 3, 6, 9, 12&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&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;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 2, 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Since I specialized these on Iterable, that means any iterable type, including
channels, gets them. For a longer example, Go has &lt;a href=&quot;http://golang.org/doc/play/sieve.go&quot;&gt;an example&lt;/a&gt;
showing a concurrent &lt;a href=&quot;http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes&quot;&gt;Sieve of Eratosthenes&lt;/a&gt;. Ported to Magpie, it looks like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// Send the sequence 2, 3, 4, ... to &amp;#39;channel&amp;#39;.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Channel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9999&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Copy the values from &amp;#39;input&amp;#39; to &amp;#39;out&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// removing those divisible by &amp;#39;prime&amp;#39;.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Channel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Channel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prime&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&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;k&quot;&gt;async&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prime&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;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pipeTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// The prime sieve: Daisy-chain Filter processes.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Spawn generate fiber.&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;receive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sieve&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sieve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sieve&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I won&amp;#8217;t walk you through the whole thing, but I do think it&amp;#8217;s pretty rad. The
neat bit is this line:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prime&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;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pipeTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&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;p&gt;Here, &lt;code&gt;where&lt;/code&gt; is Magpie&amp;#8217;s filter function. It takes an iterable on the left
and a predicate function on the right. It returns an iterable generating all of
the items of the original sequence where the predicate returns &lt;code&gt;true&lt;/code&gt;. Note that
here, the argument on the left is a &lt;em&gt;channel&lt;/em&gt; so this magically becomes an
&lt;em&gt;asynchronous&lt;/em&gt; filter.&lt;/p&gt;

&lt;p&gt;Likewise, &lt;code&gt;pipeTo&lt;/code&gt; is a method that takes an iterable and a channel and sends
all of the values in the iterable to the channel. So we&amp;#8217;re taking a channel,
filtering it like a collection, then pumping that collection back into a new
channel. This all happens asynchronously, but the code looks like you&amp;#8217;re just
playing with regular collections.&lt;/p&gt;

&lt;p&gt;Crap, I&amp;#8217;ve digressed again. I was supposed to be talking about &lt;code&gt;done&lt;/code&gt;. I just
think treating channels as sequences is really cool. OK, back to the matter at
hand.&lt;/p&gt;

&lt;h2 id=&quot;now-are-we-done-yet&quot;&gt;Now Are We Done Yet?&lt;/h2&gt;

&lt;p&gt;The simplest of the higher-order methods for working with iterables is &lt;code&gt;each&lt;/code&gt;.
All it does is call the given function for each item in the sequence. In other
languages this is sometimes called &lt;code&gt;forEach&lt;/code&gt;. Most of the time you don&amp;#8217;t
need it since it is almost identical to just using a built-in &lt;code&gt;for&lt;/code&gt; loop. But
sometimes it&amp;#8217;s convenient, so Magpie has it.&lt;/p&gt;

&lt;p&gt;If you&amp;#8217;ve worked with &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach&quot;&gt;&lt;code&gt;forEach&lt;/code&gt;&lt;/a&gt; in JavaScript, one nasty problem is
how do you stop early if you don&amp;#8217;t actually need to walk the whole sequence?
Ruby has some special syntax built into the language, &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;next&lt;/code&gt;, that
are designed to work specifically with blocks and iterators. JS ain&amp;#8217;t so lucky.&lt;/p&gt;

&lt;p&gt;Magpie has &lt;code&gt;break&lt;/code&gt; for &lt;code&gt;for&lt;/code&gt; loops, but a method like &lt;code&gt;each&lt;/code&gt; is just a method
that takes a function. You can&amp;#8217;t &lt;code&gt;break&lt;/code&gt; from within a function. But&amp;#8230; since
we have this &lt;code&gt;done&lt;/code&gt; value laying around, we can use it here too. So the way
&lt;code&gt;each&lt;/code&gt; (and &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;where&lt;/code&gt; and others) work is if the callback ever returns
&lt;code&gt;done&lt;/code&gt; that means &amp;#8220;stop iterating now&amp;#8221;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;p&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;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&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;k&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This will not only transform the values (i.e. what &lt;code&gt;map&lt;/code&gt; usually does), but it
will also truncate the sequence and just return &lt;code&gt;[2, 4]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So we&amp;#8217;ve got an easier way to handle closed channels, a simpler iterator
protocol, and more expressive collection-manipulation methods. Not too bad for
one little sentinel value.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <id>http://journal.stuffwithstuff.com/2013/02/24/iteration-inside-and-out-part-2</id>
    <link type="text/html" rel="alternate" href="http://journal.stuffwithstuff.com/2013/02/24/iteration-inside-and-out-part-2"/>
    <title>Iteration Inside and Out, Part 2</title>
    <published>2013-02-24T00:00:00-08:00</published>
    <updated>2013-02-24T00:00:00-08:00</updated>
    <author>
      <name>Robert Nystrom</name>
      <uri>http://journal.stuffwithstuff.com/</uri>
    </author>
    <content type="html">&lt;p&gt;&lt;em&gt;You&amp;#8217;ll probably want to have read &lt;a href=&quot;/2013/01/13/iteration-inside-and-out/&quot;&gt;part one&lt;/a&gt; first unless you&amp;#8217;re feeling brave.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In our last episode, we learned that iteration involves two chunks of code: one generating values, and one consuming them. During a loop, these two chunks take turns in a cycle of generate, consume, generate, consume, like some sort of weird incremental Ouroboros. This means that one chunk has to fully return and unwind its stack frames before it can hand off to the next one.&lt;/p&gt;

&lt;p&gt;We also learned that external and internal iterators each nail some problems and totally fail at others. The discrepency boils down to which chunk of code has more useful stuff to store on the callstack.&lt;/p&gt;

&lt;p&gt;With external iterators, the code &lt;em&gt;consuming&lt;/em&gt; values has control over the stack, so it works well with problems where most complexity is in consuming values. For example, short-circuiting or interleaving multiple iterators is trivial in an external iterator.&lt;/p&gt;

&lt;p&gt;Conversely, internal iterators put the code &lt;em&gt;generating&lt;/em&gt; values in control. They excel when generating values is complicated, like walking a tree and iterating over the nodes.&lt;/p&gt;

&lt;p&gt;Now we&amp;#8217;ll see some techniques to deal with this. The basic idea is &lt;a href=&quot;http://en.wikipedia.org/wiki/Reification_(computer_science)&quot;&gt;&lt;em&gt;reification&lt;/em&gt;&lt;/a&gt;. If you&amp;#8217;ve got some data on the callstack that you want to hang onto, you need to find a place to store it.&lt;/p&gt;

&lt;h2 id=&quot;iterators-and-generators&quot;&gt;Iterators and generators&lt;/h2&gt;

&lt;p&gt;Say we want to define a method that concatenates two sequences. We don&amp;#8217;t want to actually create a data structure that contains the elements of both, we just want to return an iterator that walks the first sequence and then the second one. Here&amp;#8217;s how you could do that in C#:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ConcatEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&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;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ConcatEnumerable&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ConcatEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&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;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&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;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetEnumerator&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ConcatEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetEnumerator&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;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ConcatEnumerator&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IEnumerator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IEnumerator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;onFirst&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ConcatEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&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;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&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;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MoveNext&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;c1&quot;&gt;// Which sequence are we on?&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;onFirst&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;c1&quot;&gt;// Stay on first.&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MoveNext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;// Move to the next sequence.&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;onFirst&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MoveNext&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;c1&quot;&gt;// On second.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MoveNext&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;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Current&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;onFirst&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Current&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Current&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;p&gt;Oof, that seems like a pile of code for such a simple goal. The problem is that we&amp;#8217;ve got all of this state to maintain: the two sequences being iterated, which one we&amp;#8217;re in, and where we are in it. Since this is an &lt;em&gt;external&lt;/em&gt; iterator, we can&amp;#8217;t just store that as local variables on the stack because we have to return from &lt;code&gt;MoveNext()&lt;/code&gt; between each item.&lt;/p&gt;

&lt;p&gt;But but but! C# has something called &lt;em&gt;iterators&lt;/em&gt;. (A confusing name. What other languages call &amp;#8220;iterators&amp;#8221;, C# calls &amp;#8220;enumerators&amp;#8221;. So &amp;#8220;iterator&amp;#8221; means something special in C#-land.) The above code can also be written:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&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;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&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;p&gt;How&amp;#8217;s that for an improvement? (Note: if your employer pays you by the line, you&amp;#8217;ll want to avoid this.) The magic here is &lt;code&gt;yield return&lt;/code&gt;. When a method contains it, the compiler turns the method into an &lt;em&gt;iterator&lt;/em&gt;. You can think of it sort of as a &amp;#8220;resumable method&amp;#8221;. When you call &lt;code&gt;Concat()&lt;/code&gt;, it runs to the first &lt;code&gt;yield&lt;/code&gt;, then stops. Then when you resume it, it picks up where it left after the &lt;code&gt;yield&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So here it iterates through the first sequence, stopping at each item and returning it. Then it does the same thing with the second sequence. But what does it mean to &amp;#8220;resume&amp;#8221; a method?&lt;/p&gt;

&lt;p&gt;The return type here clarifies that. When you call &lt;code&gt;Concat()&lt;/code&gt;, what you get back is an &lt;code&gt;IEnumerable&lt;/code&gt;. This is C#&amp;#8217;s &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx&quot;&gt;iterable sequence type&lt;/a&gt;. So what you get back is a &amp;#8220;collection&amp;#8221;. &amp;#8220;Resuming the method&amp;#8221; just means &amp;#8220;get the next value in the sequence&amp;#8221;.&lt;/p&gt;

&lt;p&gt;So, given the above, we&amp;#8217;ve got a nice solution to our original problem. We can use it like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stuff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moreStuff&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;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&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;p&gt;Using &lt;code&gt;yield&lt;/code&gt; lets us store all of the interesting state&amp;mdash;the two sequences and our current location in them&amp;mdash; just as local variables right in the &lt;code&gt;Concat()&lt;/code&gt; method. C# will &lt;em&gt;reify&lt;/em&gt; that stuff for us so that when the &lt;code&gt;Concat()&lt;/code&gt; method &amp;#8220;returns&amp;#8221;, that data gets squirreled away somewhere safe.&lt;/p&gt;

&lt;p&gt;You may wonder how it does that. That&amp;#8217;s kind of the funny bit. In the case of C#, the answer is that the compiler itself will &lt;a href=&quot;http://startbigthinksmall.wordpress.com/2008/06/09/behind-the-scenes-of-the-c-yield-keyword/&quot;&gt;automatically generate a little hidden class&lt;/a&gt; exactly like our &lt;code&gt;ConcatEnumerator&lt;/code&gt; one up there. The actual runtime (the &lt;a href=&quot;http://en.wikipedia.org/wiki/Common_Language_Runtime&quot;&gt;CLR&lt;/a&gt;) doesn&amp;#8217;t have any support for &lt;code&gt;yield&lt;/code&gt;. It&amp;#8217;s done purely by automatically generating code. It&amp;#8217;s large delicious lump of &lt;a href=&quot;http://en.wikipedia.org/wiki/Syntactic_sugar&quot;&gt;syntactic sugar&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the last post, I crafted some glorious ASCII art showing where all of the state is being stored. The main problem was that the state for the code generating values and the state for the code consuming them both live on the stack. Using an iterator, though, gives you this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;text&quot;&gt;  stack                       heap
+---------------------+
| iterator.MoveNext() |
+---------------------+     +-------------------+
| loop body           | --&amp;gt; | DesugaredIterator |
+---------------------+     +-------------------+
  ...

  main()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So the stack has the state for the code consuming values. But the state needed to generate values lives on the &lt;em&gt;heap&lt;/em&gt;. There&amp;#8217;s an instance of this little class that the compiler created for us. When &lt;code&gt;MoveNext()&lt;/code&gt; returns, we don&amp;#8217;t trash the state because it&amp;#8217;s still over there in the heap available the next time we call &lt;code&gt;MoveNext()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are a few other languages that do (or will) work this way. Python calls these &amp;#8220;&lt;a href=&quot;http://www.python.org/dev/peps/pep-0255/&quot;&gt;generators&lt;/a&gt;&amp;#8221; and uses a similar &lt;code&gt;yield&lt;/code&gt; statement. The next version of JavaScript will have &lt;a href=&quot;http://wiki.ecmascript.org/doku.php?id=harmony:generators&quot;&gt;something similar&lt;/a&gt;. The language that invented generators and the &lt;code&gt;yield&lt;/code&gt; keyword was Barbara Liskov&amp;#8217;s &lt;a href=&quot;http://en.wikipedia.org/wiki/CLU_(programming_language)&quot;&gt;CLU&lt;/a&gt;, immortalized forever in &lt;a href=&quot;http://en.wikipedia.org/wiki/List_of_Tron_characters#CLU&quot;&gt;Tron&lt;/a&gt;. (I&amp;#8217;m not making that up.)&lt;/p&gt;

&lt;p&gt;There&amp;#8217;s a limitation here, though. You can only yield from the method itself. Let&amp;#8217;s say (for whatever reason) we wanted to organize our C# code like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&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;n&quot;&gt;WalkFirst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;WalkSecond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&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;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;WalkFirst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&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;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&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;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;WalkSecond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&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;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&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;p&gt;What we &lt;em&gt;want&lt;/em&gt; to have happen is that the &lt;code&gt;yield return&lt;/code&gt; in &lt;code&gt;WalkFirst()&lt;/code&gt; and &lt;code&gt;WalkSecond()&lt;/code&gt; will cause &lt;code&gt;Concat()&lt;/code&gt; itself to yield and return, but it doesn&amp;#8217;t work that way. Iterators/generators reify a stack frame for you, but they only reify &lt;em&gt;one&lt;/em&gt;. If you want to have your iteration logic call other methods which also yield, you have to manually reify every level yourself by walking the sequence at each level. Something like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&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;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WalkFirst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WalkSecond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&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;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;WalkFirst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&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;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&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;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;WalkSecond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&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;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&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;p&gt;You see how we&amp;#8217;re doing &lt;code&gt;foreach&lt;/code&gt; and &lt;code&gt;yield return&lt;/code&gt; both in the &lt;code&gt;Walk_&lt;/code&gt; methods &lt;em&gt;and&lt;/em&gt; in &lt;code&gt;Concat&lt;/code&gt; itself? We&amp;#8217;re explicitly making &lt;em&gt;every&lt;/em&gt; level of the callstack an iterator. That makes sure every call frame gets reified like we need. Can we do better?&lt;/p&gt;

&lt;h2 id=&quot;python-33-delegating-generators&quot;&gt;Python 3.3: Delegating generators&lt;/h2&gt;

&lt;p&gt;The above example can be translated to Python like so:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;walkFirst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;walkSecond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;walkFirst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;walkSecond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Aside from being more terse, this is a one-to-one mapping with the C# code. But Python 3.3 adds &lt;a href=&quot;http://www.python.org/dev/peps/pep-0380/&quot;&gt;something new&lt;/a&gt; for us here. Let&amp;#8217;s use it:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;yield from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;walkFirst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;yield from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;walkSecond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;walkFirst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;walkSecond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The explicit loops in &lt;code&gt;concat()&lt;/code&gt; have been replaced with a new &lt;code&gt;yield from&lt;/code&gt; statement. Nice. This makes composing generators a little cleaner. But there&amp;#8217;s no real magic here. We still have to have &lt;code&gt;yield&lt;/code&gt; at every level of our iteration code.&lt;/p&gt;

&lt;p&gt;In most cases, this is just a bit tedious but not a showstopper. But if you have higher-order functions this can actually prevent code reuse. If you have some function that takes a callback, it doesn&amp;#8217;t know if that callback wants to yield or not, so it doesn&amp;#8217;t know if it needs to yield. You may end up having to implement that function twice, once for each style.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;yield from&lt;/code&gt; is only a tiny improvement. Can we do better?&lt;/p&gt;

&lt;h2 id=&quot;ruby-enumerables-enumerators-and-fibers&quot;&gt;Ruby: Enumerables, Enumerators, and Fibers&lt;/h2&gt;

&lt;p&gt;If you ever find yourself in a game of &amp;#8220;which language has a better feature than X&amp;#8221;, Ruby is usually a safe play. Matz has culled an impressive array of features from Smalltalk and Lisp. (And let&amp;#8217;s not forget Perl, the ugly duckling paddling around Ruby&amp;#8217;s Pond of Inspiration.)&lt;/p&gt;

&lt;p&gt;In Ruby, iteration is usually internal. The idiomatic way to go through a collection is by passing a &lt;a href=&quot;http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/&quot;&gt;block&lt;/a&gt; (more or less a callback, if you&amp;#8217;re not familiar with Ruby/Smalltalk parlance) to the &lt;code&gt;each&lt;/code&gt; method on a collection. But it also supports external iterators and a &lt;code&gt;for&lt;/code&gt; expression. Impressively, it can convert the former to the latter. (Going the other way is trivial in any language.)&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s dig up an example from the previous post. Here&amp;#8217;s some Ruby code for defining a tree and iterating over the nodes of the tree in order:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Tree&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:right&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@left&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@left&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@right&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@right&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We can use it like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&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;node&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This is using internal iteration. We pass in that &lt;code&gt;{ |node| ... }&lt;/code&gt; block and the Tree class itself recursively walks the nodes and invokes the callback on each node.&lt;/p&gt;

&lt;p&gt;Now let&amp;#8217;s say we want this to be an external iterator. Maybe we want to walk two trees in parallel to see if they have the same labels. We can do this something like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Tree&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Mixin all of the enumerable methods to our class.&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Enumerable&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;another&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&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;each&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all?&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;pair&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pair&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;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pair&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;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Equal!&amp;quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;zip&lt;/code&gt; method takes an enumerable on the left and another on the right and &amp;#8220;zips&amp;#8221; them together one pair at a time. The result is an array of pairs of elements. If you zip &lt;code&gt;[1, 2, 3]&lt;/code&gt; and &lt;code&gt;['a', 'b', 'c']&lt;/code&gt; together, you get &lt;code&gt;[[1, 'a'], [2, 'b'], [3, 'c']]&lt;/code&gt;. Neat.&lt;/p&gt;

&lt;p&gt;Then &lt;code&gt;all?&lt;/code&gt; walks an array, testing each element using the given block. If the block returns &lt;code&gt;true&lt;/code&gt; for every element, &lt;code&gt;all?&lt;/code&gt; returns &lt;code&gt;true&lt;/code&gt; too. Swell.&lt;/p&gt;

&lt;p&gt;But there&amp;#8217;s a subtle problem here. The &lt;code&gt;zip&lt;/code&gt; method converts its arguments to &lt;em&gt;arrays&lt;/em&gt; before doing anything. So we&amp;#8217;ve got our nice &lt;code&gt;each&lt;/code&gt; method on Tree that generates values incrementally without wasting memory, but then we throw it at &lt;code&gt;zip&lt;/code&gt; and it goes ahead and allocates big arrays to store all of these intermediate values. If the two trees we&amp;#8217;re comparing are huge, that&amp;#8217;s a lot of wasted memory.&lt;/p&gt;

&lt;p&gt;What we&amp;#8217;d like is a way to walk those two trees &lt;em&gt;iteratively&lt;/em&gt; without creating any intermediate arrays. The &lt;code&gt;each&lt;/code&gt; method on Tree does that, but it&amp;#8217;s an internal iterator. External iterators are perfect for this task. Can we convert it? In Ruby, that&amp;#8217;s as easy as:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;another&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;a_enum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_enum&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b_enum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_enum&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That little &lt;code&gt;to_enum&lt;/code&gt; method takes an object that implements &lt;code&gt;each&lt;/code&gt; and returns an external iterator. We can use these iterators like so:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;kp&quot;&gt;loop&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a_enum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b_enum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Not equal!&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The protocol here is that &lt;code&gt;next&lt;/code&gt; returns the next item in the sequence. If there are no more items, it raises a &lt;code&gt;StopIteration&lt;/code&gt; error (which &lt;code&gt;loop&lt;/code&gt; conveniently handles).&lt;/p&gt;

&lt;p&gt;Double-plus good! We&amp;#8217;ve got nice support for both internal and external iterators, and we can easily convert back and forth between them. It&amp;#8217;s like having cake &lt;em&gt;and&lt;/em&gt; pie for dessert. The one remaining question is &lt;em&gt;how does this work&lt;/em&gt;? Look at what we have here:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;The internal iterator (the &lt;code&gt;each&lt;/code&gt; method on Tree) recursively calls itself and builds a deep callstack. At any point during that, it can emit values.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;The &lt;code&gt;to_enum&lt;/code&gt; method takes that and returns an enumerable object. When you call &lt;code&gt;next&lt;/code&gt; it runs that recursive code then somehow suspends it whenever a value is generated.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;The next time you call &lt;code&gt;next&lt;/code&gt; it picks up exactly where it left off. Somehow that entire callstack gets frozen and then thawed between each call to &lt;code&gt;next&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There must be some kind of data structure that represents an entire callstack. It doesn&amp;#8217;t reify a strack &lt;em&gt;frame&lt;/em&gt; like generators, it reifies the whole &lt;em&gt;stack.&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;a-fiber-by-any-other-name&quot;&gt;A fiber by any other name&lt;/h2&gt;

&lt;p&gt;This mystery data structure is what Ruby calls a &lt;em&gt;fiber&lt;/em&gt;. Its sort of like a thread in that it represents an in-progress computation. It has a callstack, local variables, etc. Unlike a &amp;#8220;real&amp;#8221; thread, though, it doesn&amp;#8217;t involve the OS, kernel scheduling and all of that other heavyweight stuff.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s also &lt;em&gt;cooperatively&lt;/em&gt; scheduled instead of &lt;em&gt;pre-emptively&lt;/em&gt;. That&amp;#8217;s a fancy way of saying fibers have to play nice with other. If you want a fiber to run, you have to give it control. It can&amp;#8217;t take it from you.&lt;/p&gt;

&lt;p&gt;These constructs have been given as many names as languages that support them. Lua calls them &amp;#8220;&lt;a href=&quot;http://www.lua.org/pil/9.html&quot;&gt;coroutines&lt;/a&gt;&amp;#8221; (which is, I think, the &lt;a href=&quot;http://en.wikipedia.org/wiki/Coroutine&quot;&gt;oldest name&lt;/a&gt; for the idea). &lt;a href=&quot;http://www.stackless.com/&quot;&gt;Stackless Python&lt;/a&gt; calls them &amp;#8220;tasklets&amp;#8221;. Go&amp;#8217;s &amp;#8220;&lt;a href=&quot;http://golang.org/doc/effective_go.html#goroutines&quot;&gt;goroutines&lt;/a&gt;&amp;#8221; are similar, though with some interesting differences.&lt;/p&gt;

&lt;p&gt;This is the special sauce we need for &lt;code&gt;to_enum&lt;/code&gt;. When you call it, it spins up a new fiber. Then it runs the internal iterator on that &lt;em&gt;new&lt;/em&gt; fiber. When you call &lt;code&gt;next&lt;/code&gt; on the enumerator, it runs that fiber until it generates a value. When it does, it suspends the fiber, returns the value, and runs the main fiber. When we need the next value, it just suspends the main fiber and resumes the spawned one again.&lt;/p&gt;

&lt;p&gt;In other words, a simplified implementation of &lt;code&gt;to_enum&lt;/code&gt; looks a bit like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Object&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_enum&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;MyEnumerator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And the MyEnumerator class (which is simplified from &lt;a href=&quot;http://stackoverflow.com/a/1437678/9457&quot;&gt;this excellent StackOverflow answer&lt;/a&gt;) is:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyEnumerator&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Enumerable&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@fiber&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Fiber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Spin up a new fiber.&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Run the internal iterator on it.&lt;/span&gt;
        &lt;span class=&quot;no&quot;&gt;Fiber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;yield&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# When it yields a value, suspend&lt;/span&gt;
                           &lt;span class=&quot;c1&quot;&gt;# the fiber and emit the value.&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;StopIteration&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Then signal that we&amp;#39;re done.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;next&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@fiber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;resume&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;# When the next value is requested,&lt;/span&gt;
                           &lt;span class=&quot;c1&quot;&gt;# resume the fiber.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;iteration-or-concurrency&quot;&gt;Iteration or concurrency?&lt;/h2&gt;

&lt;p&gt;I started this two-parter with a question about how you can make iteration beautiful and easy to work with. This leads us to wanting both internal and external iteration, and the ability to go from one to the other. The piece needed to really make that work is &lt;em&gt;an easy way to create a new callstack&lt;/em&gt;. And fibers are a great answer for that.&lt;/p&gt;

&lt;p&gt;But what I find interesting is where we arrived at. We started talking about &lt;em&gt;iteration&lt;/em&gt;, one of the most shallow of flow control structures. But look where we ended up. Fibers are a &lt;em&gt;concurrency&lt;/em&gt; mechanism. Concurrency is way over in the deep end of language features.&lt;/p&gt;

&lt;p&gt;I don&amp;#8217;t think this is a coincidence. If you look at iteration, it actually is about concurrency. You&amp;#8217;ve got two &amp;#8220;threads&amp;#8221; of behavior: one that&amp;#8217;s generating values and one that&amp;#8217;s consuming them. You need to run these two threads together and coordinate them. That &lt;em&gt;is&lt;/em&gt; concurrency. We&amp;#8217;re just so used to it, that we don&amp;#8217;t think of it that way.&lt;/p&gt;

&lt;h2 id=&quot;wait-what-about-magpie&quot;&gt;Wait, what about Magpie?&lt;/h2&gt;

&lt;p&gt;I screwed myself over here. The secret agenda of this pair of posts was to trick you into reading about &lt;em&gt;my&lt;/em&gt; language by promising to teach you something about other languages you may actually use in real life.&lt;/p&gt;

&lt;p&gt;If we&amp;#8217;re both lucky you did learn something, but I haven&amp;#8217;t gotten to my language yet. Alas, I&amp;#8217;m already 5,000 words in and I&amp;#8217;ve surely exhausted your patience. I guess Magpie will have to wait for a later post. Trust me, though. It&amp;#8217;s &lt;em&gt;awesome&lt;/em&gt;. Promise.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <id>http://journal.stuffwithstuff.com/2013/01/13/iteration-inside-and-out</id>
    <link type="text/html" rel="alternate" href="http://journal.stuffwithstuff.com/2013/01/13/iteration-inside-and-out"/>
    <title>Iteration Inside and Out</title>
    <published>2013-01-13T00:00:00-08:00</published>
    <updated>2013-01-13T00:00:00-08:00</updated>
    <author>
      <name>Robert Nystrom</name>
      <uri>http://journal.stuffwithstuff.com/</uri>
    </author>
    <content type="html">&lt;p&gt;You would think iteration, you know &lt;em&gt;looping over stuff&lt;/em&gt;, would be a solved problem in programming languages. Seriously, here&amp;#8217;s some &lt;em&gt;FORTRAN&lt;/em&gt; code that does a loop and would run on a computer fifty years ago:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;fortran&quot;&gt;&lt;span class=&quot;k&quot;&gt;do &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;i&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;mi&quot;&gt;10&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end do&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So when I started designing loops in my little language &lt;a href=&quot;http://magpie-lang.org/&quot;&gt;Magpie&lt;/a&gt;, I figured it would be pretty straightforward:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Look at a bunch of other languages.&lt;/li&gt;
  &lt;li&gt;See what the awesome-est one does.&lt;/li&gt;
  &lt;li&gt;Do that.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, of course, the first wrinkle is that this isn&amp;#8217;t just about looping a certain number of times, or through just a range of numbers. That&amp;#8217;s baby stuff. Hell, &lt;em&gt;C&lt;/em&gt; can do that.&lt;/p&gt;

&lt;p&gt;This is about &lt;em&gt;iteration&lt;/em&gt;: being able to generate and consume arbitrary sequences of stuff. It&amp;#8217;s not just &amp;#8220;every item in a list,&amp;#8221; it&amp;#8217;s &amp;#8220;the leaves of a tree,&amp;#8221; or &amp;#8220;the lines in a file&amp;#8221; or &amp;#8220;the prime numbers&amp;#8221;. So there&amp;#8217;s an implied level of abstraction here: you need to be able to define what &amp;#8220;iteration&amp;#8221; means for your own uses.&lt;/p&gt;

&lt;p&gt;What I found kind of surprised me. It turns out there&amp;#8217;s two completely separate unrelated styles for doing iteration in languages out in the wild. &lt;a href=&quot;http://gafter.blogspot.com/2007/07/internal-versus-external-iterators.html&quot;&gt;Gafter and the Gang of Four&lt;/a&gt; (also an excellent band name) call these &amp;#8220;internal&amp;#8221; and &amp;#8220;external&amp;#8221; iterators, which sounds pretty fancy.&lt;/p&gt;

&lt;p&gt;Each of these styles is just beautifully elegant for some use cases, and kitten-punchingly awful for others. They&amp;#8217;re like Yin and Yang, or maybe Kid and Play.&lt;/p&gt;

&lt;h2 id=&quot;external-iterators-oops-i-did-it-again&quot;&gt;External iterators: OOPs, I did it again.&lt;/h2&gt;

&lt;p&gt;The first side of the coin is &lt;em&gt;external&lt;/em&gt; iterators. If you code in C++, Java, C#, Python, PHP, or pretty much any &lt;a href=&quot;http://en.wikipedia.org/wiki/Dynamic_dispatch#Single_and_multiple_dispatch&quot;&gt;single-dispatch&lt;/a&gt; object-oriented language, this is you. Your language gives you some &lt;code&gt;for&lt;/code&gt; or &lt;code&gt;foreach&lt;/code&gt; statement, like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elements&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elements&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;(This is &lt;a href=&quot;http://www.dartlang.org/&quot;&gt;Dart&lt;/a&gt; if you were wondering.)&lt;/p&gt;

&lt;p&gt;What the compiler sees is a little different. If you squint through the Matrix, then a loop like the above is really:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elements&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__iterator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elements&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__iterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveNext&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;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__iterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&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;p&gt;The &lt;code&gt;.iterator()&lt;/code&gt;, &lt;code&gt;.moveNext()&lt;/code&gt;, and &lt;code&gt;.current&lt;/code&gt; calls are the &lt;em&gt;iteration protocol&lt;/em&gt;. If you want to define your own iterable thing, you create a type that supports that protocol. Since a &lt;code&gt;for&lt;/code&gt; statement compiles down to that (or &amp;#8220;&lt;a href=&quot;http://en.wikipedia.org/wiki/Syntactic_sugar&quot;&gt;desugars&lt;/a&gt;&amp;#8221; if you&amp;#8217;re hip to PL nerd lingo), supporting that protocol lets your type work seamlessly inside a loop.&lt;/p&gt;

&lt;p&gt;In statically typed languages, this &amp;#8220;protocol&amp;#8221; is actually an explicit interface:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Java: &lt;a href=&quot;http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Iterable.html&quot;&gt;&lt;code&gt;Iterable&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;C#: &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx&quot;&gt;&lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Dart: &lt;a href=&quot;http://api.dartlang.org/docs/bleeding_edge/dart_core/Iterator.html&quot;&gt;&lt;code&gt;Iterable&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In dynamically-typed languages, it&amp;#8217;s more informal, like Python&amp;#8217;s &lt;a href=&quot;http://docs.python.org/2/library/stdtypes.html#iterator-types&quot;&gt;iterator protocol&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;beautiful-example-1-finding-an-item&quot;&gt;Beautiful example 1: Finding an item&lt;/h3&gt;

&lt;p&gt;Here&amp;#8217;s a simple example where it works well. Let&amp;#8217;s write a function that returns &lt;code&gt;true&lt;/code&gt; if a sequence contains a given item and &lt;code&gt;false&lt;/code&gt; if it doesn&amp;#8217;t. I&amp;#8217;ll use Dart again because I think Dart actually works pretty well as an &lt;em&gt;Ur&lt;/em&gt;-language that most programmers can grok:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;n&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Iterable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;haystack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;needle&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;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;haystack&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;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;needle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&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;p&gt;Dead simple. One key property this has is that it &lt;em&gt;short-circuits&lt;/em&gt;: it will stop iterating as soon as it finds the item. This is not just an optimization, but critical when you consider that some sequences (like reading the lines in a file) may have side-effects, or you may have an infinite sequence.&lt;/p&gt;

&lt;h3 id=&quot;beautiful-example-2-interleaving-two-sequences&quot;&gt;Beautiful example 2: Interleaving two sequences&lt;/h3&gt;

&lt;p&gt;Let&amp;#8217;s do something a bit more complex. Let&amp;#8217;s write a function that takes two sequences and returns a sequence that will alternate between items in each sequence. So if you throw &lt;code&gt;[1, 2, 3]&lt;/code&gt; and &lt;code&gt;['a', 'b', 'c']&lt;/code&gt;, you&amp;#8217;ll get back &lt;code&gt;1, 'a', 2, 'b', 3, 'c'&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;n&quot;&gt;interleave&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Iterable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Iterable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InterleaveIterable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&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;p&gt;This just delegates to an object, because you need some type to hang the iterator protocol off of. Here&amp;#8217;s that type:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;InterleaveIterable&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Iterable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Iterable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;InterleaveIterable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;Iterator&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterator&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InterleaveIterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iterator&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;p&gt;OK, again just another bit of delegation. This is because most iterator protocols separate the &amp;#8220;thing that can be iterated&amp;#8221; from the object representing the &lt;em&gt;current&lt;/em&gt; iteration state. The former is not modified by being iterated over, but the latter is. So now let&amp;#8217;s get to the real meat:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;InterleaveIterator&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Iterator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Iterator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;InterleaveIterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveNext&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;c1&quot;&gt;// Stop if we&amp;#39;re done.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&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;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moveNext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Swap them so we&amp;#39;ll pull from the other one next time.&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&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;kd&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current&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;p&gt;This is a bit verbose, but it&amp;#8217;s pretty straightforward. Each time you call &lt;code&gt;moveNext()&lt;/code&gt;, it reads from one of the iterators and then swaps them. It stops as soon as either one is done. Pretty groovy.&lt;/p&gt;

&lt;h3 id=&quot;kitten-punch-example-walking-a-tree&quot;&gt;Kitten-punch example: Walking a tree&lt;/h3&gt;

&lt;p&gt;Now let&amp;#8217;s see the ugly side of this. Let&amp;#8217;s say we&amp;#8217;ve got a simple binary tree class, like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Tree&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&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;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&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;p&gt;Now say we want to print the tree&amp;#8217;s labels &lt;em&gt;in-order&lt;/em&gt;, meaning we print everything on the left first (recursively), then print the label, then the right. The implementation is as simple as the description:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;n&quot;&gt;printTree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&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;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;printTree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;printTree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&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;p&gt;Later, we realize we need to do other stuff on trees in order. Maybe we need to convert it to JSON, or just count the number of nodes or something. What&amp;#8217;d we&amp;#8217;d really like is to be able to &lt;em&gt;iterate&lt;/em&gt; over the nodes in order and then do whatever we want with each item. So the above function becomes:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;n&quot;&gt;printTree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&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;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&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;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&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;p&gt;For this to work, &lt;code&gt;Tree&lt;/code&gt; will have to implement the iterator protocol. What does that look like? It&amp;#8217;s best just to swallow the whole bitter pill at once:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Tree&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Iterable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tree&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;Tree&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&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;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Iterator&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TreeIterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&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;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IterateState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&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;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IterateState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&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;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TreeIterator&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tree&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;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;TreeIterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&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;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IterateState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&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;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveNext&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;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hasValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hasValue&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;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step&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;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&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;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IterateState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&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;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;hasValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;removeLast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&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;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IterateState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&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;k&quot;&gt;break&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hasValue&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;n&quot;&gt;Tree&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&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;p&gt;Sweet Mother of Turing, what the hell happened here? This exact same behavior was a &lt;em&gt;three line&lt;/em&gt; recursive function and now it&amp;#8217;s a fifty line monstrosity.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;ll get back to exactly what went wrong here but for now let&amp;#8217;s just agree that this is not a beautiful fun way to abstract over an in-order traversal. Now let&amp;#8217;s cleanse our palate.&lt;/p&gt;

&lt;h2 id=&quot;interal-iterators-dont-call-me-ill-call-you&quot;&gt;Interal Iterators: Don&amp;#8217;t Call Me, I&amp;#8217;ll Call You.&lt;/h2&gt;

&lt;p&gt;Right now, the Rubyists are grinning, the Smalltalkers are furiously waving their hands in the air to get the teacher&amp;#8217;s attention and the Lispers are just nodding smugly in the back row (all as usual). Here&amp;#8217;s what they know that you may not:&lt;/p&gt;

&lt;p&gt;Those languages (Smalltalk, Ruby by way of Smalltalk, and most Lisps) use &lt;em&gt;internal&lt;/em&gt; iterators. When you&amp;#8217;re iterating you&amp;#8217;ve got two chunks of code in play:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The code responsible for generating the series of values.&lt;/li&gt;
  &lt;li&gt;The code that takes that series of values and does something with it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With external iterators, (1) is the type implementing the iterator protocol and (2) is the body of the &lt;code&gt;for&lt;/code&gt; loop. In that style, (2) is in charge. It decides when to invoke (1) to get the next value and can stop at any time.&lt;/p&gt;

&lt;p&gt;Internal iterators reverse that power dynamic. With an internal iterator, the code that generates values decides when to invoke the code that uses that value. For example, here&amp;#8217;s how you print the Beatles in Ruby:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;beatles&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;George&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;John&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Paul&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Ringo&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;beatles&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&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;beatle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;beatle&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That &lt;code&gt;each&lt;/code&gt; method on &lt;code&gt;Array&lt;/code&gt; is the iterator. Its job is to walk over each item in the array. The &lt;code&gt;{ |beatle| puts beatle }&lt;/code&gt; is the code we want to run for each item. The curlies define a &lt;em&gt;block&lt;/em&gt; in Ruby: a first-class chunk of code you can pass around.&lt;/p&gt;

&lt;p&gt;So what this does is bundle up that &lt;code&gt;puts&lt;/code&gt; expression into an object and send it to &lt;code&gt;each&lt;/code&gt;. The &lt;code&gt;each&lt;/code&gt; method can then iterate through each item in the array and call that block of code, passing in the item.&lt;/p&gt;

&lt;h3 id=&quot;beautiful-example-1-walking-a-tree&quot;&gt;Beautiful example 1: Walking a tree&lt;/h3&gt;

&lt;p&gt;Let&amp;#8217;s see what our ugly external iterator example looks like in Ruby. First, we&amp;#8217;ll define the tree:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Tree&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:right&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To walk the tree using an internal iterator style, we&amp;#8217;ll want this to magically work:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in_order&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;node&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Implementing that iterator in Dart (or Java, or C#) was about 50 lines of code. Here it is in Ruby:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Tree&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;in_order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@left&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in_order&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@left&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@right&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in_order&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@right&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Yup, that&amp;#8217;s it. It looks pretty much like the original recursive function, because it &lt;em&gt;is&lt;/em&gt; just like that function. The only difference is where that Dart function was hard-coded to just call &lt;code&gt;print()&lt;/code&gt;, this one takes a &lt;em&gt;block&lt;/em&gt;, basically a callback to invoke with each value. In fact, we can implement the same thing in any language with anonymous functions. Here&amp;#8217;s Dart:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;n&quot;&gt;inOrder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&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;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inOrder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inOrder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&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;p&gt;You couldn&amp;#8217;t do this in Java (&amp;#8230;&lt;a href=&quot;http://openjdk.java.net/projects/lambda/&quot;&gt;yet&lt;/a&gt;), but in most OOP languages you can passably fake internal iterator style. It&amp;#8217;s just not idiomatic in those languages.&lt;/p&gt;

&lt;p&gt;Internal iteration is definitely beating external style in this tree example. Let&amp;#8217;s see how it fares on the others.&lt;/p&gt;

&lt;h3 id=&quot;beatiful-example-2-finding-an-item&quot;&gt;Beatiful example 2: Finding an item&lt;/h3&gt;

&lt;p&gt;OK, let&amp;#8217;s say we&amp;#8217;re using Ruby and we want to write a method that, given any iterable object, sees if it contains some object. By &amp;#8220;any iterable object&amp;#8221;, we&amp;#8217;ll mean &amp;#8220;has an &lt;code&gt;each&lt;/code&gt;&amp;#8221; method, which is the canonical way to iterate. Something like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;haystack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;needle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;haystack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&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;item&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;needle&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Not bad! So we&amp;#8217;re two-for-two on internal style. Let&amp;#8217;s transmogrify this into Dart:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;n&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Iterable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;haystack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;needle&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;n&quot;&gt;haystack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&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;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;needle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&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;p&gt;Still pretty terse! Except there&amp;#8217;s one problem: &lt;em&gt;it doesn&amp;#8217;t actually work.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;What&amp;#8217;s the difference? In both examples, there&amp;#8217;s a little chunk of code: &lt;code&gt;return true&lt;/code&gt;. The intent of that code is to cause the &lt;code&gt;contains()&lt;/code&gt; method to return &lt;code&gt;true&lt;/code&gt;. But in the Dart example, that &lt;code&gt;return&lt;/code&gt; statement is contained inside a lambda, a little anonymous function:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&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;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;needle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&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;p&gt;So all it does is cause that &lt;em&gt;function&lt;/em&gt; to return. So it ends, and returns back to &lt;code&gt;forEach()&lt;/code&gt; which then proceeds along its merry way onto the next item. In Ruby, that &lt;code&gt;return&lt;/code&gt; doesn&amp;#8217;t return from the &lt;em&gt;block&lt;/em&gt; that contains it, it returns from the &lt;em&gt;method&lt;/em&gt; that contains it. A &lt;code&gt;return&lt;/code&gt; will walk up any enclosing blocks, returning from &lt;em&gt;all&lt;/em&gt; of them until it hits an honest-to-God method and then make &lt;em&gt;that&lt;/em&gt; return.&lt;/p&gt;

&lt;p&gt;This feature is called &amp;#8220;&lt;a href=&quot;http://yehudakatz.com/2010/02/07/the-building-blocks-of-ruby/&quot;&gt;non-local returns&lt;/a&gt;&amp;#8221;. Smalltalk has it, as does Ruby. If you want internal iterators, and you want them to be able to terminate early like we do here, you really need non-local returns.&lt;/p&gt;

&lt;p&gt;This is a big part of the reason why internal iterators aren&amp;#8217;t idiomatic in other languages. It&amp;#8217;s really limiting if your &lt;code&gt;each&lt;/code&gt; or &lt;code&gt;forEach()&lt;/code&gt; function can&amp;#8217;t early out easily.&lt;/p&gt;

&lt;h3 id=&quot;kitten-punching-example-interleaving-two-sequences&quot;&gt;Kitten-punching example: Interleaving two sequences&lt;/h3&gt;

&lt;p&gt;The other example that worked well with external iterators was interleaving two sequences together. It was a bit verbose, but it worked just fine and could be used with any pair of sequences. Let&amp;#8217;s translate that to an internal style. This post is plenty long, so I&amp;#8217;ll leave it as an exercise. Go do it real quick and come back.&lt;/p&gt;

&lt;p&gt;&amp;#8230;&lt;/p&gt;

&lt;p&gt;Back so soon? How&amp;#8217;d it go? How much time did you waste?&lt;/p&gt;

&lt;p&gt;Right. As far as I can tell, you simply &lt;em&gt;can&amp;#8217;t&lt;/em&gt; solve this problem using internal iterators unless you&amp;#8217;re willing to reach for some heavy weaponry like threads or continuations. You&amp;#8217;re up a creek &lt;em&gt;sans&lt;/em&gt; paddle.&lt;/p&gt;

&lt;p&gt;This is, I think, a big reason why most mainstream languages do external iterators. Sure, the tree example was verbose, but at least it was &lt;em&gt;possible&lt;/em&gt;. (It&amp;#8217;s also probably why languages that do internal iterators also have continuations.)&lt;/p&gt;

&lt;h2 id=&quot;whats-the-problem&quot;&gt;What&amp;#8217;s the problem?&lt;/h2&gt;

&lt;p&gt;It appears we&amp;#8217;re at a stalemate. External iterators rock for some things, internal at others. Why is there no solution that&amp;#8217;s great at all of them? The issue boils down to one thing: &lt;em&gt;the callstack&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You probably don&amp;#8217;t think about it like this, but &lt;em&gt;the callstack is a data structure&lt;/em&gt;. Each stack frame (i.e. a function that you&amp;#8217;re currently in) is like an object. The local variables are the fields in that object.&lt;/p&gt;

&lt;p&gt;You get another bit of extra data for free too: the current execution pointer. The callstack keeps track of where you are in your function. For example:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;n&quot;&gt;lameExample&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;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;I&amp;#39;m at the top&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;doSomething&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;I&amp;#39;m in the middle&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;doSomething&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Dead last like a chump&amp;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;p&gt;We kind of take this for granted, but each time &lt;code&gt;doSomething()&lt;/code&gt; returns to this &lt;code&gt;lameExample()&lt;/code&gt;, it picks up right where it left off. That&amp;#8217;s handy. Remember our recursive tree traversal:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;n&quot;&gt;printTree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&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;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;printTree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;printTree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&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;p&gt;After calling &lt;code&gt;printTree()&lt;/code&gt; on the left branch, it resumed where it left off, printed the label, and went to the next branch. Once you throw in recursion, you also get the ability to represent a &lt;em&gt;stack&lt;/em&gt; of these implicit data structures. The callstack itself (hence the name) will track which parent branches we&amp;#8217;re in the middle of traversing.&lt;/p&gt;

&lt;p&gt;When we converted that function to an external iterator, that fifty lines of boilerplate was just &lt;a href=&quot;http://en.wikipedia.org/wiki/Reification_(computer_science)&quot;&gt;reifying&lt;/a&gt; the data structures the callstack was giving us for free. The &lt;code&gt;IterateState&lt;/code&gt; class is exactly what each call frame stored. The &lt;code&gt;tree&lt;/code&gt; field in it was the &lt;code&gt;tree&lt;/code&gt; &lt;em&gt;parameter&lt;/em&gt; in the &lt;code&gt;printTree&lt;/code&gt; function. The &lt;code&gt;step&lt;/code&gt; field was the execution pointer. The &lt;code&gt;stack&lt;/code&gt; in &lt;code&gt;TreeIterator&lt;/code&gt; was the callstack.&lt;/p&gt;

&lt;p&gt;The lesson here is that stack frames are an amazingly terse way of storing state. You don&amp;#8217;t realize how much it&amp;#8217;s doing for you until you have to write it all out by hand. If anyone ever asks me what my favorite data structure is, my answer is always: &lt;em&gt;the callstack&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;who-owns-the-callstack&quot;&gt;Who owns the callstack?&lt;/h2&gt;

&lt;p&gt;This is the key we need to see why each iteration style sucks for some things. It&amp;#8217;s a question of who gets to control the callstack. Earlier, I said that there are two chunks of code involved in iteration: the code generating the values, and the code doing stuff with them. In an external iterator, your callstack looks like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;text&quot;&gt;+------------+
| moveNext() |
+------------+
| loop body  |
+------------+
  ...

  main()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The method containing the loop calls &lt;code&gt;moveNext()&lt;/code&gt;, which pushes it on top of the stack. It can in turn call whatever it wants, so it &lt;em&gt;temporarily&lt;/em&gt; has free reign on the callstack. But it has to return, unwind, and discard all of that state to return to the loop body before it can generate the next value.&lt;/p&gt;

&lt;p&gt;That&amp;#8217;s why the tree example was so verbose. Since all of that state would be trashed if it was stored in callframes, it had to reify it&amp;mdash;stick it in that &lt;code&gt;stack&lt;/code&gt; of &lt;code&gt;IterateState&lt;/code&gt; objects stored in the iterator object. That way it&amp;#8217;s still around the next time &lt;code&gt;moveNext()&lt;/code&gt; is called.&lt;/p&gt;

&lt;p&gt;With an internal iterator, it&amp;#8217;s like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;text&quot;&gt;+------------------------+
| each                   |
+------------------------+
| method containing loop |
+------------------------+
  ...

  main()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now the iterator is on top. It can build up whatever stack frames it wants, and then, whenever its convenient, invoke the block:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;text&quot;&gt;+------------------------+
| block                  |
+------------------------+
  stuff...
+------------------------+
| each                   |
+------------------------+
| method containing loop |
+------------------------+
  ...

  main()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The &lt;em&gt;block&lt;/em&gt; now has to return to &lt;code&gt;each&lt;/code&gt; (or whatever &lt;code&gt;each&lt;/code&gt; calls). So the iterator can keep whatever state on the callstack it wants, since it&amp;#8217;s in control. But, as you can see, you really need non-local returns for this to work well. Because, when the block &lt;em&gt;does&lt;/em&gt; want to stop iteration, it needs a way to unwind all the way through &lt;code&gt;stuff...&lt;/code&gt; and &lt;code&gt;each&lt;/code&gt; all the way back to the method.&lt;/p&gt;

&lt;p&gt;That&amp;#8217;s the issue. Whoever gets put on top of the stack is in the weaker position, because it has to return all the way to the other one between each generated value. In some use cases, the generator of values needs that power (recursively walking a tree), and internal iterators work great. In others, the consumer of values needs that power (interleaving two iterators) and external ones win.&lt;/p&gt;

&lt;p&gt;Since there&amp;#8217;s just one callstack, that&amp;#8217;s the best we can do. &lt;em&gt;Or is it?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Check out &lt;a href=&quot;/2013/02/24/iteration-inside-and-out-part-2/&quot;&gt;part two&lt;/a&gt; to see what some languages have done to try to deal with this.&lt;/em&gt;&lt;/p&gt;

</content>
  </entry>
  
  <entry>
    <id>http://journal.stuffwithstuff.com/2012/12/19/the-impoliteness-of-overriding-methods</id>
    <link type="text/html" rel="alternate" href="http://journal.stuffwithstuff.com/2012/12/19/the-impoliteness-of-overriding-methods"/>
    <title>The Impoliteness of Overriding Methods</title>
    <published>2012-12-19T00:00:00-08:00</published>
    <updated>2012-12-19T00:00:00-08:00</updated>
    <author>
      <name>Robert Nystrom</name>
      <uri>http://journal.stuffwithstuff.com/</uri>
    </author>
    <content type="html">&lt;p&gt;Over the weekend, I was reading one of the shagadelic papers on &lt;a href=&quot;http://selflanguage.org&quot;&gt;Self&lt;/a&gt;,
&lt;a href=&quot;http://selflanguage.org/documentation/published/parents-shared-parts.html&quot;&gt;Parents are Shared Parts of Objects: Inheritance and Encapsulation in SELF&lt;/a&gt;.
What can I say, I have a weird idea of fun. If you&amp;#8217;re interested in prototypes,
or you&amp;#8217;re a Javascripter (but I repeat myself) you owe it to yourself to read
these papers. They are gems.&lt;/p&gt;

&lt;p&gt;But this post isn&amp;#8217;t about prototypes, it&amp;#8217;s about something the Self folks
mention in passing:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In BETA, virtual functions are invoked from least specific to most specific,
with the keyword &lt;code&gt;inner&lt;/code&gt; being used to invoke the next more specific method.
This mechanism is a product of the philosophy in BETA that subclasses should
be behavioral extensions to their superclasses and therefore specialize the
behavior of their superclasses at well-defined points (i.e. at calls to &lt;code&gt;inner&lt;/code&gt;).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It took me a while to tease out what this is saying, but once I did, it was
like a dim little light bulb flickered on in my head.&lt;/p&gt;

&lt;h2 id=&quot;whats-beta&quot;&gt;What&amp;#8217;s BETA?&lt;/h2&gt;

&lt;p&gt;Before I get into the lightbulb part, a bit of history. &lt;a href=&quot;http://daimi.au.dk/~beta&quot;&gt;BETA&lt;/a&gt; is a language
that came out of the &amp;#8220;Scandinavian School&amp;#8221; in Denmark, the &lt;a href=&quot;http://en.wikipedia.org/wiki/Kristen_Nygaard&quot;&gt;same people&lt;/a&gt; that
brought you &lt;a href=&quot;http://en.wikipedia.org/wiki/Simula&quot;&gt;Simula&lt;/a&gt; and kicked off the object-oriented revolution. Alan Kay
may have coined &amp;#8220;object-oriented programming&amp;#8221;, but it was Simula that gave him
the idea. Chances are, the language you should be coding in right now instead
of slacking off reading my blog was directly inspired by these guys.&lt;/p&gt;

&lt;p&gt;So after Simula, they went off and made BETA. I think this is more or less
equivalent to &amp;#8220;famous rock band goes into hiding for ten years and emerges with
avant garde free jazz album&amp;#8221;. BETA was used as a teaching language, I think,
and there were some papers about it, but I don&amp;#8217;t know if many people seriously
used it in anger.&lt;/p&gt;

&lt;p&gt;(Trivia time! Some of the guys who made &lt;a href=&quot;http://code.google.com/p/v8/&quot;&gt;V8&lt;/a&gt;, the famously-fast JavaScript
engine in Chrome &lt;em&gt;did&lt;/em&gt; use BETA. &amp;#8220;V8&amp;#8221; got its name because it&amp;#8217;s the eighth
virtual machine that Lars Bak created. His first VM? A BETA one.)&lt;/p&gt;

&lt;p&gt;Part of the reason BETA didn&amp;#8217;t flourish may have to do with terminology.
Instead of &lt;em&gt;classes&lt;/em&gt; and &lt;em&gt;methods&lt;/em&gt;, BETA has &lt;em&gt;patterns&lt;/em&gt; which subsume both,
somehow, and aren&amp;#8217;t related to other uses of the term in other languages. I
wrote that sentence, and I don&amp;#8217;t even know what the hell that means.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;http://daimi.au.dk/~beta/Books/index.html#betabook_download&quot;&gt;BETA book&lt;/a&gt; is
a bit&amp;#8230; dense. Or maybe it&amp;#8217;s just that the syntax is so&amp;#8230; &lt;em&gt;weird&lt;/em&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;text&quot;&gt;Account:
  (# balance: @integer;
    Deposit:
      (# amount: @integer
      enter amount
      do balance+amount-&amp;gt;balance
      exit balance
      #);

    Withdraw:
      (# amount: @integer
      enter amount
      do balance-amount-&amp;gt;balance
      exit balance
      #);
  #)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I pride myself on being able to grok syntax on a pretty wide variety of
languages but I&amp;#8217;m not even sure what&amp;#8217;s a &lt;em&gt;comment&lt;/em&gt; there. I &lt;em&gt;think&lt;/em&gt; if you
translated that to JavaScript, it would be something like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;account&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;balance&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;deposit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;amount&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;balance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;amount&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;nx&quot;&gt;withdraw&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;amount&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;balance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;amount&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;p&gt;Like avant garde jazz, this may be genius, but it&amp;#8217;s so out there and
unapproachable, it&amp;#8217;s hard to tell. Fortunately, the Self guys have deciphered
some of the mystery and left that little nugget in their paper.&lt;/p&gt;

&lt;h2 id=&quot;overriding-and-super&quot;&gt;Overriding and &lt;code&gt;super()&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Let&amp;#8217;s cover one last bit of context before I get to the point. If you&amp;#8217;re using
any OOP language, you&amp;#8217;re hopefully familiar with overriding methods. Details
vary between languages, but the two main points are:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;A subclass can &lt;em&gt;override&lt;/em&gt; a method in its superclass. When you invoke the
method on an instance of the subclass, the derived method gets called &lt;em&gt;first&lt;/em&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;In the body of the overriding method, it can invoke the base class method
directly in order to chain the two methods together. In Java, you do so by
calling &lt;code&gt;super.someMethod()&lt;/code&gt;. In C# it&amp;#8217;s &lt;code&gt;base.someMethod()&lt;/code&gt;. In CLOS, you use
&lt;code&gt;call-next-method&lt;/code&gt;. You get the idea.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I&amp;#8217;m using class terminology here, but all of the above applies equally well to
prototypal languages too, with a couple of names changed.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s an example:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Account&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;balance&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;o&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;deposit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;balance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CrappyBankAccount&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Account&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;deposit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Service charge, sucker!&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;CrappyBank&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;mainAccount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;deposit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;deposit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So now, if you do something like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;Account&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CrappyBankAccount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;deposit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;First, it invokes &lt;code&gt;CrappyBankAccount#deposit()&lt;/code&gt;. Then, when that calls
&lt;code&gt;super.deposit()&lt;/code&gt;, it chains to the base &lt;code&gt;Account#deposit()&lt;/code&gt; method.&lt;/p&gt;

&lt;h2 id=&quot;whos-in-charge-here&quot;&gt;Who&amp;#8217;s in Charge Here?&lt;/h2&gt;

&lt;p&gt;What this means is that the &lt;em&gt;subclass&lt;/em&gt; is in control of the dispatch chain.
When you override a method in Java, you get to decide if you do stuff before
calling &lt;code&gt;super&lt;/code&gt; or after. You can change the arguments you pass to it, or even
skip calling it entirely.&lt;/p&gt;

&lt;p&gt;This is great for flexibility, but as an API designer, that can be frustrating.
When I&amp;#8217;m making a class that&amp;#8217;s designed to be subclassed, I often have
constraints that I want my class to ensure.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GameObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Renderer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setTransform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ScaryMonster&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GameObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Renderer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;drawImage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Images&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;SCARY_MONSTER&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Here we&amp;#8217;re making a game with a base class for a character in the world. It
provides a default &lt;code&gt;render()&lt;/code&gt; method that tells the renderer where to render.
The subclass overrides it and draws the specific image that&amp;#8217;s appropriate for
that character.&lt;/p&gt;

&lt;p&gt;There&amp;#8217;s an implied requirement here: if you override &lt;code&gt;render()&lt;/code&gt; in a subclass,
you &lt;em&gt;must&lt;/em&gt; call &lt;code&gt;super.render()&lt;/code&gt; &lt;em&gt;before&lt;/em&gt; you do any drawing. If you don&amp;#8217;t, the
transform won&amp;#8217;t be set and it&amp;#8217;ll draw wrong.&lt;/p&gt;

&lt;p&gt;These hidden requirements rub me the wrong way. If you&amp;#8217;re implementing a
subclass of &lt;code&gt;GameObject&lt;/code&gt;, how are you supposed to know that you need to do that?
You can document it, but it would be better if the base class itself made sure
you did the right thing.&lt;/p&gt;

&lt;h2 id=&quot;render-and-onrender&quot;&gt;&lt;code&gt;render()&lt;/code&gt; and &lt;code&gt;onRender()&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;To solve this, what I (and lots of other people) do is split these into two
methods, like so:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GameObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Renderer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setTransform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;onRender&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onRender&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ScaryMonster&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GameObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onRender&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Renderer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;drawImage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Images&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;SCARY_MONSTER&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Our public &lt;code&gt;render()&lt;/code&gt; method is now designed to &lt;em&gt;not&lt;/em&gt; be overridden. (In C++ or
C# you&amp;#8217;d make it non-virtual.) It does the set-up it needs and calls the
protected abstract &lt;code&gt;onRender()&lt;/code&gt; method. That method &lt;em&gt;is&lt;/em&gt; intended to be
overridden, and by making it protected and abstract, it&amp;#8217;s clear you &lt;em&gt;must&lt;/em&gt;
override it. Marking it abstract also makes it clear that you don&amp;#8217;t need to
call &lt;code&gt;super()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This lets the base class stay in control of the dispatch process. It can do
set-up before and after the subclass&amp;#8217;s &amp;#8220;overridden&amp;#8221; method gets called. The
dispatch order is reversed now. When you call &lt;code&gt;render()&lt;/code&gt;, you hit the
superclass &lt;em&gt;first&lt;/em&gt; and then it calls &lt;code&gt;onRender()&lt;/code&gt; in the subclass.&lt;/p&gt;

&lt;p&gt;This is almost always how I design classes that I intend to be subclassed. It&amp;#8217;s
rare that I override methods in my code that aren&amp;#8217;t abstract, and I&amp;#8217;ve been on
teams with style guides that enforced this pattern.&lt;/p&gt;

&lt;h2 id=&quot;back-to-beta&quot;&gt;Back to BETA&lt;/h2&gt;

&lt;p&gt;Of course, the problem with this is that it &lt;em&gt;is&lt;/em&gt; a pattern. You have to make a
pair of methods, and every time you have another level of subclassing, you need
a new name. (If there was a subclass of &lt;code&gt;ScaryMonster&lt;/code&gt; that wanted to override
&lt;code&gt;onRender()&lt;/code&gt; then &lt;code&gt;ScaryMonster&lt;/code&gt; would have to add a &lt;code&gt;onOnRender()&lt;/code&gt;.) This
brings us back to BETA.&lt;/p&gt;

&lt;p&gt;Overriding methods in BETA works exactly like this pattern, but baked right
into the language. Instead of calling &lt;code&gt;super()&lt;/code&gt; in the &lt;em&gt;derived&lt;/em&gt; class, you
call &lt;code&gt;inner()&lt;/code&gt; in the &lt;em&gt;base&lt;/em&gt; class. That tells it to chain &lt;em&gt;down&lt;/em&gt; to the
subclass at that point.&lt;/p&gt;

&lt;p&gt;When you invoke an overridden method, dispatch starts at the &lt;em&gt;base&lt;/em&gt; class (just
like we want in the &lt;code&gt;GameObject&lt;/code&gt; example) and then walks down to the subclasses
at the superclass&amp;#8217;s whim. In other words, our example with BETA-style
overriding would look like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GameObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Renderer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setTransform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ScaryMonster&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GameObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Renderer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;drawImage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Images&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;SCARY_MONSTER&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If you chain more than two levels of subclasses, BETA scales better because you
don&amp;#8217;t need to keep coming up with new names. It has some other neat attributes
too.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GameObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Renderer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setTransform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;restoreState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ScaryMonster&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GameObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Renderer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;renderer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;drawImage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Images&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;SCARY_MONSTER&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Here, we&amp;#8217;ve added a call to &lt;code&gt;restoreState()&lt;/code&gt; after the call to &lt;code&gt;inner()&lt;/code&gt;. By
giving control of the dispatch to the base class, it can execute code both
before &lt;em&gt;and&lt;/em&gt; after the derived class code. &lt;code&gt;super()&lt;/code&gt; doesn&amp;#8217;t let you do that.
(Though &lt;code&gt;super()&lt;/code&gt; does let you handle the opposite case: you can put code
before and after the &lt;em&gt;base&lt;/em&gt; class code in the derived method that calls
&lt;code&gt;super()&lt;/code&gt;.)&lt;/p&gt;

&lt;p&gt;It also gives you a convenient way to control which methods are virtual and
which aren&amp;#8217;t: if a method doesn&amp;#8217;t call &lt;code&gt;inner()&lt;/code&gt; it implicitly can&amp;#8217;t be
overridden since it cedes no control to a subclass.&lt;/p&gt;

&lt;p&gt;What this means is that base classes have explicit control over how they can be
extended. Outside of programming &amp;#8220;override&amp;#8221; has negative connotations: it means
you&amp;#8217;re hijacking something without its consent and indeed overriding does kind
of work like that in most languages.&lt;/p&gt;

&lt;p&gt;In the early days of class-based OOP, people thought any old class could be
spontaneously subclassed. You could just override some stuff and it would all
magically work out. What we&amp;#8217;ve finally realized is that the API you expose to
subclasses is another boundary layer that needs to be carefully designed.
Ad-hoc subclassing rarely works and classes generally need to be designed up
front in order to be subclassed.&lt;/p&gt;

&lt;p&gt;BETA was designed around that model. With typical Scandinavian politeness, you
don&amp;#8217;t &lt;em&gt;override&lt;/em&gt; your base class, you politely request permission to extend it.
I think right now, the style of a lot of OOP code today fits that model better.&lt;/p&gt;

&lt;p&gt;Most languages chose a different path than BETA, but this makes me wonder if
Kristen and company had it right all along.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <id>http://journal.stuffwithstuff.com/2012/06/12/multimethods-global-scope-and-monkey-patching</id>
    <link type="text/html" rel="alternate" href="http://journal.stuffwithstuff.com/2012/06/12/multimethods-global-scope-and-monkey-patching"/>
    <title>Multimethods, Global Scope, and Monkey-Patching</title>
    <published>2012-06-12T00:00:00-07:00</published>
    <updated>2012-06-12T00:00:00-07:00</updated>
    <author>
      <name>Robert Nystrom</name>
      <uri>http://journal.stuffwithstuff.com/</uri>
    </author>
    <content type="html">&lt;blockquote&gt;
  &lt;p&gt;What I mean is that if you really want to understand something, the best way is to try and explain it to someone else. That forces you to sort it out in your mind. And the more slow and dim-witted your pupil, the more you have to break things down into more and more simple ideas. And that&amp;#8217;s really the essence of programming. By the time you&amp;#8217;ve sorted out a complicated idea into little steps that even a stupid machine can deal with, you&amp;#8217;ve learned something about it yourself.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p class=&quot;cite&quot;&gt;Douglas Adams&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m interested in all kinds of languages, so I&amp;#8217;d read about &lt;a href=&quot;http://journal.stuffwithstuff.com/2011/04/21/multimethods-multiple-inheritance-multiawesome/&quot;&gt;multimethods&lt;/a&gt; and generic functions in &lt;a href=&quot;http://www.ravenbrook.com/doc/2003/07/15/clos-fundamentals/&quot;&gt;Common Lisp&lt;/a&gt;, &lt;a href=&quot;http://clojure.org/multimethods&quot;&gt;Clojure&lt;/a&gt; and &lt;a href=&quot;http://opendylan.org/&quot;&gt;Dylan&lt;/a&gt;. Even lesser-known languages like &lt;a href=&quot;http://www.cs.washington.edu/research/projects/cecil/www/cecil.html&quot;&gt;Cecil&lt;/a&gt; and &lt;a href=&quot;http://nice.sourceforge.net/&quot;&gt;Nice&lt;/a&gt;. But it wasn&amp;#8217;t until I &lt;em&gt;implemented&lt;/em&gt; them in &lt;a href=&quot;http://magpie-lang.org/&quot;&gt;my own language&lt;/a&gt; that I ran into a seemingly innocuous question: &lt;strong&gt;how are they scoped?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This question consumed a great number of showers and morning commutes. I implemented a bunch of different versions and watched them break. What I finally stumbled on is really simple, but&amp;#8230; &lt;em&gt;wrong-feeling&lt;/em&gt;. This post is an explanation of how multimethods are scoped in Magpie, but also sort of a &lt;em&gt;rationalization&lt;/em&gt; since the answer turns out to be &amp;#8220;globally&amp;#8221;.&lt;/p&gt;

&lt;p&gt;But I&amp;#8217;m getting ahead of myself. First, let me rewind and set up some preliminaries. OK, maybe a &lt;em&gt;lot&lt;/em&gt; of preliminaries. This is a corner of language design that I think few people wander into and previous explorers don&amp;#8217;t seem have left a map so maybe there will be some value in this.&lt;/p&gt;

&lt;h2 id=&quot;what-is-scope&quot;&gt;What Is Scope?&lt;/h2&gt;

&lt;p&gt;When you hear &amp;#8220;scope&amp;#8221; you probably think something like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&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;n&quot;&gt;a&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;%d&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&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;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;%d&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&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;p&gt;The curly braces in C++ define &lt;em&gt;block scopes&lt;/em&gt;. When you declare a local variable, it goes in the nearest enclosing scope. When the compiler sees a use of a variable (here &lt;code&gt;a&lt;/code&gt;), it looks in the surrounding scopes to figure out what it&amp;#8217;s bound to.&lt;/p&gt;

&lt;p&gt;Scopes can nest, like you see here. When a variable is defined in multiple scopes, the innermost one &lt;em&gt;shadows&lt;/em&gt; the others and wins. So this program will print &lt;code&gt;2&lt;/code&gt; followed by &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Some languages like JavaScript (before &lt;a href=&quot;http://wiki.ecmascript.org/doku.php?id=proposals:block_expressions&quot;&gt;ES6&lt;/a&gt;) and C (before &lt;a href=&quot;http://stackoverflow.com/questions/1880745/c99-can-i-declare-variables-in-the-beginning-of-a-block-in-a-for&quot;&gt;C99&lt;/a&gt;) don&amp;#8217;t have block scope like this. Instead they just have &lt;em&gt;function scope&lt;/em&gt;. Each function body can have its own local variables, but there are no nested scopes inside that (unless you actually nest a function in JS). You still have variables outside of functions, so you still have to think about nesting, though.&lt;/p&gt;

&lt;p&gt;Both of these kinds of scope have something in common: they use &lt;em&gt;&lt;a href=&quot;http://c2.com/cgi/wiki?LexicalScoping&quot;&gt;lexical scoping&lt;/a&gt;&lt;/em&gt;. It&amp;#8217;s called that because you can tell what a variable name is bound to just be looking at the &lt;em&gt;text&lt;/em&gt; (hence &amp;#8220;lexical&amp;#8221;) of the program. This is in contrast to &lt;em&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Static_scoping#Dynamic_scoping&quot;&gt;dynamic scoping&lt;/a&gt;&lt;/em&gt; where you can only tell what a name is bound to at &lt;em&gt;runtime&lt;/em&gt;. Lexical scoping was one of the big innovations of &lt;a href=&quot;http://en.wikipedia.org/wiki/ALGOL&quot;&gt;ALGOL&lt;/a&gt;, and almost every language works like it these days.&lt;/p&gt;

&lt;p&gt;This is all well and good for scoping &lt;em&gt;variables&lt;/em&gt;, but for the sake of this post, I&amp;#8217;ll widen the term to talk about any kind of identifier that appears in a program. Variables aren&amp;#8217;t the only place names appear in many languages. Consider this bit of JavaScript:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;who&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;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;who&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;favoriteColor&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;p&gt;Here we have two names in the program (ignoring &lt;code&gt;alert&lt;/code&gt;), &lt;code&gt;who&lt;/code&gt; and &lt;code&gt;favoriteColor&lt;/code&gt;. We need to figure out what they represent in order to run the code. &lt;code&gt;who&lt;/code&gt; is easy, it&amp;#8217;s just a lexically scoped variable and we can see that it&amp;#8217;s bound to whatever you pass to the function.&lt;/p&gt;

&lt;p&gt;What&amp;#8217;s about &lt;code&gt;favoriteColor&lt;/code&gt;? Since it comes after a &lt;code&gt;.&lt;/code&gt;, that means it&amp;#8217;s a &lt;em&gt;property accessor&lt;/em&gt;. Each object in JS carries a little bag of named properties around with it. When we do &lt;code&gt;.favoriteColor&lt;/code&gt; here, it looks for a property named &lt;code&gt;favoriteColor&lt;/code&gt; in that bag at runtime and returns its value. We&amp;#8217;ll call this &lt;em&gt;object scope&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Being a &lt;a href=&quot;http://en.wikipedia.org/wiki/Prototype-based_programming&quot;&gt;prototype-based language&lt;/a&gt;, JS is pretty simple here. It only has lexical scope and object scope. Class-based object-oriented languages often have a couple more scopes. Methods will be looked up on the &lt;em&gt;class&lt;/em&gt; of the receiver, and there is usually a separate scope for &amp;#8220;static&amp;#8221; methods.&lt;/p&gt;

&lt;h2 id=&quot;what-scopes-does-magpie-have&quot;&gt;What Scopes Does Magpie Have?&lt;/h2&gt;

&lt;p&gt;OK, so we have block scope, object scope, method scope, static scope. There&amp;#8217;s lexical scoping and dynamic scoping. Which of these does Magpie have?&lt;/p&gt;

&lt;p&gt;Just one: &lt;em&gt;lexical block scope.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Magpie is a &lt;a href=&quot;http://magpie-lang.org/classes.html&quot;&gt;class-based&lt;/a&gt; &lt;a href=&quot;http://magpie-lang.org/objects.html&quot;&gt;object-oriented&lt;/a&gt; language, and its syntax is designed to look (more or less) like one. It doesn&amp;#8217;t use a dot to separate the &amp;#8220;receiver&amp;#8221; from the method or property, but it otherwise looks like it &lt;em&gt;would&lt;/em&gt; have object, method, and static scope:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;When it&amp;#8217;s compiling &lt;code&gt;list add(item)&lt;/code&gt;, the variables &lt;code&gt;list&lt;/code&gt; and &lt;code&gt;item&lt;/code&gt; are looked up in lexical scope like you expect. But the method &lt;code&gt;add&lt;/code&gt; is too. In Magpie, methods are not bound to classes. Instead, they are defined separately. If you know C&amp;#8217;s model of &amp;#8220;structs + functions&amp;#8221;, you have roughly the right idea. If you know CLOS or Dylan, you have &lt;em&gt;exactly&lt;/em&gt; the right idea.&lt;/p&gt;

&lt;p&gt;When you see &lt;code&gt;list add(item)&lt;/code&gt; it looks like you&amp;#8217;re calling &lt;code&gt;add&lt;/code&gt; &amp;#8220;on&amp;#8221; some &lt;code&gt;list&lt;/code&gt; object, but what it really means is &amp;#8220;call this &lt;code&gt;add&lt;/code&gt; method, passing in &lt;code&gt;list&lt;/code&gt; and &lt;code&gt;item&lt;/code&gt; as arguments&amp;#8221;. It&amp;#8217;s the same as &lt;code&gt;add(list, item)&lt;/code&gt; in other languages. Magpie just has a syntax that lets you stick the method name in the middle.&lt;/p&gt;

&lt;p&gt;In addition, &amp;#8220;property accessors&amp;#8221; in Magpie &lt;a href=&quot;http://en.wikipedia.org/wiki/Uniform_access_principle&quot;&gt;are just methods too&lt;/a&gt;. So when you see &lt;code&gt;person favoriteColor&lt;/code&gt;, that&amp;#8217;s just &lt;code&gt;favoriteColor(person)&lt;/code&gt;. To compile that, we just look up &lt;code&gt;favoriteColor&lt;/code&gt; in lexical scope like you would a variable.&lt;/p&gt;

&lt;p&gt;So how far up do these scopes go? In Magpie, each source file is a &lt;a href=&quot;http://magpie-lang.org/modules.html&quot;&gt;module&lt;/a&gt; and each module has its own top-level scope. There is no single global shared scope. Instead, each module is its own little island that only sees the names that it defines or explicitly imports.&lt;/p&gt;

&lt;h2 id=&quot;what-about-polymorphism&quot;&gt;What About Polymorphism?&lt;/h2&gt;

&lt;p&gt;&amp;#8220;Polymorphism&amp;#8221; in the object-oriented sense means that the same method can do different things at runtime given different kinds of objects. &amp;#8220;Single-dispatch&amp;#8221; OOP languages, which are probably what you&amp;#8217;re familiar with, achieve this by treating the receiving object (i.e. &lt;code&gt;this&lt;/code&gt;) as a scope. By looking up the method on the receiver at runtime, you get behavior that varies based on that object. Magpie on the other hand, just looks up the method in lexical scope.&lt;/p&gt;

&lt;p&gt;If methods aren&amp;#8217;t looked up on the object (or on its class), how do we get polymorphism in Magpie? How can I make an &lt;code&gt;add()&lt;/code&gt; method that does the right thing given a list, a set, or a map?&lt;/p&gt;

&lt;p&gt;The answer is that all methods in Magpie are &lt;a href=&quot;http://journal.stuffwithstuff.com/2011/04/21/multimethods-multiple-inheritance-multiawesome/&quot;&gt;multimethods&lt;/a&gt;. The &lt;a href=&quot;http://magpie-lang.org/multimethods.html&quot;&gt;docs&lt;/a&gt; have the full story, but here&amp;#8217;s the TL;DR: You can define multiple methods with the same name but different argument &lt;a href=&quot;http://magpie-lang.org/patterns.html&quot;&gt;patterns&lt;/a&gt;, like so:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// add stuff to list...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// add stuff to set...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&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;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// add stuff to map...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In C, this would just be an error because the names collide. In Magpie, it&amp;#8217;s A-OK. What this does is a create a &lt;em&gt;single&lt;/em&gt; &lt;code&gt;add()&lt;/code&gt; &lt;em&gt;multi&lt;/em&gt;method that contains those three methods. When you call &lt;code&gt;add()&lt;/code&gt;, it looks at the types of the arguments at runtime and picks the right method to call. Instead of using &lt;em&gt;scoping&lt;/em&gt; for polymorphism, it essentially uses &lt;a href=&quot;http://www.haskell.org/tutorial/patterns.html&quot;&gt;pattern matching&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;time-for-a-snack-break&quot;&gt;Time For a Snack Break&lt;/h2&gt;

&lt;p&gt;That probably all sounds a bit hand-wavey. Let&amp;#8217;s walk through a concrete example and see how all of the moving parts mesh together. First, let&amp;#8217;s make a module that defines a class and some methods for it.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// sandwich.mag&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;defclass&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sandwich&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;meat&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;condiment&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sandwich&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sandwich&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isVegetarian&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sandwich&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;meat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;tofu&amp;quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sandwich&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sandwich&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;toString&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&amp;quot;A tasty &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;meat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot; and &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;condiment&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot; sandwich&amp;quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So we have a class &lt;code&gt;Sandwich&lt;/code&gt;. This will also automatically give us getter methods for &lt;code&gt;meat&lt;/code&gt; and &lt;code&gt;condiment&lt;/code&gt; that will return the appropriate fields given a &lt;code&gt;Sandwich&lt;/code&gt; instance on the left. In addition, we add another method &lt;code&gt;isVegetarian&lt;/code&gt;. It takes a sandwich and returns &lt;code&gt;true&lt;/code&gt; if it doesn&amp;#8217;t have any meat in it. Finally, we give it a &lt;code&gt;toString&lt;/code&gt; method so you can print it and stuff.&lt;/p&gt;

&lt;p&gt;Now let&amp;#8217;s make another module that uses this one:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// main.mag&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sandwich&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sandwich&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sandwich&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;meat&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;ham&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;condiment&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;mayo&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;veggie? &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sandwich&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isVegetarian&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sandwich&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Seems pretty simple, right? It turns out that not binding methods to classes leads to a couple of subtle but deep problems. (At least they were subtle to &lt;em&gt;me&lt;/em&gt;. I didn&amp;#8217;t realize them until I&amp;#8217;d implemented the intepreter and ran straight into them.)&lt;/p&gt;

&lt;p&gt;These problems all turn out to be related exactly to the original question of this post, how methods are scoped, and solving them is what led to Magpie&amp;#8217;s current (and perhaps surprising) answer.&lt;/p&gt;

&lt;h2 id=&quot;the-first-problem-overriding-methods&quot;&gt;The First Problem: &amp;#8220;Overriding&amp;#8221; Methods&lt;/h2&gt;

&lt;p&gt;So what happens when we run this example? Let&amp;#8217;s say for now that methods are scoped just like variables, which is how Magpie &lt;em&gt;used&lt;/em&gt; to work. We&amp;#8217;ll walk through it a line at a time.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sandwich&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This takes all of the top-level variables and methods in &lt;code&gt;sandwich.mag&lt;/code&gt; and binds them to the same names in &lt;code&gt;main.mag&lt;/code&gt;. After that &lt;code&gt;Sandwich&lt;/code&gt;, &lt;code&gt;meat&lt;/code&gt;, &lt;code&gt;condiment&lt;/code&gt;, &lt;code&gt;isVegetarian&lt;/code&gt;, and &lt;code&gt;toString&lt;/code&gt; are all available for use.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sandwich&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sandwich&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;meat&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;ham&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;condiment&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;mayo&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This creates a new &lt;code&gt;Sandwich&lt;/code&gt; instance and stores it in a variable &lt;code&gt;sandwich&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;veggie? &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sandwich&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isVegetarian&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This calls &lt;code&gt;isVegetarian&lt;/code&gt;. We&amp;#8217;ve imported that method, so there&amp;#8217;s no problem here. Now consider the last line:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sandwich&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;code&gt;print()&lt;/code&gt; is a built-in method that takes an object, converts it to a string by calling &lt;code&gt;toString&lt;/code&gt; on it, and then displays it. &amp;#8220;Built-in&amp;#8221; just means it&amp;#8217;s defined in another &lt;code&gt;core&lt;/code&gt; module and is automatically imported, so we do indeed have access to it from &lt;code&gt;main.mag&lt;/code&gt;. So we call it and pass in the sandwich. What happens next?&lt;/p&gt;

&lt;p&gt;What &lt;em&gt;doesn&amp;#8217;t&lt;/em&gt; happen is that it doesn&amp;#8217;t call the &lt;code&gt;toString&lt;/code&gt; that we actually defined for &lt;code&gt;Sandwich&lt;/code&gt;. That method is scoped to &lt;code&gt;sandwich.mag&lt;/code&gt;. We imported it into &lt;code&gt;main.mag&lt;/code&gt; so we could call it &lt;em&gt;there&lt;/em&gt;. But we aren&amp;#8217;t calling &lt;code&gt;toString&lt;/code&gt; in &lt;code&gt;main.mag&lt;/code&gt;. It&amp;#8217;s being called from &lt;code&gt;print&lt;/code&gt;, which is in &lt;code&gt;core&lt;/code&gt;. It has no idea there&amp;#8217;s this other &lt;code&gt;toString&lt;/code&gt; method specialized for sandwiches because the &lt;code&gt;toString&lt;/code&gt; multimethod &lt;code&gt;core&lt;/code&gt; knows about is unrelated to the one &lt;code&gt;sandwich.mag&lt;/code&gt; and &lt;code&gt;main.mag&lt;/code&gt; have.&lt;/p&gt;

&lt;p&gt;Ouch. Our intent in &lt;code&gt;sandwich.mag&lt;/code&gt; is that defining &lt;code&gt;toString&lt;/code&gt; would work like &lt;a href=&quot;http://en.wikipedia.org/wiki/Method_overriding&quot;&gt;overriding&lt;/a&gt; in other OOP languages. Any place that is calling &lt;code&gt;toString&lt;/code&gt; should see that new specialization even if it hasn&amp;#8217;t directly imported the module that contains it.&lt;/p&gt;

&lt;h2 id=&quot;the-first-solution-shared-multimethod-objects&quot;&gt;The First Solution: Shared Multimethod Objects&lt;/h2&gt;

&lt;p&gt;My &lt;a href=&quot;https://groups.google.com/forum/?fromgroups#!topic/magpie-lang/BilLNpvklYQ&quot;&gt;fix&lt;/a&gt; for this was to change what it means to import a multimethod. In our little example, the import graph is like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;text&quot;&gt;core
  ^
  |\
  | \
  | sandwich
  |  ^
  | /
  |/
 main
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;code&gt;main.mag&lt;/code&gt; imports &lt;code&gt;sandwich.mag&lt;/code&gt; and they both also (implicitly) import &lt;code&gt;core&lt;/code&gt;. Core itself contains a &lt;code&gt;toString&lt;/code&gt; method with specializations for the atomic types like numbers and strings.&lt;/p&gt;

&lt;p&gt;The first fix was that when you import a method, you import the &lt;em&gt;exact same multimethod object&lt;/em&gt;. So &lt;code&gt;sandwich.mag&lt;/code&gt; imports the &lt;code&gt;toString&lt;/code&gt; multimethod from &lt;code&gt;core&lt;/code&gt;. When it then defines a new &lt;code&gt;(sandwich is Sandwich) toString&lt;/code&gt; specialization, that goes into the &lt;em&gt;exact same multimethod that &lt;code&gt;core&lt;/code&gt; is using&lt;/em&gt;. There is basically a single &lt;code&gt;toString&lt;/code&gt; object in memory that all of those modules have a reference to.&lt;/p&gt;

&lt;p&gt;This works because &lt;code&gt;core&lt;/code&gt; is the first module that created it, and our other two modules are both importing it. So every place that&amp;#8217;s using &lt;code&gt;toString&lt;/code&gt; has a path of imports that ultimately traces back to the root in &lt;code&gt;core&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;the-second-problem-colliding-imports&quot;&gt;The Second Problem: Colliding Imports&lt;/h2&gt;

&lt;p&gt;Problem solved, right? Well, not so fast. After I did this, I ran into the next issue. Here&amp;#8217;s another example. We&amp;#8217;ve got these two modules:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// pal.mag&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;defclass&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pal&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// pet.mag&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;defclass&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pet&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;They both define classes that have &lt;code&gt;name&lt;/code&gt; fields. That means they both implicitly create &lt;code&gt;name&lt;/code&gt; methods. Then we use them:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// main.mag&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pal&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pet&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;who&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Hi, &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;who&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Pal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Fred&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Pet&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Rex&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The intent here is clear: &lt;code&gt;greet()&lt;/code&gt; should be able to print anything that has a &lt;code&gt;name&lt;/code&gt; getter. What happens if we run this with our current semantics? It turns out we don&amp;#8217;t get very far.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;import Pal&lt;/code&gt; line works fine. It brings &lt;code&gt;Pal&lt;/code&gt; and &lt;code&gt;name&lt;/code&gt; into &lt;code&gt;main.mag&lt;/code&gt;. Then the second import comes along. It defines &lt;code&gt;Pet&lt;/code&gt; fine, but there&amp;#8217;s already a &lt;code&gt;name&lt;/code&gt; multimethod defined now. We have a name collision.&lt;/p&gt;

&lt;p&gt;We didn&amp;#8217;t have a name collision in the first example. Even though &lt;code&gt;toString&lt;/code&gt; got imported into &lt;code&gt;main.mag&lt;/code&gt; both from &lt;code&gt;core&lt;/code&gt; and &lt;code&gt;sandwich.mag&lt;/code&gt;, they were the exact same object, so we could just safely ignore it. Here, though, that isn&amp;#8217;t the case. The import graph is like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;text&quot;&gt;pal  pet
 ^    ^
  \  /
   \/
  main
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;There is no root module where a single &lt;code&gt;name&lt;/code&gt; multimethod is being created. Instead, &lt;code&gt;pal.mag&lt;/code&gt; and &lt;code&gt;pet.mag&lt;/code&gt; both create their own unrelated &lt;code&gt;name&lt;/code&gt; multimethods. When &lt;code&gt;main.mag&lt;/code&gt; tries to import them both, they aren&amp;#8217;t the same object, so they collide.&lt;/p&gt;

&lt;h2 id=&quot;the-second-failed-solution-just-deal-with-it&quot;&gt;The Second (Failed) Solution: Just Deal With It&lt;/h2&gt;

&lt;p&gt;My first stab at &amp;#8220;fixing&amp;#8221; this was to just declare those semantics are How It Should Be. If you have colliding method names, you just rename on import, like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// main.mag&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pal&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pal.name&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pet&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pet.name&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That works, but it&amp;#8217;s really ugly. I found in practice that method collisions were surprisingly common, almost always coming from fields with simple names like &lt;code&gt;name&lt;/code&gt;. Having to qualify those at every use site sucks:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;greet(who)
    print(&quot;Hi, &quot; + who pal.name + &quot;!&quot;)
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Worse, it breaks &lt;a href=&quot;http://en.wikipedia.org/wiki/Duck_typing&quot;&gt;duck typing&lt;/a&gt;. Using this scheme, there&amp;#8217;s no way to create a single &lt;code&gt;greet()&lt;/code&gt; method that works on both pets and pals since there isn&amp;#8217;t a single &lt;code&gt;name&lt;/code&gt; method it can call on both.&lt;/p&gt;

&lt;h2 id=&quot;the-third-failed-solution-do-something-really-complex&quot;&gt;The Third (Failed) Solution: Do Something Really Complex&lt;/h2&gt;

&lt;p&gt;At first, I tried to fix this by doing some hairy method merging when you imported. If you had a method name collision on import, it would create a new local multimethod object that contained all of the specializations of both of the ones you&amp;#8217;re importing. That fixes the above problem. &lt;code&gt;main.mag&lt;/code&gt; will end up with one &lt;code&gt;name&lt;/code&gt; method that can accept both pals and pets.&lt;/p&gt;

&lt;p&gt;But it unfixes the &lt;em&gt;first&lt;/em&gt; problem. If you import a multimethod (like &lt;code&gt;toString&lt;/code&gt; in the first example) and then add a new specialization, you need to push that specialization back &lt;em&gt;up&lt;/em&gt; to the modules that you imported.&lt;/p&gt;

&lt;p&gt;So to get that working, I made it track where you had all of the modules you had imported a multimethod from. When you defined a new specialization, that would get pushed back up to those modules too.&lt;/p&gt;

&lt;p&gt;This did sort of work, but it was complex, felt brittle, and was hard to reason about.&lt;/p&gt;

&lt;h2 id=&quot;the-fourth-solution-go-global&quot;&gt;The Fourth Solution: Go Global&lt;/h2&gt;

&lt;p&gt;I spent a lot of time thinking about it and finally asked if there was a radically simpler answer that would work. One came to mind: &lt;strong&gt;make methods &lt;em&gt;globally&lt;/em&gt; scoped.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Any time you define a method, in &lt;em&gt;any&lt;/em&gt; module, it would just go into a single global pool of multimethods that all modules share. Overriding works, because you&amp;#8217;re &lt;em&gt;always&lt;/em&gt; overriding: there is only a single multimethod with a given name across the entire program. Duck typing works for the exact same reason.&lt;/p&gt;

&lt;p&gt;But global scope is &lt;a href=&quot;http://c2.com/cgi/wiki?GlobalVariablesAreBad&quot;&gt;bad&lt;/a&gt;, right? I felt like I was committing some cardinal sin by even contemplating this. After much gnashing of teeth, I concocted a rationalization for why this might not be so bad. Here goes&amp;#8230;&lt;/p&gt;

&lt;p&gt;Consider your favorite conventional OOP language. We&amp;#8217;ll do Ruby because it looks nice here:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Cow&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;speak&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;moo&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Dog&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;speak&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;woof&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We have two classes that both define &lt;code&gt;speak&lt;/code&gt; methods. We don&amp;#8217;t think of &lt;code&gt;speak&lt;/code&gt; as being &amp;#8220;global&amp;#8221; here because you get to the &lt;code&gt;speak&lt;/code&gt; methods &lt;em&gt;through&lt;/em&gt; an instance of &lt;code&gt;Cow&lt;/code&gt; or &lt;code&gt;Dog&lt;/code&gt;. Once you do, the method will correctly be associated with the right type and do the right thing. You can&amp;#8217;t call &lt;code&gt;Dog&lt;/code&gt;&amp;#8217;s &lt;code&gt;speak&lt;/code&gt; on an instance of &lt;code&gt;Cow&lt;/code&gt; or some other unrelated type. Likewise, if you don&amp;#8217;t have a dog or a cow, you can&amp;#8217;t get to &lt;code&gt;speak&lt;/code&gt; at all.&lt;/p&gt;

&lt;p&gt;Now consider the Magpie equivalent, and assume that multimethods are globally scoped:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;defclass&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cow&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;speak&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;moo&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;defclass&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dog&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;speak&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;woof&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;How does it compare? Like the Ruby solution, you can&amp;#8217;t call &lt;code&gt;Cow&lt;/code&gt;&amp;#8217;s &lt;code&gt;speak&lt;/code&gt; method using a &lt;code&gt;Dog&lt;/code&gt; or some other type. If you don&amp;#8217;t have a dog or a cow, you can&amp;#8217;t get to &lt;code&gt;speak&lt;/code&gt; at all.&lt;/p&gt;

&lt;p&gt;What this means is that the &lt;em&gt;multi&lt;/em&gt;method is globally scoped. But actual specializations, the stuff you care about, functionally aren&amp;#8217;t. Since they are specialized to types, if you don&amp;#8217;t have an object of that type, you can&amp;#8217;t get to the method. The multimethod is sitting there in global scope where anyone can get to it, but unless you have the key (an object of the right class), you can&amp;#8217;t crack it open and get at the methods.&lt;/p&gt;

&lt;p&gt;In other words, globally scoped multimethods in Magpie are isomorphic to methods in a single dispatch language. Actually, that&amp;#8217;s only half true. Single-dispatch languages only support a &lt;em&gt;subset&lt;/em&gt; of what multimethods let you do. Magpie can also express a bunch of stuff that single-dispatch languages can&amp;#8217;t.&lt;/p&gt;

&lt;h2 id=&quot;better-than-monkey-patching&quot;&gt;Better than Monkey-Patching&lt;/h2&gt;

&lt;p&gt;Multimethods match on &lt;em&gt;all&lt;/em&gt; of their arguments, not just the &amp;#8220;receiver&amp;#8221; on the left-hand side. Let&amp;#8217;s go back to our Ruby example. Say we need to serialize Dogs and Cows to a stream. Since classes in Ruby are open for extension, we can do:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Dog&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;serialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;dog&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Cow&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;serialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;cow&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Swell. But then someone else on our team gets tasked with making dogs and cows support serializing to XML. If they aren&amp;#8217;t aware of &lt;em&gt;our&lt;/em&gt; monkey-patch and add:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Dog&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;serialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xmlWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;xmlWriter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;lt;dog&amp;gt;&amp;lt;/dog&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Cow&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;serialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xmlWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;xmlWriter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;lt;cow&amp;gt;&amp;lt;/cow&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Then those &lt;code&gt;serialize&lt;/code&gt; methods will obliterate the ones for writing to a stream. Or maybe the other way around. It depends on whose code gets to run last. Nasty.&lt;/p&gt;

&lt;p&gt;Meanwhile, in Magpie:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;magpie&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dog&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;dog&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;cow&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dog&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;XmlWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;dog&amp;gt;&amp;lt;/dog&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;XmlWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;cow&amp;gt;&amp;lt;/cow&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Here, there is no collision at all. When you call &lt;code&gt;write&lt;/code&gt;, it will look at both the left-hand argument (a &lt;code&gt;Cow&lt;/code&gt; or a &lt;code&gt;Dog&lt;/code&gt;) &lt;em&gt;and&lt;/em&gt; the right-hand side (a &lt;code&gt;Stream&lt;/code&gt; or an &lt;code&gt;XmlWriter&lt;/code&gt;) and pick the one perfect method for those types.&lt;/p&gt;

&lt;p&gt;This means you can effectively &amp;#8220;monkey-patch&amp;#8221; with much finer-grained control and less chance of collision. The entire argument signature forms a key that&amp;#8217;s used to select the right method. If you don&amp;#8217;t want people to accidentally hit your specific method, just ensure that it requires an argument of a type that&amp;#8217;s hard to get a hold of.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <id>http://journal.stuffwithstuff.com/2012/01/24/higher-order-macros-in-c</id>
    <link type="text/html" rel="alternate" href="http://journal.stuffwithstuff.com/2012/01/24/higher-order-macros-in-c"/>
    <title>Higher Order Macros in C++</title>
    <published>2012-01-24T00:00:00-08:00</published>
    <updated>2012-01-24T00:00:00-08:00</updated>
    <author>
      <name>Robert Nystrom</name>
      <uri>http://journal.stuffwithstuff.com/</uri>
    </author>
    <content type="html">&lt;p&gt;My hobby project lately has been working on a little &lt;a href=&quot;https://github.com/munificent/magpie&quot;&gt;bytecode interpreter&lt;/a&gt; for &lt;a href=&quot;http://magpie-lang.org/&quot;&gt;Magpie&lt;/a&gt; in C++. As an ex-game programmer, I&amp;#8217;m pretty sad at how rusty my C++ has gotten. To try to make things a bit easier on myself, I&amp;#8217;ve been borrowing from the masters whenever possible. That means I usually have &lt;a href=&quot;http://code.google.com/p/v8/&quot;&gt;v8&lt;/a&gt;&amp;#8217;s source code open in another window.&lt;/p&gt;

&lt;p&gt;(Aside: if you&amp;#8217;re interested in programming languages, it is &lt;em&gt;so awesome&lt;/em&gt; that implementations like v8 and &lt;a href=&quot;http://hg.mozilla.org/mozilla-central/file/1982c882af0f/js/src&quot;&gt;spidermonkey&lt;/a&gt; are open source and just a click away. Learning from them is a bit like taking your first martial arts lesson from an angry Bruce Lee, but it&amp;#8217;s still amazing that industry-leading codebases are just there waiting for your perusal.)&lt;/p&gt;

&lt;p&gt;In all honesty, my usual process looks a bit like:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&amp;#8220;Hmm, I need to code up a floobinator. v8 has one. Let me see how they do it.&amp;#8221;&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;hunt through v8 code&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;&amp;#8220;Ah, here it is.&amp;#8221;&lt;/li&gt;
  &lt;li&gt;OH GOD, WHAT WIZARDRY IS THIS.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;About 90% of it is &lt;a href=&quot;http://code.google.com/p/v8/source/browse/trunk/src/ia32/codegen-ia32.cc#295&quot;&gt;over my head&lt;/a&gt;, but I figure 10% of v8 is still a pretty good chunk of smart. There is one clever technique I learned from them that I &lt;em&gt;do&lt;/em&gt; understand: macros that take macros as arguments. That&amp;#8217;s the point of this post.&lt;/p&gt;

&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;

&lt;p&gt;C++ is a pretty powerful language for defining abstractions which let you get rid of redundancy. Functions and methods address duplicate chunks of imperative code. Base classes let you reuse data definitions. Templates let you do&amp;#8230; well&amp;#8230; &lt;a href=&quot;http://crazycpp.wordpress.com/category/template-metaprogramming/&quot;&gt;almost anything&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Even so, there&amp;#8217;s still often hunks of repetition that you can&amp;#8217;t seem to eliminate. For example, let&amp;#8217;s say we&amp;#8217;re working with a language&amp;#8217;s syntax. Typically, the &lt;a href=&quot;https://github.com/munificent/finch/blob/master/src/Syntax/Parser.h&quot;&gt;parser&lt;/a&gt; generates an &lt;a href=&quot;http://en.wikipedia.org/wiki/Abstract_syntax_tree&quot;&gt;AST&lt;/a&gt; which then gets passed to the &lt;a href=&quot;https://github.com/munificent/finch/blob/master/src/Compiler/Compiler.h&quot;&gt;compiler&lt;/a&gt;. The compiler walks the AST using &lt;a href=&quot;http://en.wikipedia.org/wiki/Visitor_pattern&quot;&gt;Ye Olde Visitor Patterne&lt;/a&gt; and generates some &lt;a href=&quot;https://github.com/munificent/finch/blob/master/src/Compiler/Block.h&quot;&gt;lower-level representation&lt;/a&gt; for it.&lt;/p&gt;

&lt;p&gt;Depending on how rich your language is, you&amp;#8217;ll have quite a few different AST classes to represent the different syntactic elements: literals, unary operators, infix expressions, statements, flow control, definitions, etc. V8, for example, has &lt;a href=&quot;http://code.google.com/p/v8/source/browse/trunk/src/ast.h#123&quot;&gt;40&lt;/a&gt; classes to cover everything you can express in JavaScript.&lt;/p&gt;

&lt;p&gt;These are relatively simple types. A (greatly!) simplified one looks a bit like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BinaryOpExpr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;BinaryOpExpr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&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;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&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;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AstVisitor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visitor&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;n&quot;&gt;visitor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;visitBinaryOpExpr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&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;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&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;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&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;nl&quot;&gt;private:&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&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;p&gt;Imagine thirty-something-odd more classes like this and you&amp;#8217;ve got the right idea. There isn&amp;#8217;t &lt;em&gt;too&lt;/em&gt; much we can do in C++ to simplify these definitions themselves. Each class is different enough that it&amp;#8217;s simplest and clearest to just write them out.&lt;/p&gt;

&lt;p&gt;Where the tedium really comes in is all of the surrounding code that &lt;em&gt;uses&lt;/em&gt; these classes. First up is the aforementioned &lt;a href=&quot;http://code.google.com/p/v8/source/browse/trunk/src/ast.h#2151&quot;&gt;visitor&lt;/a&gt;. To make it easy for the compiler to dispatch to different code based on the different AST classes, you&amp;#8217;ll typically define a class like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AstVisitor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;public:&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AstVisitor&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;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visitBoolLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BoolLiteral&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr&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;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visitNumLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NumLiteral&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr&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;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visitStringLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StringLiteral&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr&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;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visitUnaryOpExpr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UnaryOpExpr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr&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;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visitBinaryOpExpr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BinaryOpExpr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr&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;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visitAssignmentExpr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AssignmentExpr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr&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;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visitConditionalExpr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConditionalExpr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr&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;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visitIfThenStmt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IfThenStmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr&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;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 30 more of these, you get the idea...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That code really &lt;em&gt;is&lt;/em&gt; just repetitive boilerplate. There&amp;#8217;s more. It&amp;#8217;s useful to also have an enum for each AST node type so that we can also &lt;code&gt;switch&lt;/code&gt; directly on the type of a node without having to go through a visitor for everything. So you&amp;#8217;ll want something like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AstType&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;kBoolLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;kNumLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;kStringLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;kUnaryOpExpr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;kBinaryOpExpr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// again, you get the idea...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;For debugging, it&amp;#8217;s handy to be able to get a string representation for an AST node&amp;#8217;s type too:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;typeString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AstType&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&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;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&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;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kBoolLiteral&lt;/span&gt;:  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;BoolLiteral&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kIntLiteral&lt;/span&gt;:   &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;IntLiteral&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kNumLiteral&lt;/span&gt;:   &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;NumLiteral&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kUnaryOpExpr&lt;/span&gt;:  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;UnaryOpLiteral&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kBinaryOpExpr&lt;/span&gt;: &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;BinaryOpLiteral&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// yup...&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;p&gt;C++&amp;#8217;s usual abstraction facilities won&amp;#8217;t help us here: in all of these cases the repetition is in the &lt;em&gt;middle&lt;/em&gt; of some type definition or statement. C++ is really only designed to let you abstract over entire statements (by making functions) or types (by making templates or base classes).&lt;/p&gt;

&lt;h2 id=&quot;lets-get-dirty&quot;&gt;Let&amp;#8217;s Get Dirty&lt;/h2&gt;

&lt;p&gt;But there is, of course, one grease-covered rusty tool in the C and C++ toolbox that doesn&amp;#8217;t give a damn about actual syntactic elements: &lt;em&gt;the preprocessor.&lt;/em&gt; It doesn&amp;#8217;t even know what a statement is! It just sees chunks of text.&lt;/p&gt;

&lt;p&gt;More often than not, that fact makes it too blunt of an instrument to be wielded indiscriminately without risking bloodshed but here it&amp;#8217;s just what we need. In all of our problem examples, we want to be able to say &amp;#8220;for each AST node type, insert this chunk of code but with the node&amp;#8217;s class name inserted in it in a few places.&amp;#8221;&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s try doing something like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;cp&quot;&gt;#define DEFINE_VISIT(type)  \&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;    virtual void visit##type(type* expr) = 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;With this, we can simplify our visitor class to:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AstVisitor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;public:&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AstVisitor&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;n&quot;&gt;DEFINE_VISIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BoolLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;DEFINE_VISIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NumLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;DEFINE_VISIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StringLiteral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;DEFINE_VISIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UnaryOpExpr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;DEFINE_VISIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BinaryOpExpr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;DEFINE_VISIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AssignmentExpr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;DEFINE_VISIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConditionalExpr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;DEFINE_VISIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IfThenStmt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 30 more of these, you get the idea...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That&amp;#8217;s a &lt;em&gt;little&lt;/em&gt; better, I guess. But not really. This trick doesn&amp;#8217;t help at all with the enum, and only helps a little in &lt;code&gt;typeString()&lt;/code&gt;. The problem is that it&amp;#8217;s the &amp;#8220;for each AST type&amp;#8221; part of our problem where the repetition really is. It&amp;#8217;s the &lt;em&gt;loop itself&lt;/em&gt; we want to abstract over more than the loop &lt;em&gt;body&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;What we want is a macro that will &lt;em&gt;itself&lt;/em&gt; walk over all of the types, like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;cp&quot;&gt;#define AST_NODE_LIST   \&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;    BoolLiteral         \&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;    NumLiteral          \&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;    StringLiteral       \&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;    UnaryOpExpr         \&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;    ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;But of course, that one doesn&amp;#8217;t do anything useful. We don&amp;#8217;t want it to just expand to the type names themselves. It needs to do something with them. But that something is different for each problem area. We need it to take a parameter that is the chunk of code that we generate for each type. Like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;cp&quot;&gt;#define AST_NODE_LIST(code) \&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;    code(BoolLiteral)       \&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;    code(NumLiteral)        \&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;    code(StringLiteral)     \&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;    code(UnaryOpExpr)       \&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;    ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;macros-taking-macros-we-must-go-deeper&quot;&gt;Macros Taking Macros&amp;#8230; We Must Go Deeper&lt;/h2&gt;

&lt;p&gt;Now the fun part. When we use that &lt;code&gt;AST_NODE_LIST&lt;/code&gt; macro, what is that &lt;code&gt;code&lt;/code&gt; argument going to be? It needs to be a thing that&amp;#8217;s available at preprocess time, can take an argument, and can generate a chunk of code. That leaves only one answer: a macro.&lt;/p&gt;

&lt;p&gt;Until I saw this in V8, I didn&amp;#8217;t even know you &lt;em&gt;could&lt;/em&gt; pass macros to macros. But indeed you can. Using the &lt;code&gt;AST_NODE_LIST&lt;/code&gt; we just defined, our visitor becomes:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AstVisitor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;public:&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AstVisitor&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;err&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEFINE_VISIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  \
  &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visit&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;##&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr&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;0&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;AST_NODE_LIST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEFINE_VISIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;undef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEFINE_VISIT&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Clean it up since we&amp;#39;re done with it.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;When &lt;code&gt;AST_NODE_LIST&lt;/code&gt; is expanded, it will expand to one call to &lt;code&gt;DEFINE_VISIT&lt;/code&gt; for each of the AST node types. Then &lt;em&gt;those&lt;/em&gt; will in turn be expanded to define the visitor method for that type.&lt;/p&gt;

&lt;p&gt;Likewise, our enum becomes:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;cp&quot;&gt;#define DEFINE_ENUM_TYPE(type) k##type,&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AstType&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;AST_NODE_LIST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEFINE_ENUM_TYPE&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;cp&quot;&gt;#undef DEFINE_ENUM_TYPE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That&amp;#8217;s the whole thing in its entirety. Finally, the function for converting it to a string:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;cp&quot;&gt;#define DEFINE_TYPE_STRING(type) case k##type: return #type;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;typeString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AstType&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&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;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&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;n&quot;&gt;AST_NODE_LIST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEFINE_TYPE_STRING&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;cp&quot;&gt;#undef DEFINE_TYPE_STRING&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Note that we&amp;#8217;re using &lt;a href=&quot;http://gcc.gnu.org/onlinedocs/cpp/Stringification.html&quot;&gt;stringification&lt;/a&gt; and &lt;a href=&quot;http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html&quot;&gt;token pasting&lt;/a&gt; here to not just literally substitute in the AST node class&amp;#8217;s name, but also to use it as a string, or to build a larger name (like &lt;code&gt;kBinaryOpExpr&lt;/code&gt;) out of it.&lt;/p&gt;

&lt;p&gt;This gets rid of some tedious code, but it has another nice side-effect. If you later need to add a new node class, you just add it to &lt;code&gt;AST_NODE_LIST&lt;/code&gt;. Once it&amp;#8217;s there, every place that&amp;#8217;s using that will automatically pick it up. That way you don&amp;#8217;t have to remember to touch &lt;code&gt;AstVisitor&lt;/code&gt;, &lt;code&gt;AstType&lt;/code&gt; and &lt;code&gt;typeString()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I can&amp;#8217;t tell if this is crazy awesome, or just plain crazy, but it&amp;#8217;s not something you see every day. What other interesting stuff is hiding in the bowels of your favorite open source project?&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <id>http://journal.stuffwithstuff.com/2011/10/29/a-proposal-for-null-safety-in-dart</id>
    <link type="text/html" rel="alternate" href="http://journal.stuffwithstuff.com/2011/10/29/a-proposal-for-null-safety-in-dart"/>
    <title>A Proposal for Null-safety in Dart</title>
    <published>2011-10-29T00:00:00-07:00</published>
    <updated>2011-10-29T00:00:00-07:00</updated>
    <author>
      <name>Robert Nystrom</name>
      <uri>http://journal.stuffwithstuff.com/</uri>
    </author>
    <content type="html">&lt;p&gt;Page 75 of the current (0.04) draft of &lt;a href=&quot;http://www.dartlang.org/docs/spec/index.html&quot;&gt;the Dart language spec&lt;/a&gt; has this note in it:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Should we do something with respect to non-nullable types?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you asked me I&amp;#8217;d answer a resounding yes! (Alas, no one did, but that&amp;#8217;s never stopped me before.) So, here&amp;#8217;s my attempt at an answer. I&amp;#8217;m posting it here on my blog just to clarify that this is &lt;em&gt;my&lt;/em&gt; strawman proposal, and not something that&amp;#8217;s got any official Dart or Google stamp of approval on it.&lt;/p&gt;

&lt;h2 id=&quot;the-proposal-in-a-nutshell&quot;&gt;The Proposal In a Nutshell&lt;/h2&gt;

&lt;p&gt;By default, all types are non-nullable. If you declare a variable of type &lt;code&gt;int&lt;/code&gt;, it is a static error to try to assign &lt;code&gt;null&lt;/code&gt; to it. In checked mode, it is a dynamic error to assign &lt;code&gt;null&lt;/code&gt; to a non-nullable variable.&lt;/p&gt;

&lt;p&gt;You can define a nullable type by adding &lt;code&gt;?&lt;/code&gt; after the type name. &lt;code&gt;int&lt;/code&gt; is non-nullable, &lt;code&gt;int?&lt;/code&gt; is nullable. Non-null types are a property of Dart&amp;#8217;s type system, but (unlike &lt;a href=&quot;http://lukeplant.me.uk/blog/posts/null-pointers-vs-none-vs-maybe/&quot;&gt;&lt;code&gt;Maybe&lt;/code&gt;&lt;/a&gt; or &lt;a href=&quot;http://www.standardml.org/Basis/option.html&quot;&gt;&lt;code&gt;option&lt;/code&gt;&lt;/a&gt; in other languages) not a part of its runtime behavior.&lt;/p&gt;

&lt;p&gt;At runtime, Dart continues to work like a dynamic language where any variable may hold a value of any type, including &lt;code&gt;null&lt;/code&gt;. &amp;#8220;Nullability&amp;#8221; is as optional as the rest of Dart&amp;#8217;s type system.&lt;/p&gt;

&lt;h2 id=&quot;why-default-to-non-nullable&quot;&gt;Why Default to Non-nullable?&lt;/h2&gt;

&lt;p&gt;One of the first decisions a non-null proposal has to make is which is the default: nullable or non-nullable?&lt;/p&gt;

&lt;p&gt;I prefer defaulting to non-null for a couple of reasons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Most of the time, you don&amp;#8217;t want to allow &lt;code&gt;null&lt;/code&gt;. I went through
&lt;a href=&quot;https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/client/samples/swarm/SwarmViews.dart&quot;&gt;SwarmViews.dart&lt;/a&gt;, one of the largest
Dart source files that I&amp;#8217;m familiar with and annotated it for nullability.
I found 16 nullable variables or fields and 172 non-nullable ones. In other
words, defaulting to non-nullability there is right about 90% of the time.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;A very large number of type annotations are for atomic types (&lt;code&gt;int&lt;/code&gt;, &lt;code&gt;bool&lt;/code&gt;,
etc.) and people expect those to be non-nullable like they are in other
languages. It would be strange to have to go out of your way to say &amp;#8220;I want a &lt;em&gt;non-nullable&lt;/em&gt; bool&amp;#8221;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;In Dart, you don&amp;#8217;t have to provide a type annotation at all. It seems to me
then that if you&amp;#8217;ve chosen to go out of your way to say a variable is of
type &lt;code&gt;Foo&lt;/code&gt;, it should really be a &lt;code&gt;Foo&lt;/code&gt; and not a &lt;code&gt;Foo-or-null&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;semantics&quot;&gt;Semantics&lt;/h2&gt;

&lt;p&gt;A nullable type is essentially the &lt;a href=&quot;http://en.wikipedia.org/wiki/Type_system#Union_types&quot;&gt;union&lt;/a&gt; of the original type and the &lt;code&gt;Null&lt;/code&gt; type. In other words, &lt;code&gt;bool?&lt;/code&gt; contains all of the values of the &lt;code&gt;bool&lt;/code&gt; type (&lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;) as well as all of the values of the &lt;code&gt;Null&lt;/code&gt; type (&lt;code&gt;null&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Unlike option types, nullable types do not nest. &lt;code&gt;int??&lt;/code&gt; is equivalent to &lt;code&gt;int?&lt;/code&gt;. This follows naturally from thinking of it as a union of types. &lt;code&gt;true | false | null&lt;/code&gt; is an equivalent set to &lt;code&gt;true | false | null | null&lt;/code&gt;. The redundant &lt;code&gt;null&lt;/code&gt;s just collapse.&lt;/p&gt;

&lt;h2 id=&quot;subtyping&quot;&gt;Subtyping&lt;/h2&gt;

&lt;p&gt;A non-null type is a subtype of its nullable type. In other words, you can always pass a &lt;code&gt;Foo&lt;/code&gt; to something that expects a &lt;code&gt;Foo?&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also, the special &lt;code&gt;Null&lt;/code&gt; type is a subtype of every nullable type. This means you can initialize nullable types with &lt;code&gt;null&lt;/code&gt; like you&amp;#8217;d expect. Since &lt;code&gt;Null&lt;/code&gt; is &lt;em&gt;not&lt;/em&gt; a subtype of &lt;em&gt;non&lt;/em&gt;-nullable types, you &lt;em&gt;cannot&lt;/em&gt; initialize those with &lt;code&gt;null&lt;/code&gt;. That implies that all variables of non-null types must be initialized. This is an error:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As is this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Point&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;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Point&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;p&gt;Requiring fields of non-nullable types to be initialized is a challenge in languages like Java and Scala where you can access a field before it&amp;#8217;s been initialized in the constructor. Fortunately, Dart already has constructor initialization lists, and they solve this problem handily. We can fix that &lt;code&gt;Point&lt;/code&gt; class like so:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Point&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;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Point&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;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&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;p&gt;Since the constructor initializers are run before you can access &lt;code&gt;this&lt;/code&gt;, we&amp;#8217;ve ensured that those fields will be initialized before &lt;em&gt;anyone&lt;/em&gt; can see them. Neat.&lt;/p&gt;

&lt;h2 id=&quot;working-with-nullable-types&quot;&gt;Working with Nullable Types&lt;/h2&gt;

&lt;p&gt;One challenge with nullable types is that working with them can be tedious. With things like &lt;a href=&quot;http://en.wikipedia.org/wiki/Option_type&quot;&gt;Maybe in Haskell and &lt;code&gt;option&lt;/code&gt; in ML&lt;/a&gt;, you have to manually unwrap the &amp;#8220;nullable&amp;#8221; value to get at the delicious real value hidden inside.&lt;/p&gt;

&lt;p&gt;This proposal doesn&amp;#8217;t have that problem (granted, it also doesn&amp;#8217;t have some of the benefits of option types). It tries to stick close to Dart&amp;#8217;s dynamic sensibilities, so there are no option-like values that you have to extract the real value from using pattern matching.&lt;/p&gt;

&lt;p&gt;Instead we rely on the fact that a variable can hold a value of any type. A nullable type just means the type checker won&amp;#8217;t yell at you if it knows &lt;code&gt;null&lt;/code&gt; is one of those values.&lt;/p&gt;

&lt;p&gt;To test for null, you can just do a vanilla &lt;code&gt;if (foo == null)&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;To go back and forth between nullable and non-nullable types, we rely on assignment compatibility. Dart&amp;#8217;s assignment compatibility rules are looser than other languages. Like most, they allow implicit upcasts. Given this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Base&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Derived&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Then this is allowed:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;But Dart also allows implicit &lt;em&gt;downcasts&lt;/em&gt; (in other words from supertype to subtype):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// declared as type Base above&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;(The reasoning here is that many times a downcast &lt;em&gt;will&lt;/em&gt; work correctly at runtime, and Dart&amp;#8217;s type system is optimistic that you know what you&amp;#8217;re doing.)&lt;/p&gt;

&lt;p&gt;Thanks to this, nullable types are easy to work with. It is, of course, always safe to go from a non-null type to a nullable one:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&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;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;123&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;But it&amp;#8217;s equally easy (though not equally &lt;em&gt;safe&lt;/em&gt;) to go in the other direction:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// declared as int? above&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If you actually want to be safe, you just need to check for null first:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&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;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// safe at runtime now too!&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So here, as in other places in Dart, the type checker won&amp;#8217;t get in your way.&lt;/p&gt;

&lt;h2 id=&quot;wait-what-good-is-it&quot;&gt;Wait, What Good is It?&lt;/h2&gt;

&lt;p&gt;You couldn&amp;#8217;t ask for a less intrusive null-tracking system, but it seems a little &lt;em&gt;too&lt;/em&gt; unintrusive. Isn&amp;#8217;t silently assigning from a nullable type to a non-nullable one the exact thing we&amp;#8217;d want the type checker to flag?&lt;/p&gt;

&lt;p&gt;Well, yes, sort of. But Dart&amp;#8217;s specified static checker isn&amp;#8217;t that strict in general. It also doesn&amp;#8217;t flag implicit downcasts, which this is just a special case of. Even without that, I think we still get a lot of mileage out of this:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Gilad describes Dart as having a &amp;#8220;documentary&amp;#8221; type system. With nullable
and non-nullable types, we can now use type annotations to tell users of our APIs which things accept &lt;code&gt;null&lt;/code&gt; and which don&amp;#8217;t. In other languages you
have no choice but to spell that out in the documentation. With this, it&amp;#8217;s
right in the type signature for a method. If you call &lt;code&gt;method(Foo a, Foo? b)&lt;/code&gt;, it&amp;#8217;s clear that it can handle a &lt;code&gt;null&lt;/code&gt; for the second argument but not the first.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;In checked mode, if you try to assign &lt;code&gt;null&lt;/code&gt; to a non-nullable type, it
&lt;em&gt;will&lt;/em&gt; flag that as an error. This means you&amp;#8217;ll know when a &lt;code&gt;null&lt;/code&gt; snuck in
&lt;em&gt;as soon as it happens&lt;/em&gt;, and not later on in the program when you accidentally try to call a method on it. That is, I think, a big part of
what you want null-checking for.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;As I mentioned before, it ensures non-nullable types are initialized. Sure,
you can implicitly assign from a nullable type to a non-nullable one, but
what you can&amp;#8217;t do is implicitly assign from &lt;em&gt;null&lt;/em&gt; to a non-nullable type.
This is a static error:&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;pre&gt;&lt;code&gt;That goes a long way towards flushing out uninitialized variable bugs. In
my experience with Dart, that's one of the most common errors I run into.
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
  &lt;li&gt;While the specified type check behavior allows implicit downcasting, tools
are encouraged to do their own smarter analysis. If we get nullability
annotations in Dart, it will be easy to have tools do data flow analysis and report warnings like this:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&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;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// WARN that we didn&amp;#39;t test i for null first&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;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&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;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// OK now&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;nullables-and-generics&quot;&gt;Nullables and Generics&lt;/h2&gt;

&lt;p&gt;A key question that comes up with nullables is how they play with generic types. I&amp;#8217;m not a type system expert, so it&amp;#8217;s entirely possible that I haven&amp;#8217;t thought this all the way through, but I think it falls out correctly from the subtyping between nullable and non-nullable types.&lt;/p&gt;

&lt;p&gt;Generics are covariant in Dart. That means that generics also allow subtyping between a nullable and non-nullable type argument. In other words, this is allowed:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&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;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As long as you use your types in a way that makes covariance safe (i.e. you read from them and don&amp;#8217;t write to them) this will work as well for nullable types as it does with subclassing.&lt;/p&gt;

&lt;p&gt;The other question is how using a type parameter in an annotation plays with nullability. I.e.:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SomeClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&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;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;someField&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;p&gt;This is &lt;em&gt;really&lt;/em&gt; beyond the edge of my type system fu, but I think the fact that nullable types flatten (&lt;code&gt;A??&lt;/code&gt; becomes &lt;code&gt;A?&lt;/code&gt;) will alleviate some of the nasty corner cases this may lead to. I&amp;#8217;m not sure though, and this is definitely something I&amp;#8217;d like feedback on.&lt;/p&gt;

&lt;h2 id=&quot;two-corner-cases&quot;&gt;Two Corner Cases&lt;/h2&gt;

&lt;p&gt;There are two types in Dart where nullability will work a little strangely: &lt;code&gt;Null&lt;/code&gt; and &lt;code&gt;Object&lt;/code&gt;. The former&amp;#8217;s behavior is pretty obviously weird. Given that a nullable type is the union of some type and &lt;code&gt;Null&lt;/code&gt;, there&amp;#8217;s no dinstinction between &lt;code&gt;Null&lt;/code&gt; and &lt;code&gt;Null?&lt;/code&gt; (since nullability flattens). Likewise, you can&amp;#8217;t have a non-nullable &lt;code&gt;Null&lt;/code&gt; type (since no possible values could inhabit it).&lt;/p&gt;

&lt;p&gt;The other case is a bit more surprising. You can&amp;#8217;t have a non-nullable &lt;code&gt;Object&lt;/code&gt; type. &lt;code&gt;Object&lt;/code&gt; is the &lt;a href=&quot;http://en.wikipedia.org/wiki/Top_type&quot;&gt;top type&lt;/a&gt; which means &lt;em&gt;every&lt;/em&gt; object is an instance of &lt;code&gt;Object&lt;/code&gt;, including &lt;code&gt;null&lt;/code&gt;. In practice, I think this works OK. If you have a variable of type &lt;code&gt;Object&lt;/code&gt;, the only operations you can perform on it are ones that all types support, like &lt;code&gt;toString()&lt;/code&gt;. Even &lt;code&gt;null&lt;/code&gt; supports those, so a non-nullable &lt;code&gt;Object&lt;/code&gt; type isn&amp;#8217;t needed to avoid type errors.&lt;/p&gt;

&lt;p&gt;That covers most of the implications of the proposal as far as I can tell. To get more concrete, let&amp;#8217;s see how we&amp;#8217;d need to modify the spec and libs to support it:&lt;/p&gt;

&lt;h2 id=&quot;spec-changes&quot;&gt;Spec Changes&lt;/h2&gt;

&lt;p&gt;I definitely don&amp;#8217;t have the skills Gilad has at specifying these semantics precisely, but I&amp;#8217;ll at least make an honest try. Most of the changes, as you&amp;#8217;ll see, are simplifications. Null shows up as a special case in much of the spec. One of the reasons I like this proposal is that it eliminates most of those.&lt;/p&gt;

&lt;h3 id=&quot;section-102&quot;&gt;Section 10.2&lt;/h3&gt;

&lt;p&gt;Change:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The static type of null is ⊥.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;to:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The static type of null is Null.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;section-1016-assignment&quot;&gt;Section 10.16 Assignment&lt;/h3&gt;

&lt;p&gt;In both:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In checked mode, it is a dynamic type error if o is not null and the interface induced by the class of o is not a subtype of the actual type (13.8.1) of v.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In checked mode, it is a dynamic type error if o is not null and the interface induced by the class of o is not a subtype of the static type of C.v.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;remove &amp;#8220;o is not null and&amp;#8221;.&lt;/p&gt;

&lt;h3 id=&quot;section-10132-binding-actuals-to-formals&quot;&gt;Section 10.13.2 Binding Actuals to Formals&lt;/h3&gt;

&lt;p&gt;From:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In checked mode, it is a dynamic type error if oi is not null and the actual type (13.8.1) of pi is not a supertype of the type of oi,i ∈ 1..m.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;remove &amp;#8220;oi is not null and&amp;#8221;. And from:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In checked mode, it is a dynamic type error if om+j is not null and the actual type (13.8.1) of qj is not a supertype of the type of om+j,j ∈ 1..l.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;remove &amp;#8220;om+j is not null and&amp;#8221;.&lt;/p&gt;

&lt;h3 id=&quot;section-119-try&quot;&gt;Section 11.9 Try&lt;/h3&gt;

&lt;p&gt;From:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A catch clause catch (T1 p1, T2 p2) s matches an object o if o is null or if the type of o is a subtype of T1.&amp;#8221;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;remove &amp;#8220;o is null or&amp;#8221;.&lt;/p&gt;

&lt;p&gt;Then we need to add a section:&lt;/p&gt;

&lt;h3 id=&quot;nullable-types&quot;&gt;13.9 Nullable Types&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;A &lt;em&gt;nullable type&lt;/em&gt; is a parameterized type that allows both values of the type parameter&amp;#8217;s type or the singleton value of type &lt;em&gt;Null&lt;/em&gt;, &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

  &lt;p&gt;Let &lt;em&gt;A?&lt;/em&gt; be the nullable type of type &lt;em&gt;A&lt;/em&gt;. Nullable types do not nest or wrap: &lt;em&gt;B??&lt;/em&gt; is equivalent to &lt;em&gt;B?&lt;/em&gt; for any type &lt;em&gt;B&lt;/em&gt;. The subtype relations are:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;em&gt;A&lt;/em&gt; &amp;lt;: &lt;em&gt;A?&lt;/em&gt; (non-nullable is a subtype of a nullable)&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;Null&lt;/em&gt; &amp;lt;: &lt;em&gt;A?&lt;/em&gt; (non-nullable is also a subtype of Null)&lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;It is a static error to declare a variable of a non-nullable type without giving it an initializer whose static type is assignment compatible with the variable&amp;#8217;s type.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;corelib-changes&quot;&gt;Corelib Changes&lt;/h2&gt;

&lt;p&gt;This proposal implies some modification to the corelib APIs to be null-aware. There are two kinds of changes we&amp;#8217;d need: minor changes to type annotations and a few actual semantic changes.&lt;/p&gt;

&lt;h3 id=&quot;type-annotation-changes&quot;&gt;Type Annotation Changes&lt;/h3&gt;

&lt;p&gt;The minor change is that a few type annotations need to be tweaked. Operations that are defined to return something &amp;#8220;or null&amp;#8221; need to be made explicitly nullable.&lt;/p&gt;

&lt;p&gt;For example, the &lt;a href=&quot;http://www.dartlang.org/docs/api/Map.html#Map::Map&quot;&gt;&lt;code&gt;Map&amp;amp;lt;K,V&amp;amp;gt;&lt;/code&gt; interface&lt;/a&gt; has a subscript operator (i.e. &lt;code&gt;map[key]&lt;/code&gt;) that returns &lt;code&gt;null&lt;/code&gt; if the key isn&amp;#8217;t found. Right now it&amp;#8217;s return type is declared to be &lt;code&gt;V&lt;/code&gt;, the value type. It will need to be &lt;code&gt;V?&lt;/code&gt; instead to explicitly permit that &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Likewise, there are a number of methods that take named optional parameters whose types aren&amp;#8217;t nullable, like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Expect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isFalse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;actual&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;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reason&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// more...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Here, that &lt;code&gt;String&lt;/code&gt; will need to be made nullable.&lt;/p&gt;

&lt;h3 id=&quot;semantic-changes&quot;&gt;Semantic Changes&lt;/h3&gt;

&lt;p&gt;The more intrusive change is that operations that implicitly create &lt;code&gt;null&lt;/code&gt; entries in collections might need to be modified. For example, if you do:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&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;m&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;then you create a list whose four elements are automatically set to &lt;code&gt;null&lt;/code&gt;. That&amp;#8217;s a problem here because the element type (&lt;code&gt;int&lt;/code&gt;) is non-nullable.&lt;/p&gt;

&lt;p&gt;The safest way to handle this is to change the constructor to let the user explicitly provide the value to initialize the new entries with. So instead of the above, you could do:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&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;m&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;fill:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// [1, 1, 1, 1]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;or maybe:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&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;generate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;4&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;n&quot;&gt;index&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;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// [0, 2, 4, 6]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I may have missed something, but I believe &lt;code&gt;List&lt;/code&gt; is the only type that needs any real API changes like this.&lt;/p&gt;

&lt;h2 id=&quot;super-summmary&quot;&gt;Super Summmary&lt;/h2&gt;

&lt;p&gt;If you made it this far, you deserve a reward. Sadly, the best I can offer is a bullet list of what you&amp;#8217;ve already read:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;All types are non-nullable by default.&lt;/li&gt;
  &lt;li&gt;You define a nullable type by adding &lt;code&gt;?&lt;/code&gt; after the type.&lt;/li&gt;
  &lt;li&gt;Nested nullable types flatten: &lt;code&gt;A?? -&amp;gt; A?&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;A non-nullable type is a subtype of its nullable type.&lt;/li&gt;
  &lt;li&gt;No nullable &lt;em&gt;values&lt;/em&gt;: no &lt;code&gt;option&lt;/code&gt; or &lt;code&gt;Maybe&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Can assign back and forth between nullable and non-nullable types.&lt;/li&gt;
  &lt;li&gt;Must initialize non-nullable variables to non-null values.&lt;/li&gt;
  &lt;li&gt;Lets types document which things expect null.&lt;/li&gt;
  &lt;li&gt;Catches invalid nulls early in checked mode.&lt;/li&gt;
  &lt;li&gt;Most spec changes are just removing &amp;#8220;or null&amp;#8221;.&lt;/li&gt;
  &lt;li&gt;Most corelib changes are making some annotations nullable.&lt;/li&gt;
  &lt;li&gt;Probably want to change &lt;code&gt;List&lt;/code&gt; constructor to avoid implicit null fill values.&lt;/li&gt;
  &lt;li&gt;This proposal is awesome.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do I have that right? Thoughts?&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <id>http://journal.stuffwithstuff.com/2011/10/21/wrapping-my-head-around-optional-typing</id>
    <link type="text/html" rel="alternate" href="http://journal.stuffwithstuff.com/2011/10/21/wrapping-my-head-around-optional-typing"/>
    <title>Wrapping My Head Around Optional Typing</title>
    <published>2011-10-21T00:00:00-07:00</published>
    <updated>2011-10-21T00:00:00-07:00</updated>
    <author>
      <name>Robert Nystrom</name>
      <uri>http://journal.stuffwithstuff.com/</uri>
    </author>
    <content type="html">&lt;p&gt;One of the really cool parts about being involved with &lt;a href=&quot;http://dartlang.org&quot;&gt;Dart&lt;/a&gt; is that I get a lot of first-hand experience with an optionally-typed language. I&amp;#8217;ve been &lt;a href=&quot;/2010/08/31/type-checking-a-dynamic-language/&quot;&gt;fascinated&lt;/a&gt; &lt;a href=&quot;/2010/09/01/a-type-checking-conundrum/&quot;&gt;by&lt;/a&gt; &lt;a href=&quot;/2010/10/29/bootstrapping-a-type-system/&quot;&gt;optional typing&lt;/a&gt; for a while, but there are few opportunities to actually try it out on non-trivial code. With Dart, I get to use an optionally-typed language whose lineage goes right back to &lt;a href=&quot;http://www.strongtalk.org/&quot;&gt;one of the original wellsprings&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What I found was that it was surprisingly hard to wrap my head around. I initially considered it just sort of halfway between dynamic and static typing, like a 50/50 blend. It turns out, I think, that is more different than that. If there is a line between dynamic languages and static ones, optionally typed ones aren&amp;#8217;t on that line. They float off in their own axis &lt;a href=&quot;http://en.wikipedia.org/wiki/Complex_plane&quot;&gt;like imaginary numbers&lt;/a&gt;. In fact, I think they go off in &lt;em&gt;multiple&lt;/em&gt; axes.&lt;/p&gt;

&lt;p&gt;Before I go into how I &lt;em&gt;think&lt;/em&gt; about them, I should probably lay down the basic semantics. Here&amp;#8217;s the super science breakdown on how optional types work in Dart. If you want a more, uh, professional treatment, you can also check out &lt;a href=&quot;http://www.dartlang.org/articles/optional-types/&quot;&gt;Gilad&amp;#8217;s less rambling version&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;type-annotations-are-optional&quot;&gt;Type Annotations are Optional&lt;/h2&gt;

&lt;p&gt;At it&amp;#8217;s heart, Dart is a dynamically-typed language, so you can code without any annotations:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&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;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&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;p&gt;Here, you&amp;#8217;ve said that &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, and &lt;code&gt;result&lt;/code&gt; can be any type at all and that&amp;#8217;s OK. But you can also choose to provide a type annotation in all of the usual places: variable declarations, function parameters, or fields, and return types. So this is valid too:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kt&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&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;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&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;p&gt;Now we&amp;#8217;ve stated our intent that &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; be numbers. We&amp;#8217;ve also said that &lt;code&gt;sum&lt;/code&gt; should return a number. Note that we didn&amp;#8217;t annotate &lt;code&gt;result&lt;/code&gt;. It can still be anything. Dart lets you mix and match untyped and typed code, so here the result of a &lt;code&gt;a + b&lt;/code&gt; is assigned to an untyped variable. Likewise, we get the untyped &lt;code&gt;result&lt;/code&gt; and use it as the return value for a function with a typed return.&lt;/p&gt;

&lt;p&gt;This mixing and matching is important because it means you can gradually fold types into your code. You can leave them all off while you&amp;#8217;re prototyping and then start filling them in as you&amp;#8217;ve nailed down the design.&lt;/p&gt;

&lt;h3 id=&quot;tools-can-use-them&quot;&gt;Tools Can Use Them&lt;/h3&gt;

&lt;p&gt;OK, so you&amp;#8217;ve sprinkled some types through your code. Why bother? What do they do? The most basic &amp;#8220;feature&amp;#8221; that, surprisingly, does add value, is that they help document your code. Other people reading it can see what types you expect variables to be.&lt;/p&gt;

&lt;p&gt;The next step up on the scale of usefulness is that, since they&amp;#8217;re in the code, tools like IDEs, compilers, and linters are free to do whatever analysis they want using them. Dart does two favors for anyone writing an editor for it:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Types have a built-in declarative syntax.&lt;/strong&gt; Unlike JavaScript, Python and other dynamic languages, Dart doesn&amp;#8217;t use an &lt;em&gt;imperative&lt;/em&gt; syntax for defining types. The grammar for classes and interfaces is essentially static. That means an editor can figure out all of the methods a type supports just by parsing a source file. There&amp;#8217;s no runtime modifcation or monkey-patching that it needs to worry about.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Variables can have a known type.&lt;/strong&gt; If you choose to annotate them (or the editor can infer them), then it knows the type of a variable. If it knows the type, then thanks to the previous point, it knows what you can do with it. Ta-da: auto-complete and refactoring are now possible for a dynamic language. It can also do static type-checking like you get in most statically-typed languages.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;they-can-be-checked-at-runtime&quot;&gt;They Can Be Checked at Runtime&lt;/h3&gt;

&lt;p&gt;But tooling is gilding the lily. When you&amp;#8217;re talking about types, you expect, you know, a &lt;em&gt;type-checker&lt;/em&gt;. You have that too (at least in the VM&amp;mdash; what types mean when compiled to JS is another interesting story). When you run in checked mode, every type annotation gets checked at runtime. It&amp;#8217;s as if every line of code like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;someFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Turns into (more or less, simplifying things a bit) this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;someFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_temp&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;o&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;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Type error! Run for your life!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;You can think of every type annotation as an &lt;em&gt;expectation&lt;/em&gt;: this thing &lt;em&gt;should&lt;/em&gt; be a number here. In checked mode, the VM will constantly validate your expectations and stop if something doesn&amp;#8217;t hold.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s important to note that these checks are done &lt;em&gt;dynamically&lt;/em&gt;, at runtime. There isn&amp;#8217;t a separate static type checking pass. For example, if you have code like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&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;c1&quot;&gt;// should never get here&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;not int&amp;#39;&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;p&gt;You won&amp;#8217;t see a type error here because it will never actually get inside the &lt;code&gt;if&lt;/code&gt; block. You may be rightly wondering why in the hell you&amp;#8217;d want to wait until runtime to find a type error instead of doing it statically. Dart&amp;#8217;s take is that you can do both.&lt;/p&gt;

&lt;p&gt;Note that we said earlier that tools &lt;em&gt;can&lt;/em&gt; do static type-checking if they want. What Dart does is &lt;em&gt;also&lt;/em&gt; give you the option to perform those checks at runtime. This is actually how most static languages work too. Very few languages are &lt;em&gt;not&lt;/em&gt; unsound, and in the presence of unsoundness, they have to check at runtime. Consider this Java code:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;java&quot;&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;array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&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;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;untyped&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;untyped&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;123&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// not a string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Here we&amp;#8217;re creating an array of strings. Then we assign it to a variable whose type is an array of objects (i.e. anything). Then we try to stuff something that isn&amp;#8217;t a string (but &lt;em&gt;is&lt;/em&gt; an object) in it.&lt;/p&gt;

&lt;blockquote class=&quot;update&quot;&gt;
&lt;p&gt;&lt;em&gt;Update 2011/10/23:&lt;/em&gt; I was wrongly using an int array here. Changed it to &lt;code&gt;string[]&lt;/code&gt;. I didn't realize only arrays of reference types are covariant in Java.
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The static type checker won&amp;#8217;t catch this because &lt;a href=&quot;http://c2.com/cgi/wiki?JavaArraysBreakTypeSafety&quot;&gt;arrays are covariant in Java&lt;/a&gt;. That means that to ensure the last line doesn&amp;#8217;t crash your VM, it will do a &lt;em&gt;runtime&lt;/em&gt; check every time you set an element in an array to make sure it&amp;#8217;s the right type.&lt;/p&gt;

&lt;p&gt;There&amp;#8217;s another common case where you skirt around the static checker and rely on dynamic type tests: casts.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;java&quot;&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// It&amp;#39;s my callback, so I know the data is an int&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;There are times when you know more than the type system does and you just forcibly assert your knowledge. Doing so shouldn&amp;#8217;t let you just take down the VM (unlike in C++ where an improper type cast &lt;em&gt;can&lt;/em&gt; set your house on fire), so every cast does a runtime check too.&lt;/p&gt;

&lt;p&gt;Since Dart lets you mix untyped and typed code, it just embraces this model of validating at runtime more fully. Doing so has a couple of other advantages:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;You don&amp;#8217;t have to rely on your type system for security.&lt;/strong&gt; Java tries to
rely on the type system and bytecode verification to ensure that code can&amp;#8217;t
maliciously break the security guarantees of the VM. From what I&amp;#8217;ve heard,
doing so turned out to be unbelievably complicated.&lt;/p&gt;

    &lt;p&gt;Dart, on the other hand, can rely on a much simpler runtime model
(isolates) to ensure security boundaries.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;The type system can be less pessimistic.&lt;/strong&gt; Static type systems, by their
fundamental nature, are pessimistic. Since they don&amp;#8217;t know what &lt;em&gt;actual&lt;/em&gt;
code paths will be run at runtime and which &lt;em&gt;actual&lt;/em&gt; types a variable will
have, they err on the side of caution. They will report errors for code that
&lt;em&gt;may&lt;/em&gt; run, or variables that &lt;em&gt;may&lt;/em&gt; have the wrong type.&lt;/p&gt;

    &lt;p&gt;This is good for ensuring real errors don&amp;#8217;t get missed, but it&amp;#8217;s drag when
it reports false positives. Consider:&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;needle&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;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collection&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;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;needle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&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;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&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;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;pre&gt;&lt;code&gt;There's a type error here (according to most static type systems). We're
passing a `List&amp;lt;int&amp;gt;` to a function that takes a `List&amp;lt;Object&amp;gt;`, which
relies on [covariance](http://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29), but that
isn't statically safe. This `contains` method *could* call `collection.add(&quot;not an int&quot;)` and that would be an error.

However, it *doesn't actually do that*. It's using the collection in a way
that's perfectly safe with covariance. By loosening the type system, and
relying on dynamic checks to catch *actual* errors at runtime, we can
reduce the number of false positives that the type system chokes on.
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;they-can-be-ignored-at-runtime&quot;&gt;They Can be Ignored at Runtime&lt;/h3&gt;

&lt;p&gt;This is where things get weird. (Actually, if you&amp;#8217;re a type system person, they&amp;#8217;re already weird because covariant generics are wrong wrong wrong.) I&amp;#8217;ve been talking about checked mode, but there&amp;#8217;s another mode: production mode. In that mode, the type annotations &lt;em&gt;completely ignored&lt;/em&gt;. In other words, it will let you run this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;not int&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;not a bool either&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wtf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wtf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// not intnot a bool either&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This probably seems a little odd.&lt;/p&gt;

&lt;p&gt;&lt;img alt=&quot;Your type errors will blot out the sun! Then we will code in the shade.&quot; src=&quot;/image/2011/10/daaart.jpeg&quot; class=&quot;framed&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Maybe more than a little odd. In production mode, Dart behaves exactly as if it were a dynamically-typed language. Imagine if you decided to write your JavaScript like this (not that anyone would be &lt;a href=&quot;http://code.google.com/closure/compiler/docs/js-for-compiler.html&quot;&gt;crazy enough to do that&lt;/a&gt;):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* int */&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;not int&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* bool */&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;not a bool either&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* num */&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;wtf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;wtf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// not intnot a bool either&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Unsurprisingly, those comments won&amp;#8217;t do anything at runtime. That&amp;#8217;s how Dart runs in production mode.&lt;/p&gt;

&lt;h2 id=&quot;how-should-you-think-of-this&quot;&gt;How Should You Think of This?&lt;/h2&gt;

&lt;p&gt;OK, so what do we have? We have a nice little syntax for jamming type annotations into your program. If you use them, then tools can take advantage of that to help you work with your code. Also, in checked mode, the VM will validate them for you. But in production, they are ignored.&lt;/p&gt;

&lt;p&gt;That&amp;#8230; doesn&amp;#8217;t really sound much like a &amp;#8220;type system&amp;#8221; compared to other languages. In fact, if you try to think of it as a type system, it&amp;#8217;s pretty disappointing. It&amp;#8217;s more like some type&amp;#8230; stuff. Instead of thinking of it in terms of type systems, I tried to find something else I was familiar with from other languages that I could map it to. It finally clicked when someone at work referred to them as &lt;em&gt;type assertions&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now I get it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you&amp;#8217;ve done C or C++ programming, you&amp;#8217;ve probably used &lt;a href=&quot;http://en.wikipedia.org/wiki/Assertion_%28computing%29&quot;&gt;&lt;code&gt;assert()&lt;/code&gt;&lt;/a&gt; or some flavor of it. If not, it looks like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;divide&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;denom&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;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;denom&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;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;denom&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;p&gt;That &lt;code&gt;assert&lt;/code&gt; statement evaluates the condition in it. If it comes up false, then it stops the show and starts ringing the alarm bells. Actually, that&amp;#8217;s not entirely true. &lt;em&gt;If you run it in debug mode&lt;/em&gt; and the assertion fails, then it halts. Most projects have at least two build configurations.&lt;/p&gt;

&lt;p&gt;Debug is what developers use day in and day out. It has extra diagnostic stuff like symbol table information for debugging, and also includes all of the assertions. But there is also usually a &lt;em&gt;release&lt;/em&gt; mode. This is what you ship to customers and run in production. In that mode, the assertions are compiled out. They are erased completely.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sound familiar?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now why on Earth would you want to disable your asserts in release mode? Isn&amp;#8217;t that like wearing your life jacket in the boating classroom and then taking it off when you go out on the water?&lt;/p&gt;

&lt;p&gt;It turns out that removing your asserts at runtime actually has a few advantages:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;It&amp;#8217;s faster.&lt;/strong&gt; All of those assert conditions have to be executed and
checked at runtime. That can add a lot of runtime overhead. When I used to
be a game developer, debug builds of games typically ran much slower than
release. Playing a videogame running at four frames a second is a strange
skill to cultivate.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;The app should try its hardest to continue.&lt;/strong&gt; Once your program is in the
customer&amp;#8217;s hands, you &lt;em&gt;really&lt;/em&gt; don&amp;#8217;t want it to crash. It&amp;#8217;s possible that
some of those assertions are bogus and the app will still actually work if
you run past a failed one. Sure, in rehearsal you stop on the first wrong
note, but once the audience sits down and the curtains go up, &lt;em&gt;the show
must go on.&lt;/em&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;The user can&amp;#8217;t handle a failed assertion anyway.&lt;/strong&gt; If you &lt;em&gt;were&lt;/em&gt; to let
the asserts remain in release mode, what do you do when one fails? In
debug mode, a failed assertion will do all sorts of helpful stuff like
show a stack trace with line numbers, maybe do a heap dump. All that is
really helpful&amp;#8230; if you&amp;#8217;re a programmer on the project.&lt;/p&gt;

    &lt;p&gt;If you&amp;#8217;re just Joe User, that&amp;#8217;s utterly useless (and may be a security
hazard!) The best the app could hope to do is show an sad face error message and restart. There&amp;#8217;s nothing an end user can productively do with the knowledge that a bug in the code itself has manifested.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So what Dart does is apply that reasoning to the type assertions themselves. You can really think of type annotations in Dart as being syntactic sugar for this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;dart&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;someFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_temp&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&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;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As a developer, you can run in checked mode and Dart gives you much of the benefit of a typed language. All those asserts help you enforce API requirements just like they do in C++ or other languages. In fact, you could adopt this style in JavaScript if you really wanted to.&lt;/p&gt;

&lt;p&gt;By baking a certain flavor of assert (asserting on type) into the &lt;em&gt;syntax&lt;/em&gt; of the language itself, Dart makes it easy for tools to parse those type annotations too and provide more contextual information about your code.&lt;/p&gt;

&lt;h2 id=&quot;is-it-a-type-system&quot;&gt;Is It a Type System?&lt;/h2&gt;

&lt;p&gt;&amp;#8220;Type system&amp;#8221; carries a lot of implied assumptions and meaning with it. If you take what you know about type systems and look at Dart through that lens, you will be one or more of:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Disappointed&lt;/li&gt;
  &lt;li&gt;Infuriated&lt;/li&gt;
  &lt;li&gt;Confused&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This doesn&amp;#8217;t mean it&amp;#8217;s a &lt;em&gt;bad feature&lt;/em&gt;, just that it&amp;#8217;s not what you think it is. Meatloaf is a pretty terrible dessert, but it&amp;#8217;s a fine entrée. If you don&amp;#8217;t think about type systems and just ask yourself &amp;#8220;is the set of features that Dart provides helpful in writing code?&amp;#8221;, I think the answer is &amp;#8220;yes&amp;#8221;. It&amp;#8217;s just not helpful in exactly the same way that type systems in other languages help.&lt;/p&gt;

&lt;p&gt;I don&amp;#8217;t really think of Dart as having a &lt;em&gt;type system.&lt;/em&gt; I think of it as having &lt;em&gt;type requirements&lt;/em&gt;. I can use types to define what the APIs of my library expect (preconditions) and promise to deliver (postconditions). At runtime, those assertions will be validated so I can figure out which side is failing to live up to its obligation.&lt;/p&gt;

&lt;p&gt;I find that&amp;#8217;s a pretty handy tool to have, and it&amp;#8217;s really nice to get that without having to give up the simplicity and flexibility of a dynamically typed language.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <id>http://journal.stuffwithstuff.com/2011/10/12/semicolons-are-a-shibboleth</id>
    <link type="text/html" rel="alternate" href="http://journal.stuffwithstuff.com/2011/10/12/semicolons-are-a-shibboleth"/>
    <title>Semicolons are a Shibboleth</title>
    <published>2011-10-12T00:00:00-07:00</published>
    <updated>2011-10-12T00:00:00-07:00</updated>
    <author>
      <name>Robert Nystrom</name>
      <uri>http://journal.stuffwithstuff.com/</uri>
    </author>
    <content type="html">&lt;p&gt;For better or worse, anything that Google does attracts a ton of attention. New programming languages also bring out the inner critic in most people. Type systems are prone to enraging the &lt;a href=&quot;http://lambda-the-ultimate.org/node/4377&quot;&gt;LtU&lt;/a&gt; set, especially &lt;a href=&quot;http://www.dartlang.org/articles/optional-types/&quot;&gt;unsound ones&lt;/a&gt;. And doing anything that &lt;a href=&quot;http://www.2ality.com/2011/09/google-dart.html&quot;&gt;approaches JavaScript&lt;/a&gt; is bound to infuriate a set of people that are passionately devoted to JS and The Way of the Prototype.&lt;/p&gt;

&lt;p&gt;So, really, &lt;a href=&quot;http://www.dartlang.org/&quot;&gt;Dart&lt;/a&gt; was a perfect storm. It&amp;#8217;s been strangely fun watching the complaints, critiques, feature requests and condemnation roll in. One particular issue has incited way more attention than I expected given its significance in the grand scheme of things: semicolons.&lt;/p&gt;

&lt;p&gt;Dart requires semicolons as statement terminators just like C, C++, Java, and C# do. (And JavaScript unless you&amp;#8217;re &lt;strike&gt;brave&lt;/strike&gt; insane enough to use &lt;abbr title=&quot;Automatic Semicolon Insertion&quot;&gt;ASI&lt;/abbr&gt;.) Making them optional is currently &lt;a href=&quot;https://code.google.com/p/dart/issues/detail?id=34&quot;&gt;the second-most starred bug&lt;/a&gt; on the tracker. It&amp;#8217;s one of the most frequently discussed points on &lt;a href=&quot;http://www.reddit.com/r/programming/comments/l6uwv/dart_programming_language/&quot;&gt;this insanely long reddit thread&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Whatever your opinion of them, you have to admit they&amp;#8217;re a relatively innocuous language feature. Making them optional has very little impact on your code, and there isn&amp;#8217;t anything you can express without them that you couldn&amp;#8217;t with (and vice versa). What&amp;#8217;s the big deal?&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s my theory: &lt;strong&gt;semicolons are a shibboleth to tell which language camp the designers come from.&lt;/strong&gt; If you don&amp;#8217;t know the term, &amp;#8220;&lt;a href=&quot;http://en.wikipedia.org/wiki/Shibboleth&quot;&gt;shibboleth&lt;/a&gt;&amp;#8221; comes from this passage in the Old Testament:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Gilead then cut Ephraim off from the fords of the Jordan, and whenever
Ephraimite fugitives said, &amp;#8216;Let me cross,&amp;#8217; the men of Gilead would ask, &amp;#8216;Are
you an Ephraimite?&amp;#8217; If he said, &amp;#8216;No,&amp;#8217; they then said, &amp;#8216;Very well, say
&amp;#8220;Shibboleth&amp;#8221; (שבלת).&amp;#8217; If anyone said, &amp;#8220;Sibboleth&amp;#8221; (סבלת), because he could
not pronounce it, then they would seize him and kill him by the fords of the
Jordan. Forty-two thousand Ephraimites fell on this occasion.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p class=&quot;cite&quot;&gt;Judges 12:5-6, NJB&lt;/p&gt;

&lt;h2 id=&quot;the-two-camps&quot;&gt;The Two Camps&lt;/h2&gt;

&lt;p&gt;I hate generalizations, and I particularly hate dichotomies, so I apologize for doing both right here. There is a huge amount of untruth to what I&amp;#8217;m about to say, but I hope enough truth for this to be worth your time. Here&amp;#8217;s two opposing philosophies for programming languages:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;A language is a tool to tame the complexity of the problems we&amp;#8217;re solving.
It should protect me from my fallibility (and more importantly the
fallability of the jack-asses on my team). The more it can help me control
things and prevent problems, the better. &lt;em&gt;Good fences make good neighbors.&lt;/em&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;A language is a tool for expressing my ideas. It should amplify
my creativity (and that of the smart people writing the cool libraries I
use). The fewer restrictions it gives me, the better. &lt;em&gt;We&amp;#8217;re all consenting
adults here.&lt;/em&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I think you can divide languages pretty effectively into those two camps. The former gets the &amp;#8220;serious business&amp;#8221; languages: Fortran, C++, Java, C#. The latter gets you the hipster languages: JavaScript, Ruby, Python, Lisp.&lt;/p&gt;

&lt;p&gt;Which camp is Dart in? Usually the presence of a static type system alone is enough to toss it into the first camp, but Dart&amp;#8217;s type system is so&amp;#8230; &lt;em&gt;optional&lt;/em&gt;, that it sort of hovers in the &lt;a href=&quot;http://en.wikipedia.org/wiki/Uncanny_valley&quot;&gt;uncanny valley&lt;/a&gt; between static and dynamic.&lt;/p&gt;

&lt;p&gt;If you&amp;#8217;re trying to figure out whether you should give a damn about Dart, you&amp;#8217;ll want to know if its philosophy lines up with yours. You can look at the language today, but it&amp;#8217;s hard to know how much that tells you since the it&amp;#8217;s clearly not done yet.&lt;/p&gt;

&lt;h2 id=&quot;semicolons&quot;&gt;Semicolons&lt;/h2&gt;

&lt;p&gt;So you ask the shibboleth question: &amp;#8220;mandatory semicolons?&amp;#8221;&lt;/p&gt;

&lt;p&gt;By itself, semicolons mean little to the language, but almost all of the second camp languages make them optional or eschew them completely. They say punctuation is ugly, makes the code less beautiful, and is needless boilerplate since they are almost always at the ends of lines anyway. Requiring them is just making the programmer do mindless gruntwork.&lt;/p&gt;

&lt;p&gt;But a &amp;#8220;serious business&amp;#8221; programmer likes them: they are simple and unambiguous to parse. You don&amp;#8217;t have to worry about some other guy&amp;#8217;s weird coding style making it impossible for you to understand their code. We&amp;#8217;ve been using semicolons for years, why stop now? They&amp;#8217;re safe and familiar.&lt;/p&gt;

&lt;p&gt;Asking which way Dart is going to with semicolons is, I think, a way of asking which group of programmers the language wants to cater to. Either choice carries an implied signal about how other choices in the language will likely be made: will it favor individual expressiveness over group consistency? Freedom over safety?&lt;/p&gt;

&lt;h2 id=&quot;so-how-does-dart-pronounce-it&quot;&gt;So How Does Dart Pronounce It?&lt;/h2&gt;

&lt;p&gt;So the question is, what will Dart do?&lt;/p&gt;

&lt;p&gt;I honestly have no idea. I&amp;#8217;m not one of the designers, and I think they may be split on the issue. I have my own personal preference, but that carries about as much weight as yours. Even if they were to make semicolons optional, I don&amp;#8217;t think that necessarily says much about your or my other pet features being included.&lt;/p&gt;

&lt;p&gt;I think Dart really is trying to inhabit the ground between these two camps, so each attempt to pull in one direction or the other will be handled individually.&lt;/p&gt;

&lt;p&gt;But, like everyone else, I hope they decide to go with &lt;em&gt;my&lt;/em&gt; preference, which is of course the right one.&lt;/p&gt;
</content>
  </entry>
  

</feed>
