<?xml version="1.0" encoding="UTF-8"?>
<feed
  xmlns="http://www.w3.org/2005/Atom"
  xmlns:thr="http://purl.org/syndication/thread/1.0"
  xml:lang="en">
  <title type="text">journal.stuffwithstuff.com</title>
  <subtitle type="text">Bob Nystrom's Blog</subtitle>

  <updated>2012-01-25T16:06:50Z</updated>
  <generator uri="http://blogofile.com/">Blogofile</generator>

  <link rel="alternate" type="text/html" href="http://journal.stuffwithstuff.com" />
  <id>http://journal.stuffwithstuff.com/feed/atom/</id>
  <link rel="self" type="application/atom+xml" href="http://journal.stuffwithstuff.com/feed/atom/" />
  <entry>
    <author>
      <name></name>
      <uri>http://journal.stuffwithstuff.com</uri>
    </author>
    <title type="html"><![CDATA[Higher Order Macros in C++]]></title>
    <link rel="alternate" type="text/html" href="http://journal.stuffwithstuff.com/2012/01/24/higher-order-macros-in-c" />
    <id>http://journal.stuffwithstuff.com/2012/01/24/higher-order-macros-in-c</id>
    <updated>2012-01-24T00:00:00Z</updated>
    <published>2012-01-24T00:00:00Z</published>
    <category scheme="http://journal.stuffwithstuff.com" term="c" />
    <category scheme="http://journal.stuffwithstuff.com" term="code" />
    <category scheme="http://journal.stuffwithstuff.com" term="c++" />
    <summary type="html"><![CDATA[Higher Order Macros in C++]]></summary>
    <content type="html" xml:base="http://journal.stuffwithstuff.com/2012/01/24/higher-order-macros-in-c"><![CDATA[<p>My hobby project lately has been working on a little <a href="https://github.com/munificent/magpie-cpp">bytecode interpreter</a> for <a href="http://magpie-lang.org/">Magpie</a> in C++. As an ex-game programmer, I'm pretty sad at how rusty my C++ has gotten. To try to make things a bit easier on myself, I've been borrowing from the masters whenever possible. That means I usually have <a href="http://code.google.com/p/v8/">v8</a>'s source code open in another window.</p>
<p>(Aside: if you're interested in programming languages, it is <em>so awesome</em> that implementations like v8 and <a href="http://hg.mozilla.org/mozilla-central/file/1982c882af0f/js/src">spidermonkey</a> 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's still amazing that industry-leading codebases are just there waiting for your perusal.)</p>
<p>In all honesty, my usual process looks a bit like:</p>
<ol>
<li>"Hmm, I need to code up a floobinator. v8 has one. Let me see how they do it."</li>
<li><em>hunt through v8 code</em></li>
<li>"Ah, here it is."</li>
<li>OH GOD, WHAT WIZARDRY IS THIS.</li>
</ol>
<p>About 90% of it is <a href="http://code.google.com/p/v8/source/browse/trunk/src/ia32/codegen-ia32.cc#295">over my head</a>, 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 <em>do</em> understand: macros that take macros as arguments. That's the point of this post.</p>
<h2>The Problem</h2>
<p>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... well... <a href="http://crazycpp.wordpress.com/category/template-metaprogramming/">almost anything</a>.</p>
<p>Even so, there's still often hunks of repetition that you can't seem to eliminate. For example, let's say we're working with a language's syntax. Typically, the <a href="https://github.com/munificent/finch/blob/master/src/Syntax/Parser.h">parser</a> generates an <a href="http://en.wikipedia.org/wiki/Abstract_syntax_tree">AST</a> which then gets passed to the <a href="https://github.com/munificent/finch/blob/master/src/Compiler/Compiler.h">compiler</a>. The compiler walks the AST using <a href="http://en.wikipedia.org/wiki/Visitor_pattern">Ye Olde Visitor Patterne</a> and generates some <a href="https://github.com/munificent/finch/blob/master/src/Compiler/Block.h">lower-level representation</a> for it.</p>
<p>Depending on how rich your language is, you'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 <a href="http://code.google.com/p/v8/source/browse/trunk/src/ast.h#123">40</a> classes to cover everything you can express in JavaScript.</p>
<p>These are relatively simple types. A (greatly!) simplified one looks a bit like:</p>
<div class="codehilite"><pre><span class="k">class</span> <span class="nc">BinaryOpExpr</span> <span class="o">:</span> <span class="k">public</span> <span class="n">Expression</span> <span class="p">{</span>
  <span class="n">BinaryOpExpr</span><span class="p">(</span><span class="n">Expression</span><span class="o">*</span> <span class="n">left</span><span class="p">,</span> <span class="n">Expression</span><span class="o">*</span> <span class="n">right</span><span class="p">)</span>
  <span class="o">:</span> <span class="n">left</span><span class="p">(</span><span class="n">left</span><span class="p">),</span>
    <span class="n">right</span><span class="p">(</span><span class="n">right</span><span class="p">)</span> <span class="p">{}</span>

  <span class="k">virtual</span> <span class="kt">void</span> <span class="n">accept</span><span class="p">(</span><span class="n">AstVisitor</span><span class="o">&amp;</span> <span class="n">visitor</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">visitor</span><span class="p">.</span><span class="n">visitBinaryOpExpr</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
  <span class="p">}</span>

  <span class="n">Expression</span><span class="o">*</span> <span class="n">left</span><span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="n">left</span><span class="p">;</span> <span class="p">}</span>
  <span class="n">Expression</span><span class="o">*</span> <span class="n">right</span><span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="n">right</span><span class="p">;</span> <span class="p">}</span>

<span class="k">private</span><span class="o">:</span>
  <span class="n">Expression</span><span class="o">*</span> <span class="n">left</span><span class="p">;</span>
  <span class="n">Expression</span><span class="o">*</span> <span class="n">right</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>


<p>Imagine thirty-something-odd more classes like this and you've got the right idea. There isn't <em>too</em> much we can do in C++ to simplify these definitions themselves. Each class is different enough that it's simplest and clearest to just write them out.</p>
<p>Where the tedium really comes in is all of the surrounding code that <em>uses</em> these classes. First up is the aforementioned <a href="http://code.google.com/p/v8/source/browse/trunk/src/ast.h#2151">visitor</a>. To make it easy for the compiler to dispatch to different code based on the different AST classes, you'll typically define a class like:</p>
<div class="codehilite"><pre><span class="k">class</span> <span class="nc">AstVisitor</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
  <span class="o">~</span><span class="k">virtual</span> <span class="n">AstVisitor</span><span class="p">()</span> <span class="p">{}</span>

  <span class="k">virtual</span> <span class="kt">void</span> <span class="n">visitBoolLiteral</span><span class="p">(</span><span class="n">BoolLiteral</span><span class="o">*</span> <span class="n">expr</span><span class="p">)</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  <span class="k">virtual</span> <span class="kt">void</span> <span class="n">visitNumLiteral</span><span class="p">(</span><span class="n">NumLiteral</span><span class="o">*</span> <span class="n">expr</span><span class="p">)</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  <span class="k">virtual</span> <span class="kt">void</span> <span class="n">visitStringLiteral</span><span class="p">(</span><span class="n">StringLiteral</span><span class="o">*</span> <span class="n">expr</span><span class="p">)</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  <span class="k">virtual</span> <span class="kt">void</span> <span class="n">visitUnaryOpExpr</span><span class="p">(</span><span class="n">UnaryOpExpr</span><span class="o">*</span> <span class="n">expr</span><span class="p">)</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  <span class="k">virtual</span> <span class="kt">void</span> <span class="n">visitBinaryOpExpr</span><span class="p">(</span><span class="n">BinaryOpExpr</span><span class="o">*</span> <span class="n">expr</span><span class="p">)</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  <span class="k">virtual</span> <span class="kt">void</span> <span class="n">visitAssignmentExpr</span><span class="p">(</span><span class="n">AssignmentExpr</span><span class="o">*</span> <span class="n">expr</span><span class="p">)</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  <span class="k">virtual</span> <span class="kt">void</span> <span class="n">visitConditionalExpr</span><span class="p">(</span><span class="n">ConditionalExpr</span><span class="o">*</span> <span class="n">expr</span><span class="p">)</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  <span class="k">virtual</span> <span class="kt">void</span> <span class="n">visitIfThenStmt</span><span class="p">(</span><span class="n">IfThenStmt</span><span class="o">*</span> <span class="n">expr</span><span class="p">)</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  <span class="c1">// 30 more of these, you get the idea...</span>
<span class="p">};</span>
</pre></div>


<p>That code really <em>is</em> just repetitive boilerplate. There's more. It's useful to also have an enum for each AST node type so that we can also <code>switch</code> directly on the type of a node without having to go through a visitor for everything. So you'll want something like:</p>
<div class="codehilite"><pre><span class="k">enum</span> <span class="n">AstType</span> <span class="p">{</span>
  <span class="n">kBoolLiteral</span><span class="p">,</span>
  <span class="n">kNumLiteral</span><span class="p">,</span>
  <span class="n">kStringLiteral</span><span class="p">,</span>
  <span class="n">kUnaryOpExpr</span><span class="p">,</span>
  <span class="n">kBinaryOpExpr</span><span class="p">,</span>
  <span class="c1">// again, you get the idea...</span>
<span class="p">};</span>
</pre></div>


<p>For debugging, it's handy to be able to get a string representation for an AST node's type too:</p>
<div class="codehilite"><pre><span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">typeString</span><span class="p">(</span><span class="n">AstType</span> <span class="n">type</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">switch</span> <span class="p">(</span><span class="n">type</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">case</span> <span class="nl">kBoolLiteral:</span>  <span class="k">return</span> <span class="s">&quot;BoolLiteral&quot;</span><span class="p">;</span>
    <span class="k">case</span> <span class="nl">kIntLiteral:</span>   <span class="k">return</span> <span class="s">&quot;IntLiteral&quot;</span><span class="p">;</span>
    <span class="k">case</span> <span class="nl">kNumLiteral:</span>   <span class="k">return</span> <span class="s">&quot;NumLiteral&quot;</span><span class="p">;</span>
    <span class="k">case</span> <span class="nl">kUnaryOpExpr:</span>  <span class="k">return</span> <span class="s">&quot;UnaryOpLiteral&quot;</span><span class="p">;</span>
    <span class="k">case</span> <span class="nl">kBinaryOpExpr:</span> <span class="k">return</span> <span class="s">&quot;BinaryOpLiteral&quot;</span><span class="p">;</span>
    <span class="c1">// yup...</span>
  <span class="p">}</span>
<span class="p">}</span>
</pre></div>


<p>C++'s usual abstraction facilities won't help us here: in all of these cases the repetition is in the <em>middle</em> 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).</p>
<h2>Let's Get Dirty</h2>
<p>But there is, of course, one grease-covered rusty tool in the C and C++ toolbox that doesn't give a damn about actual syntactic elements: <em>the preprocessor.</em> It doesn't even know what a statement is! It just sees chunks of text.</p>
<p>More often than not, that fact makes it too blunt of an instrument to be wielded indiscriminately without risking bloodshed but here it's just what we need. In all of our problem examples, we want to be able to say "for each AST node type, insert this chunk of code but with the node's class name inserted in it in a few places."</p>
<p>Let's try doing something like this:</p>
<div class="codehilite"><pre><span class="cp">#define DEFINE_VISIT(type)  \</span>
<span class="cp">    virtual void visit##type(type* expr) = 0</span>
</pre></div>


<p>With this, we can simplify our visitor class to:</p>
<div class="codehilite"><pre><span class="k">class</span> <span class="nc">AstVisitor</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
  <span class="o">~</span><span class="k">virtual</span> <span class="n">AstVisitor</span><span class="p">()</span> <span class="p">{}</span>

  <span class="n">DEFINE_VISIT</span><span class="p">(</span><span class="n">BoolLiteral</span><span class="p">);</span>
  <span class="n">DEFINE_VISIT</span><span class="p">(</span><span class="n">NumLiteral</span><span class="p">);</span>
  <span class="n">DEFINE_VISIT</span><span class="p">(</span><span class="n">StringLiteral</span><span class="p">);</span>
  <span class="n">DEFINE_VISIT</span><span class="p">(</span><span class="n">UnaryOpExpr</span><span class="p">);</span>
  <span class="n">DEFINE_VISIT</span><span class="p">(</span><span class="n">BinaryOpExpr</span><span class="p">);</span>
  <span class="n">DEFINE_VISIT</span><span class="p">(</span><span class="n">AssignmentExpr</span><span class="p">);</span>
  <span class="n">DEFINE_VISIT</span><span class="p">(</span><span class="n">ConditionalExpr</span><span class="p">);</span>
  <span class="n">DEFINE_VISIT</span><span class="p">(</span><span class="n">IfThenStmt</span><span class="p">);</span>
  <span class="c1">// 30 more of these, you get the idea...</span>
<span class="p">};</span>
</pre></div>


<p>That's a <em>little</em> better, I guess. But not really. This trick doesn't help at all with the enum, and only helps a little in <code>typeString()</code>. The problem is that it's the "for each AST type" part of our problem where the repetition really is. It's the <em>loop itself</em> we want to abstract over more than the loop <em>body</em>.</p>
<p>What we want is a macro that will <em>itself</em> walk over all of the types, like:</p>
<div class="codehilite"><pre><span class="cp">#define AST_NODE_LIST   \</span>
<span class="cp">    BoolLiteral         \</span>
<span class="cp">    NumLiteral          \</span>
<span class="cp">    StringLiteral       \</span>
<span class="cp">    UnaryOpExpr         \</span>
<span class="cp">    ...</span>
</pre></div>


<p>But of course, that one doesn't do anything useful. We don'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:</p>
<div class="codehilite"><pre><span class="cp">#define AST_NODE_LIST(code) \</span>
<span class="cp">    code(BoolLiteral)       \</span>
<span class="cp">    code(NumLiteral)        \</span>
<span class="cp">    code(StringLiteral)     \</span>
<span class="cp">    code(UnaryOpExpr)       \</span>
<span class="cp">    ...</span>
</pre></div>


<h2>Macros Taking Macros... We Must Go Deeper</h2>
<p>Now the fun part. When we use that <code>AST_NODE_LIST</code> macro, what is that <code>code</code> argument going to be? It needs to be a thing that's available at preprocess time, can take an argument, and can generate a chunk of code. That leaves only one answer: a macro.</p>
<p>Until I saw this in V8, I didn't even know you <em>could</em> pass macros to macros. But indeed you can. Using the <code>AST_NODE_LIST</code> we just defined, our visitor becomes:</p>
<div class="codehilite"><pre><span class="k">class</span> <span class="nc">AstVisitor</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
  <span class="o">~</span><span class="k">virtual</span> <span class="n">AstVisitor</span><span class="p">()</span> <span class="p">{}</span>

<span class="cp">  #define DEFINE_VISIT(type)  \</span>
<span class="cp">  virtual void visit##type(type* expr) = 0</span>
  <span class="n">AST_NODE_LIST</span><span class="p">(</span><span class="n">DEFINE_VISIT</span><span class="p">)</span>
<span class="cp">  #undef DEFINE_VISIT </span><span class="c1">// Clean it up since we&#39;re done with it.</span>
<span class="p">};</span>
</pre></div>


<p>When <code>AST_NODE_LIST</code> is expanded, it will expand to one call to <code>DEFINE_VISIT</code> for each of the AST node types. Then <em>those</em> will in turn be expanded to define the visitor method for that type.</p>
<p>Likewise, our enum becomes:</p>
<div class="codehilite"><pre><span class="cp">#define DEFINE_ENUM_TYPE(type) k##type,</span>
<span class="k">enum</span> <span class="n">AstType</span> <span class="p">{</span>
  <span class="n">AST_NODE_LIST</span><span class="p">(</span><span class="n">DEFINE_ENUM_TYPE</span><span class="p">)</span>
<span class="p">};</span>
<span class="cp">#undef DEFINE_ENUM_TYPE</span>
</pre></div>


<p>That's the whole thing in its entirety. Finally, the function for converting it to a string:</p>
<div class="codehilite"><pre><span class="cp">#define DEFINE_TYPE_STRING(type) case k##type: return #type;</span>
<span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">typeString</span><span class="p">(</span><span class="n">AstType</span> <span class="n">type</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">switch</span> <span class="p">(</span><span class="n">type</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">AST_NODE_LIST</span><span class="p">(</span><span class="n">DEFINE_TYPE_STRING</span><span class="p">)</span>
  <span class="p">}</span>
<span class="p">}</span>
<span class="cp">#undef DEFINE_TYPE_STRING</span>
</pre></div>


<p>Note that we're using <a href="http://gcc.gnu.org/onlinedocs/cpp/Stringification.html">stringification</a> and <a href="http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html">token pasting</a> here to not just literally substitute in the AST node class's name, but also to use it as a string, or to build a larger name (like <code>kBinaryOpExpr</code>) out of it.</p>
<p>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 <code>AST_NODE_LIST</code>. Once it's there, every place that's using that will automatically pick it up. That way you don't have to remember to touch <code>AstVisitor</code>, <code>AstType</code> and <code>typeString()</code>.</p>
<p>I can't tell if this is crazy awesome, or just plain crazy, but it's not something you see every day. What other interesting stuff is hiding in the bowels of your favorite open source project?</p>]]></content>
  </entry>
  <entry>
    <author>
      <name></name>
      <uri>http://journal.stuffwithstuff.com</uri>
    </author>
    <title type="html"><![CDATA[A Proposal for Null-safety in Dart]]></title>
    <link rel="alternate" type="text/html" href="http://journal.stuffwithstuff.com/2011/10/29/a-proposal-for-null-safety-in-dart" />
    <id>http://journal.stuffwithstuff.com/2011/10/29/a-proposal-for-null-safety-in-dart</id>
    <updated>2011-10-29T00:00:00Z</updated>
    <published>2011-10-29T00:00:00Z</published>
    <category scheme="http://journal.stuffwithstuff.com" term="code" />
    <category scheme="http://journal.stuffwithstuff.com" term="language" />
    <category scheme="http://journal.stuffwithstuff.com" term="dart" />
    <summary type="html"><![CDATA[A Proposal for Null-safety in Dart]]></summary>
    <content type="html" xml:base="http://journal.stuffwithstuff.com/2011/10/29/a-proposal-for-null-safety-in-dart"><![CDATA[<p>Page 75 of the current (0.04) draft of <a href="http://www.dartlang.org/docs/spec/index.html">the Dart language spec</a> has this note in it:</p>
<blockquote>
<p>Should we do something with respect to non-nullable types?</p>
</blockquote>
<p>If you asked me I'd answer a resounding yes! (Alas, no one did, but that's never stopped me before.) So, here's my attempt at an answer. I'm posting it here on my blog just to clarify that this is <em>my</em> strawman proposal, and not something that's got any official Dart or Google stamp of approval on it.</p>
<h2>The Proposal In a Nutshell</h2>
<p>By default, all types are non-nullable. If you declare a variable of type <code>int</code>, it is a static error to try to assign <code>null</code> to it. In checked mode, it is a dynamic error to assign <code>null</code> to a non-nullable variable.</p>
<p>You can define a nullable type by adding <code>?</code> after the type name. <code>int</code> is non-nullable, <code>int?</code> is nullable. Non-null types are a property of Dart's type system, but (unlike <a href="http://lukeplant.me.uk/blog/posts/null-pointers-vs-none-vs-maybe/"><code>Maybe</code></a> or <a href="http://www.standardml.org/Basis/option.html"><code>option</code></a> in other languages) not a part of its runtime behavior.</p>
<p>At runtime, Dart continues to work like a dynamic language where any variable may hold a value of any type, including <code>null</code>. "Nullability" is as optional as the rest of Dart's type system.</p>
<h2>Why Default to Non-nullable?</h2>
<p>One of the first decisions a non-null proposal has to make is which is the default: nullable or non-nullable?</p>
<p>I prefer defaulting to non-null for a couple of reasons:</p>
<ul>
<li>
<p>Most of the time, you don't want to allow <code>null</code>. I went through
    <a href="https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/client/samples/swarm/SwarmViews.dart">SwarmViews.dart</a>, one of the largest
    Dart source files that I'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.</p>
</li>
<li>
<p>A very large number of type annotations are for atomic types (<code>int</code>, <code>bool</code>,
    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 "I want a <em>non-nullable</em> bool".</p>
</li>
<li>
<p>In Dart, you don't have to provide a type annotation at all. It seems to me
    then that if you've chosen to go out of your way to say a variable is of
    type <code>Foo</code>, it should really be a <code>Foo</code> and not a <code>Foo-or-null</code>.</p>
</li>
</ul>
<h2>Semantics</h2>
<p>A nullable type is essentially the <a href="http://en.wikipedia.org/wiki/Type_system#Union_types">union</a> of the original type and the <code>Null</code> type. In other words, <code>bool?</code> contains all of the values of the <code>bool</code> type (<code>true</code> and <code>false</code>) as well as all of the values of the <code>Null</code> type (<code>null</code>).</p>
<p>Unlike option types, nullable types do not nest. <code>int??</code> is equivalent to <code>int?</code>. This follows naturally from thinking of it as a union of types. <code>true | false | null</code> is an equivalent set to <code>true | false | null | null</code>. The redundant <code>null</code>s just collapse.</p>
<h2>Subtyping</h2>
<p>A non-null type is a subtype of its nullable type. In other words, you can always pass a <code>Foo</code> to something that expects a <code>Foo?</code>.</p>
<p>Also, the special <code>Null</code> type is a subtype of every nullable type. This means you can initialize nullable types with <code>null</code> like you'd expect. Since <code>Null</code> is <em>not</em> a subtype of <em>non</em>-nullable types, you <em>cannot</em> initialize those with <code>null</code>. That implies that all variables of non-null types must be initialized. This is an error:</p>
<div class="codehilite"><pre><span class="kt">int</span> <span class="n">a</span><span class="p">;</span>
</pre></div>


<p>As is this:</p>
<div class="codehilite"><pre><span class="k">class</span> <span class="n">Point</span> <span class="p">{</span>
  <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">;</span>
  <span class="n">Point</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>


<p>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's been initialized in the constructor. Fortunately, Dart already has constructor initialization lists, and they solve this problem handily. We can fix that <code>Point</code> class like so:</p>
<div class="codehilite"><pre><span class="k">class</span> <span class="n">Point</span> <span class="p">{</span>
  <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">;</span>
  <span class="n">Point</span><span class="p">()</span> <span class="o">:</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<p>Since the constructor initializers are run before you can access <code>this</code>, we've ensured that those fields will be initialized before <em>anyone</em> can see them. Neat.</p>
<h2>Working with Nullable Types</h2>
<p>One challenge with nullable types is that working with them can be tedious. With things like <a href="http://en.wikipedia.org/wiki/Option_type">Maybe in Haskell and <code>option</code> in ML</a>, you have to manually unwrap the "nullable" value to get at the delicious real value hidden inside.</p>
<p>This proposal doesn't have that problem (granted, it also doesn't have some of the benefits of option types). It tries to stick close to Dart's dynamic sensibilities, so there are no option-like values that you have to extract the real value from using pattern matching.</p>
<p>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't yell at you if it knows <code>null</code> is one of those values.</p>
<p>To test for null, you can just do a vanilla <code>if (foo == null)</code> statement.</p>
<p>To go back and forth between nullable and non-nullable types, we rely on assignment compatibility. Dart's assignment compatibility rules are looser than other languages. Like most, they allow implicit upcasts. Given this:</p>
<div class="codehilite"><pre><span class="k">class</span> <span class="n">Base</span> <span class="p">{}</span>
<span class="k">class</span> <span class="n">Derived</span> <span class="kd">extends</span> <span class="n">Base</span> <span class="p">{}</span>
</pre></div>


<p>Then this is allowed:</p>
<div class="codehilite"><pre><span class="n">Base</span> <span class="n">b</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Derived</span><span class="p">();</span>
</pre></div>


<p>But Dart also allows implicit <em>downcasts</em> (in other words from supertype to subtype):</p>
<div class="codehilite"><pre><span class="n">Derived</span> <span class="n">d</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span> <span class="c1">// declared as type Base above</span>
</pre></div>


<p>(The reasoning here is that many times a downcast <em>will</em> work correctly at runtime, and Dart's type system is optimistic that you know what you're doing.)</p>
<p>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:</p>
<div class="codehilite"><pre><span class="kt">int</span><span class="o">?</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">123</span><span class="p">;</span>
</pre></div>


<p>But it's equally easy (though not equally <em>safe</em>) to go in the other direction:</p>
<div class="codehilite"><pre><span class="kt">int</span> <span class="n">b</span> <span class="o">=</span> <span class="n">a</span><span class="p">;</span> <span class="c1">// declared as int? above</span>
</pre></div>


<p>If you actually want to be safe, you just need to check for null first:</p>
<div class="codehilite"><pre><span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">!=</span> <span class="nb">null</span><span class="p">)</span> <span class="p">{</span>
  <span class="kt">int</span> <span class="n">b</span> <span class="o">=</span> <span class="n">a</span><span class="p">;</span> <span class="c1">// safe at runtime now too!</span>
<span class="p">}</span>
</pre></div>


<p>So here, as in other places in Dart, the type checker won't get in your way.</p>
<h2>Wait, What Good is It?</h2>
<p>You couldn't ask for a less intrusive null-tracking system, but it seems a little <em>too</em> unintrusive. Isn't silently assigning from a nullable type to a non-nullable one the exact thing we'd want the type checker to flag?</p>
<p>Well, yes, sort of. But Dart's specified static checker isn't that strict in general. It also doesn'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:</p>
<ul>
<li>
<p>Gilad describes Dart as having a "documentary" type system. With nullable
    and non-nullable types, we can now use type annotations to tell users of our APIs which things accept <code>null</code> and which don't. In other languages you
    have no choice but to spell that out in the documentation. With this, it's
    right in the type signature for a method. If you call <code>method(Foo a, Foo? b)</code>, it's clear that it can handle a <code>null</code> for the second argument but not the first.</p>
</li>
<li>
<p>In checked mode, if you try to assign <code>null</code> to a non-nullable type, it
    <em>will</em> flag that as an error. This means you'll know when a <code>null</code> snuck in
    <em>as soon as it happens</em>, 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.</p>
</li>
<li>
<p>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't do is implicitly assign from <em>null</em> to a non-nullable type.
    This is a static error:</p>
<div class="codehilite"><pre><span class="kt">int</span> <span class="n">a</span> <span class="o">=</span> <span class="nb">null</span><span class="p">;</span>
</pre></div>


<p>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.</p>
</li>
<li>
<p>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:</p>
<div class="codehilite"><pre><span class="kt">double</span><span class="p">(</span><span class="kt">int</span><span class="o">?</span> <span class="n">i</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">i</span> <span class="o">*</span> <span class="mi">2</span><span class="p">;</span> <span class="c1">// WARN that we didn&#39;t test i for null first</span>
<span class="p">}</span>

<span class="kt">double</span><span class="p">(</span><span class="kt">int</span><span class="o">?</span> <span class="n">i</span> <span class="p">)</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">==</span> <span class="nb">null</span><span class="p">)</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
  <span class="k">return</span> <span class="n">i</span> <span class="o">*</span> <span class="mi">2</span><span class="p">;</span> <span class="c1">// OK now</span>
<span class="p">}</span>
</pre></div>


</li>
</ul>
<h2>Nullables and Generics</h2>
<p>A key question that comes up with nullables is how they play with generic types. I'm not a type system expert, so it's entirely possible that I haven't thought this all the way through, but I think it falls out correctly from the subtyping between nullable and non-nullable types.</p>
<p>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:</p>
<div class="codehilite"><pre><span class="n">List</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">?&gt;</span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="n">List</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">();</span>
</pre></div>


<p>As long as you use your types in a way that makes covariance safe (i.e. you read from them and don't write to them) this will work as well for nullable types as it does with subclassing.</p>
<p>The other question is how using a type parameter in an annotation plays with nullability. I.e.:</p>
<div class="codehilite"><pre><span class="k">class</span> <span class="n">SomeClass</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="p">{</span>
  <span class="n">T</span><span class="o">?</span> <span class="n">someField</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<p>This is <em>really</em> beyond the edge of my type system fu, but I think the fact that nullable types flatten (<code>A??</code> becomes <code>A?</code>) will alleviate some of the nasty corner cases this may lead to. I'm not sure though, and this is definitely something I'd like feedback on.</p>
<h2>Two Corner Cases</h2>
<p>There are two types in Dart where nullability will work a little strangely: <code>Null</code> and <code>Object</code>. The former's behavior is pretty obviously weird. Given that a nullable type is the union of some type and <code>Null</code>, there's no dinstinction between <code>Null</code> and <code>Null?</code> (since nullability flattens). Likewise, you can't have a non-nullable <code>Null</code> type (since no possible values could inhabit it).</p>
<p>The other case is a bit more surprising. You can't have a non-nullable <code>Object</code> type. <code>Object</code> is the <a href="http://en.wikipedia.org/wiki/Top_type">top type</a> which means <em>every</em> object is an instance of <code>Object</code>, including <code>null</code>. In practice, I think this works OK. If you have a variable of type <code>Object</code>, the only operations you can perform on it are ones that all types support, like <code>toString()</code>. Even <code>null</code> supports those, so a non-nullable <code>Object</code> type isn't needed to avoid type errors.</p>
<p>That covers most of the implications of the proposal as far as I can tell. To get more concrete, let's see how we'd need to modify the spec and libs to support it:</p>
<h2>Spec Changes</h2>
<p>I definitely don't have the skills Gilad has at specifying these semantics precisely, but I'll at least make an honest try. Most of the changes, as you'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.</p>
<h3>Section 10.2</h3>
<p>Change:</p>
<blockquote>
<p>The static type of null is ⊥.</p>
</blockquote>
<p>to:</p>
<blockquote>
<p>The static type of null is Null.</p>
</blockquote>
<h3>Section 10.16 Assignment</h3>
<p>In both:</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>and:</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>remove "o is not null and".</p>
<h3>Section 10.13.2 Binding Actuals to Formals</h3>
<p>From:</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>remove "oi is not null and". And from:</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>remove "om+j is not null and".</p>
<h3>Section 11.9 Try</h3>
<p>From:</p>
<blockquote>
<p>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."</p>
</blockquote>
<p>remove "o is null or".</p>
<p>Then we need to add a section:</p>
<h3>13.9 Nullable Types</h3>
<blockquote>
<p>A <em>nullable type</em> is a parameterized type that allows both values of the type parameter's type or the singleton value of type <em>Null</em>, <code>null</code>.</p>
<p>Let <em>A?</em> be the nullable type of type <em>A</em>. Nullable types do not nest or wrap: <em>B??</em> is equivalent to <em>B?</em> for any type <em>B</em>. The subtype relations are:</p>
<ul>
<li><em>A</em> &lt;: <em>A?</em> (non-nullable is a subtype of a nullable)</li>
<li><em>Null</em> &lt;: <em>A?</em> (non-nullable is also a subtype of Null)</li>
</ul>
<p>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's type.</p>
</blockquote>
<h2>Corelib Changes</h2>
<p>This proposal implies some modification to the corelib APIs to be null-aware. There are two kinds of changes we'd need: minor changes to type annotations and a few actual semantic changes.</p>
<h3>Type Annotation Changes</h3>
<p>The minor change is that a few type annotations need to be tweaked. Operations that are defined to return something "or null" need to be made explicitly nullable.</p>
<p>For example, the <a href="http://www.dartlang.org/docs/api/Map.html#Map::Map"><code>Map&lt;K,V&gt;</code> interface</a> has a subscript operator (i.e. <code>map[key]</code>) that returns <code>null</code> if the key isn't found. Right now it's return type is declared to be <code>V</code>, the value type. It will need to be <code>V?</code> instead to explicitly permit that <code>null</code>.</p>
<p>Likewise, there are a number of methods that take named optional parameters whose types aren't nullable, like:</p>
<div class="codehilite"><pre><span class="k">class</span> <span class="n">Expect</span> <span class="p">{</span>
    <span class="kd">static</span> <span class="kt">void</span> <span class="n">isFalse</span><span class="p">(</span><span class="n">actual</span><span class="p">,</span> <span class="p">[</span><span class="kt">String</span> <span class="n">reason</span> <span class="o">=</span> <span class="nb">null</span><span class="p">])</span>
    <span class="c1">// more...</span>
<span class="p">}</span>
</pre></div>


<p>Here, that <code>String</code> will need to be made nullable.</p>
<h3>Semantic Changes</h3>
<p>The more intrusive change is that operations that implicitly create <code>null</code> entries in collections might need to be modified. For example, if you do:</p>
<div class="codehilite"><pre><span class="kt">var</span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="n">List</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">(</span><span class="mi">4</span><span class="p">);</span>
</pre></div>


<p>then you create a list whose four elements are automatically set to <code>null</code>. That's a problem here because the element type (<code>int</code>) is non-nullable.</p>
<p>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:</p>
<div class="codehilite"><pre><span class="kt">var</span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="n">List</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="n">fill</span><span class="o">:</span> <span class="mi">1</span><span class="p">);</span>
<span class="n">print</span><span class="p">(</span><span class="n">list</span><span class="p">);</span> <span class="c1">// [1, 1, 1, 1]</span>
</pre></div>


<p>or maybe:</p>
<div class="codehilite"><pre><span class="kt">var</span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="n">List</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">.</span><span class="n">generate</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="p">(</span><span class="n">index</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="n">index</span> <span class="o">*</span> <span class="mi">2</span><span class="p">);</span>
<span class="n">print</span><span class="p">(</span><span class="n">list</span><span class="p">);</span> <span class="c1">// [0, 2, 4, 6]</span>
</pre></div>


<p>I may have missed something, but I believe <code>List</code> is the only type that needs any real API changes like this.</p>
<h2>Super Summmary</h2>
<p>If you made it this far, you deserve a reward. Sadly, the best I can offer is a bullet list of what you've already read:</p>
<ul>
<li>All types are non-nullable by default.</li>
<li>You define a nullable type by adding <code>?</code> after the type.</li>
<li>Nested nullable types flatten: <code>A?? -&gt; A?</code>.</li>
<li>A non-nullable type is a subtype of its nullable type.</li>
<li>No nullable <em>values</em>: no <code>option</code> or <code>Maybe</code>.</li>
<li>Can assign back and forth between nullable and non-nullable types.</li>
<li>Must initialize non-nullable variables to non-null values.</li>
<li>Lets types document which things expect null.</li>
<li>Catches invalid nulls early in checked mode.</li>
<li>Most spec changes are just removing "or null".</li>
<li>Most corelib changes are making some annotations nullable.</li>
<li>Probably want to change <code>List</code> constructor to avoid implicit null fill values.</li>
<li>This proposal is awesome.</li>
</ul>
<p>Do I have that right? Thoughts?</p>]]></content>
  </entry>
  <entry>
    <author>
      <name></name>
      <uri>http://journal.stuffwithstuff.com</uri>
    </author>
    <title type="html"><![CDATA[Wrapping My Head Around Optional Typing]]></title>
    <link rel="alternate" type="text/html" href="http://journal.stuffwithstuff.com/2011/10/21/wrapping-my-head-around-optional-typing" />
    <id>http://journal.stuffwithstuff.com/2011/10/21/wrapping-my-head-around-optional-typing</id>
    <updated>2011-10-21T00:00:00Z</updated>
    <published>2011-10-21T00:00:00Z</published>
    <category scheme="http://journal.stuffwithstuff.com" term="language" />
    <category scheme="http://journal.stuffwithstuff.com" term="dart" />
    <summary type="html"><![CDATA[Wrapping My Head Around Optional Typing]]></summary>
    <content type="html" xml:base="http://journal.stuffwithstuff.com/2011/10/21/wrapping-my-head-around-optional-typing"><![CDATA[<p>One of the really cool parts about being involved with <a href="http://dartlang.org">Dart</a> is that I get a lot of first-hand experience with an optionally-typed language. I've been <a href="/2010/08/31/type-checking-a-dynamic-language/">fascinated</a> <a href="/2010/09/01/a-type-checking-conundrum/">by</a> <a href="/2010/10/29/bootstrapping-a-type-system/">optional typing</a> 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 <a href="http://www.strongtalk.org/">one of the original wellsprings</a>.</p>
<p>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't on that line. They float off in their own axis <a href="http://en.wikipedia.org/wiki/Complex_plane">like imaginary numbers</a>. In fact, I think they go off in <em>multiple</em> axes.</p>
<p>Before I go into how I <em>think</em> about them, I should probably lay down the basic semantics. Here'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 <a href="http://www.dartlang.org/articles/optional-types/">Gilad's less rambling version</a>.</p>
<h2>Type Annotations are Optional</h2>
<p>At it's heart, Dart is a dynamically-typed language, so you can code without any annotations:</p>
<div class="codehilite"><pre><span class="n">sum</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span>
  <span class="kt">var</span> <span class="n">result</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">;</span>
  <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<p>Here, you've said that <code>a</code>, <code>b</code>, and <code>result</code> can be any type at all and that'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:</p>
<div class="codehilite"><pre><span class="kt">num</span> <span class="n">sum</span><span class="p">(</span><span class="kt">num</span> <span class="n">a</span><span class="p">,</span> <span class="kt">num</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span>
  <span class="kt">var</span> <span class="n">result</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">;</span>
  <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<p>Now we've stated our intent that <code>a</code> and <code>b</code> be numbers. We've also said that <code>sum</code> should return a number. Note that we didn't annotate <code>result</code>. It can still be anything. Dart lets you mix and match untyped and typed code, so here the result of a <code>a + b</code> is assigned to an untyped variable. Likewise, we get the untyped <code>result</code> and use it as the return value for a function with a typed return.</p>
<p>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're prototyping and then start filling them in as you've nailed down the design.</p>
<h3>Tools Can Use Them</h3>
<p>OK, so you've sprinkled some types through your code. Why bother? What do they do? The most basic "feature" 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.</p>
<p>The next step up on the scale of usefulness is that, since they'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:</p>
<ol>
<li>
<p><strong>Types have a built-in declarative syntax.</strong> Unlike JavaScript, Python and other dynamic languages, Dart doesn't use an <em>imperative</em> 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's no runtime modifcation or monkey-patching that it needs to worry about.</p>
</li>
<li>
<p><strong>Variables can have a known type.</strong> 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.</p>
</li>
</ol>
<h3>They Can Be Checked at Runtime</h3>
<p>But tooling is gilding the lily. When you're talking about types, you expect, you know, a <em>type-checker</em>. You have that too (at least in the VM&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's as if every line of code like this:</p>
<div class="codehilite"><pre><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">someFunction</span><span class="p">();</span>
</pre></div>


<p>Turns into (more or less, simplifying things a bit) this:</p>
<div class="codehilite"><pre><span class="kt">var</span> <span class="n">_temp</span> <span class="o">=</span> <span class="n">someFunction</span><span class="p">();</span>
<span class="k">if</span> <span class="p">(</span><span class="n">_temp</span> <span class="k">is</span><span class="o">!</span> <span class="kt">int</span><span class="p">)</span> <span class="k">throw</span> <span class="s">&#39;Type error! Run for your life!&#39;</span><span class="p">;</span>
<span class="kt">var</span> <span class="n">i</span> <span class="o">=</span> <span class="n">_temp</span><span class="p">;</span>
</pre></div>


<p>You can think of every type annotation as an <em>expectation</em>: this thing <em>should</em> be a number here. In checked mode, the VM will constantly validate your expectations and stop if something doesn't hold.</p>
<p>It's important to note that these checks are done <em>dynamically</em>, at runtime. There isn't a separate static type checking pass. For example, if you have code like this:</p>
<div class="codehilite"><pre><span class="k">if</span> <span class="p">(</span><span class="mi">2</span> <span class="o">==</span> <span class="mi">3</span><span class="p">)</span> <span class="p">{</span>
  <span class="c1">// should never get here</span>
  <span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="s">&#39;not int&#39;</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<p>You won't see a type error here because it will never actually get inside the <code>if</code> block. You may be rightly wondering why in the hell you'd want to wait until runtime to find a type error instead of doing it statically. Dart's take is that you can do both.</p>
<p>Note that we said earlier that tools <em>can</em> do static type-checking if they want. What Dart does is <em>also</em> give you the option to perform those checks at runtime. This is actually how most static languages work too. Very few languages are <em>not</em> unsound, and in the presence of unsoundness, they have to check at runtime. Consider this Java code:</p>
<div class="codehilite"><pre><span class="n">String</span><span class="o">[]</span> <span class="n">array</span> <span class="o">=</span> <span class="k">new</span> <span class="n">String</span><span class="o">[</span><span class="mi">5</span><span class="o">];</span>
<span class="n">Object</span><span class="o">[]</span> <span class="n">untyped</span> <span class="o">=</span> <span class="n">array</span><span class="o">;</span>
<span class="n">untyped</span><span class="o">[</span><span class="mi">2</span><span class="o">]</span> <span class="o">=</span> <span class="mi">123</span><span class="o">;</span> <span class="c1">// not a string</span>
</pre></div>


<p>Here we'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't a string (but <em>is</em> an object) in it.</p>
<blockquote class="update">
<p><em>Update 2011/10/23:</em> I was wrongly using an int array here. Changed it to <code>string[]</code>. I didn't realize only arrays of reference types are covariant in Java.
</p>
</blockquote>

<p>The static type checker won't catch this because <a href="http://c2.com/cgi/wiki?JavaArraysBreakTypeSafety">arrays are covariant in Java</a>. That means that to ensure the last line doesn't crash your VM, it will do a <em>runtime</em> check every time you set an element in an array to make sure it's the right type.</p>
<p>There's another common case where you skirt around the static checker and rely on dynamic type tests: casts.</p>
<div class="codehilite"><pre><span class="kt">void</span> <span class="nf">callback</span><span class="o">(</span><span class="n">Object</span> <span class="n">data</span><span class="o">)</span> <span class="o">{</span>
  <span class="c1">// It&#39;s my callback, so I know the data is an int</span>
  <span class="kt">int</span> <span class="n">value</span> <span class="o">=</span> <span class="o">(</span><span class="n">Integer</span><span class="o">)</span> <span class="n">data</span><span class="o">;</span>
<span class="o">}</span>
</pre></div>


<p>There are times when you know more than the type system does and you just forcibly assert your knowledge. Doing so shouldn't let you just take down the VM (unlike in C++ where an improper type cast <em>can</em> set your house on fire), so every cast does a runtime check too.</p>
<p>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:</p>
<ol>
<li>
<p><strong>You don't have to rely on your type system for security.</strong> Java tries to
    rely on the type system and bytecode verification to ensure that code can't
    maliciously break the security guarantees of the VM. From what I've heard,
    doing so turned out to be unbelievably complicated.</p>
<p>Dart, on the other hand, can rely on a much simpler runtime model
(isolates) to ensure security boundaries.</p>
</li>
<li>
<p><strong>The type system can be less pessimistic.</strong> Static type systems, by their
    fundamental nature, are pessimistic. Since they don't know what <em>actual</em>
    code paths will be run at runtime and which <em>actual</em> types a variable will
    have, they err on the side of caution. They will report errors for code that
    <em>may</em> run, or variables that <em>may</em> have the wrong type.</p>
<p>This is good for ensuring real errors don't get missed, but it's drag when
it reports false positives. Consider:</p>
<div class="codehilite"><pre><span class="kt">bool</span> <span class="n">contains</span><span class="p">(</span><span class="n">List</span><span class="o">&lt;</span><span class="n">Object</span><span class="o">&gt;</span> <span class="n">collection</span><span class="p">,</span> <span class="n">Object</span> <span class="n">needle</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">for</span> <span class="p">(</span><span class="kd">final</span> <span class="n">item</span> <span class="n">in</span> <span class="n">collection</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">item</span> <span class="o">==</span> <span class="n">needle</span><span class="p">)</span> <span class="k">return</span> <span class="nb">true</span><span class="p">;</span>
  <span class="p">}</span>
  <span class="k">return</span> <span class="nb">false</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">var</span> <span class="n">numbers</span> <span class="o">=</span> <span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">];</span>
<span class="n">print</span><span class="p">(</span><span class="n">numbers</span><span class="p">.</span><span class="n">contains</span><span class="p">(</span><span class="mi">2</span><span class="p">));</span>
</pre></div>


<p>There's a type error here (according to most static type systems). We're
passing a <code>List&lt;int&gt;</code> to a function that takes a <code>List&lt;Object&gt;</code>, which
relies on <a href="http://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29">covariance</a>, but that
isn't statically safe. This <code>contains</code> method <em>could</em> call <code>collection.add("not an int")</code> and that would be an error.</p>
<p>However, it <em>doesn't actually do that</em>. 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 <em>actual</em> errors at runtime, we can
reduce the number of false positives that the type system chokes on.</p>
</li>
</ol>
<h3>They Can be Ignored at Runtime</h3>
<p>This is where things get weird. (Actually, if you're a type system person, they're already weird because covariant generics are wrong wrong wrong.) I've been talking about checked mode, but there's another mode: production mode. In that mode, the type annotations <em>completely ignored</em>. In other words, it will let you run this:</p>
<div class="codehilite"><pre><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="s">&#39;not int&#39;</span><span class="p">;</span>
<span class="kt">bool</span> <span class="n">b</span> <span class="o">=</span> <span class="s">&#39;not a bool either&#39;</span><span class="p">;</span>
<span class="kt">num</span> <span class="n">wtf</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="n">b</span><span class="p">;</span>
<span class="n">print</span><span class="p">(</span><span class="n">wtf</span><span class="p">);</span> <span class="c1">// not intnot a bool either</span>
</pre></div>


<p>This probably seems a little odd.</p>
<img alt="Your type errors will blot out the sun! Then we will code in the shade." src="/image/2011/10/daaart.jpeg" class="framed"/>

<p>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 <a href="http://code.google.com/closure/compiler/docs/js-for-compiler.html">crazy enough to do that</a>):</p>
<div class="codehilite"><pre><span class="kd">var</span> <span class="cm">/* int */</span> <span class="nx">i</span> <span class="o">=</span> <span class="s1">&#39;not int&#39;</span><span class="p">;</span>
<span class="kd">var</span> <span class="cm">/* bool */</span> <span class="nx">b</span> <span class="o">=</span> <span class="s1">&#39;not a bool either&#39;</span><span class="p">;</span>
<span class="kd">var</span> <span class="cm">/* num */</span> <span class="nx">wtf</span> <span class="o">=</span> <span class="nx">i</span> <span class="o">+</span> <span class="nx">b</span><span class="p">;</span>
<span class="nx">print</span><span class="p">(</span><span class="nx">wtf</span><span class="p">);</span> <span class="c1">// not intnot a bool either</span>
</pre></div>


<p>Unsurprisingly, those comments won't do anything at runtime. That's how Dart runs in production mode.</p>
<h2>How Should You Think of This?</h2>
<p>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.</p>
<p>That... doesn't really sound much like a "type system" compared to other languages. In fact, if you try to think of it as a type system, it's pretty disappointing. It's more like some type... 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 <em>type assertions</em>.</p>
<p><strong>Now I get it.</strong></p>
<p>If you've done C or C++ programming, you've probably used <a href="http://en.wikipedia.org/wiki/Assertion_%28computing%29"><code>assert()</code></a> or some flavor of it. If not, it looks like this:</p>
<div class="codehilite"><pre><span class="kt">float</span> <span class="nf">divide</span><span class="p">(</span><span class="kt">float</span> <span class="n">num</span><span class="p">,</span> <span class="kt">float</span> <span class="n">denom</span><span class="p">)</span> <span class="p">{</span>
  <span class="n">assert</span><span class="p">(</span><span class="n">denom</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">);</span>
  <span class="k">return</span> <span class="n">num</span> <span class="o">/</span> <span class="n">denom</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<p>That <code>assert</code> statement evaluates the condition in it. If it comes up false, then it stops the show and starts ringing the alarm bells. Actually, that's not entirely true. <em>If you run it in debug mode</em> and the assertion fails, then it halts. Most projects have at least two build configurations.</p>
<p>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 <em>release</em> mode. This is what you ship to customers and run in production. In that mode, the assertions are compiled out. They are erased completely.</p>
<p><em>Sound familiar?</em></p>
<p>Now why on Earth would you want to disable your asserts in release mode? Isn't that like wearing your life jacket in the boating classroom and then taking it off when you go out on the water?</p>
<p>It turns out that removing your asserts at runtime actually has a few advantages:</p>
<ol>
<li>
<p><strong>It's faster.</strong> 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.</p>
</li>
<li>
<p><strong>The app should try its hardest to continue.</strong> Once your program is in the
    customer's hands, you <em>really</em> don't want it to crash. It'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, <em>the show
    must go on.</em></p>
</li>
<li>
<p><strong>The user can't handle a failed assertion anyway.</strong> If you <em>were</em> 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... if you're a programmer on the project.</p>
<p>If you're just Joe User, that'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's nothing an end user can productively do with the knowledge that a bug in the code itself has manifested.</p>
</li>
</ol>
<p>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:</p>
<div class="codehilite"><pre><span class="kt">var</span> <span class="n">_temp</span> <span class="o">=</span> <span class="n">someFunction</span><span class="p">();</span>
<span class="k">assert</span><span class="p">(</span><span class="n">_temp</span> <span class="k">is</span> <span class="kt">int</span><span class="p">);</span>
<span class="kt">var</span> <span class="n">i</span> <span class="o">=</span> <span class="n">_temp</span><span class="p">;</span>
</pre></div>


<p>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.</p>
<p>By baking a certain flavor of assert (asserting on type) into the <em>syntax</em> of the language itself, Dart makes it easy for tools to parse those type annotations too and provide more contextual information about your code.</p>
<h2>Is It a Type System?</h2>
<p>"Type system" 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:</p>
<ul>
<li>Disappointed</li>
<li>Infuriated</li>
<li>Confused</li>
</ul>
<p>This doesn't mean it's a <em>bad feature</em>, just that it's not what you think it is. Meatloaf is a pretty terrible dessert, but it's a fine entrée. If you don't think about type systems and just ask yourself "is the set of features that Dart provides helpful in writing code?", I think the answer is "yes". It's just not helpful in exactly the same way that type systems in other languages help.</p>
<p>I don't really think of Dart as having a <em>type system.</em> I think of it as having <em>type requirements</em>. 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.</p>
<p>I find that's a pretty handy tool to have, and it's really nice to get that without having to give up the simplicity and flexibility of a dynamically typed language.</p>]]></content>
  </entry>
  <entry>
    <author>
      <name></name>
      <uri>http://journal.stuffwithstuff.com</uri>
    </author>
    <title type="html"><![CDATA[Semicolons are a Shibboleth]]></title>
    <link rel="alternate" type="text/html" href="http://journal.stuffwithstuff.com/2011/10/12/semicolons-are-a-shibboleth" />
    <id>http://journal.stuffwithstuff.com/2011/10/12/semicolons-are-a-shibboleth</id>
    <updated>2011-10-12T00:00:00Z</updated>
    <published>2011-10-12T00:00:00Z</published>
    <category scheme="http://journal.stuffwithstuff.com" term="code" />
    <category scheme="http://journal.stuffwithstuff.com" term="dart" />
    <summary type="html"><![CDATA[Semicolons are a Shibboleth]]></summary>
    <content type="html" xml:base="http://journal.stuffwithstuff.com/2011/10/12/semicolons-are-a-shibboleth"><![CDATA[<p>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 <a href="http://lambda-the-ultimate.org/node/4377">LtU</a> set, especially <a href="http://www.dartlang.org/articles/optional-types/">unsound ones</a>. And doing anything that <a href="http://www.2ality.com/2011/09/google-dart.html">approaches JavaScript</a> is bound to infuriate a set of people that are passionately devoted to JS and The Way of the Prototype.</p>
<p>So, really, <a href="http://www.dartlang.org/">Dart</a> was a perfect storm. It'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.</p>
<p>Dart requires semicolons as statement terminators just like C, C++, Java, and C# do. (And JavaScript unless you're <strike>brave</strike> insane enough to use <abbr title="Automatic Semicolon Insertion">ASI</abbr>.) Making them optional is currently <a href="https://code.google.com/p/dart/issues/detail?id=34">the second-most starred bug</a> on the tracker. It's one of the most frequently discussed points on <a href="http://www.reddit.com/r/programming/comments/l6uwv/dart_programming_language/">this insanely long reddit thread</a>.</p>
<p>Whatever your opinion of them, you have to admit they're a relatively innocuous language feature. Making them optional has very little impact on your code, and there isn't anything you can express without them that you couldn't with (and vice versa). What's the big deal?</p>
<p>Here's my theory: <strong>semicolons are a shibboleth to tell which language camp the designers come from.</strong> If you don't know the term, "<a href="http://en.wikipedia.org/wiki/Shibboleth">shibboleth</a>" comes from this passage in the Old Testament:</p>
<blockquote>
<p>Gilead then cut Ephraim off from the fords of the Jordan, and whenever
Ephraimite fugitives said, 'Let me cross,' the men of Gilead would ask, 'Are
you an Ephraimite?' If he said, 'No,' they then said, 'Very well, say
"Shibboleth" (שבלת).' If anyone said, "Sibboleth" (סבלת), 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.</p>
</blockquote>
<p class="cite">Judges 12:5-6, NJB</p>

<h2>The Two Camps</h2>
<p>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'm about to say, but I hope enough truth for this to be worth your time. Here's two opposing philosophies for programming languages:</p>
<ol>
<li>
<p>A language is a tool to tame the complexity of the problems we'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. <em>Good fences make good neighbors.</em></p>
</li>
<li>
<p>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. <em>We're all consenting
    adults here.</em></p>
</li>
</ol>
<p>I think you can divide languages pretty effectively into those two camps. The former gets the "serious business" languages: Fortran, C++, Java, C#. The latter gets you the hipster languages: JavaScript, Ruby, Python, Lisp.</p>
<p>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's type system is so... <em>optional</em>, that it sort of hovers in the <a href="http://en.wikipedia.org/wiki/Uncanny_valley">uncanny valley</a> between static and dynamic.</p>
<p>If you're trying to figure out whether you should give a damn about Dart, you'll want to know if its philosophy lines up with yours. You can look at the language today, but it's hard to know how much that tells you since the it's clearly not done yet.</p>
<h2>Semicolons</h2>
<p>So you ask the shibboleth question: "mandatory semicolons?"</p>
<p>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.</p>
<p>But a "serious business" programmer likes them: they are simple and unambiguous to parse. You don't have to worry about some other guy's weird coding style making it impossible for you to understand their code. We've been using semicolons for years, why stop now? They're safe and familiar.</p>
<p>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?</p>
<h2>So How Does Dart Pronounce It?</h2>
<p>So the question is, what will Dart do?</p>
<p>I honestly have no idea. I'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't think that necessarily says much about your or my other pet features being included.</p>
<p>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.</p>
<p>But, like everyone else, I hope they decide to go with <em>my</em> preference, which is of course the right one.</p>]]></content>
  </entry>
  <entry>
    <author>
      <name></name>
      <uri>http://journal.stuffwithstuff.com</uri>
    </author>
    <title type="html"><![CDATA[Time for a New Blog]]></title>
    <link rel="alternate" type="text/html" href="http://journal.stuffwithstuff.com/2011/09/17/time-for-a-new-blog" />
    <id>http://journal.stuffwithstuff.com/2011/09/17/time-for-a-new-blog</id>
    <updated>2011-09-17T00:00:00Z</updated>
    <published>2011-09-17T00:00:00Z</published>
    <category scheme="http://journal.stuffwithstuff.com" term="blog" />
    <category scheme="http://journal.stuffwithstuff.com" term="blogofile" />
    <summary type="html"><![CDATA[Time for a New Blog]]></summary>
    <content type="html" xml:base="http://journal.stuffwithstuff.com/2011/09/17/time-for-a-new-blog"><![CDATA[<p>I finally got tired enough of dealing with WordPress's admin interface and writing posts in HTML to do something about it, and I've redone this here blog. Now it's all <a href="http://www.blogofile.com">blogofile</a>. I can edit in <a href="http://daringfireball.net/projects/markdown/">markdown</a> and preview locally. All of my content is backed up just by pushing it to <a href="https://github.com/munificent/journal">github</a>.</p>
<p>It also got a facelift in the process.</p>
<h2>The New Design</h2>
<p>The old design was based around a 500 pixel column with 14 pixel text. As resolutions have grown, that started to look tiny. Now it's 640 pixels wide using a 16 pixel body font. Like the old site, the new one is designed to rigidly adhere to a <a href="http://typophile.com/node/47265">baseline grid</a>. This time, the grid is 24 pixels tall.</p>
<p>I'm using the <a href="http://www.google.com/webfonts">Google Font API</a> to go beyond the half dozen safe webfonts, but just barely. Titles are using <a href="http://www.google.com/webfonts/specimen/Varela+Round">Varela Round</a>, but should fallback to Georgia and look OK if it's missing.</p>
<p>The old design was a classic two column layout. This time around, I went with a single column. The entire site is now 640 pixels wide, almost like I was designing in 1998! I pushed all of the navigation down to the bottom.</p>
<p>Two reasons: First, when reading a post I wanted to remove as much clutter as possible. I wanted it to feel like reading a book without some constellation of navigation and social buttons in your peripheral vision vying for your attention.</p>
<p>And I figured a narrower layout would be more mobile friendly. On my phone, I think the new design looks pretty nice.</p>
<p>Since this was more or less redoing things from scratch, I probably broke some stuff. Let me know if you see something busted. In the meantime, I can get back to actually writing about stuff again!</p>]]></content>
  </entry>
</feed>

