<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;DUAHSX86cCp7ImA9WhRbF08.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634</id><updated>2012-02-08T11:02:18.118-08:00</updated><category term="flash" /><category term="icons" /><category term="pojo" /><category term="hosting" /><category term="scaling" /><category term="algorithms" /><category term="rubyonrails" /><category term="owl" /><category term="audio" /><category term="activewarehouse" /><category term="sudoku" /><category term="sqlserver" /><category term="git" /><category term="javapolis" /><category term="rails" /><category term="video" /><category term="email" /><category term="combinations" /><category term="xhtml" /><category term="mashup" /><category term="work" /><category term="googleio" /><category term="scripting" /><category term="thespringexperience" /><category term="facebook" /><category term="fixtures" /><category term="i18n" /><category term="aop" /><category term="information" /><category term="swoop" /><category term="aspectj" /><category term="tune" /><category term="networking" /><category term="crud" /><category term="rest" /><category term="io2011" /><category term="mvc" /><category term="controller" /><category term="interview" /><category term="qotd" /><category term="springwebflow" /><category term="Web Architecture" /><category term="dns" /><category term="groovy" /><category term="flickr" /><category term="view" /><category term="delicious" /><category term="rocketboom" /><category term="mac" /><category term="railsconf" /><category term="mp3" /><category term="framework" /><category term="j2ee" /><category term="owl1.1" /><category term="json" /><category term="subversion" /><category term="google" /><category term="pig" /><category term="Python" /><category term="activerecord" /><category term="alohaonrails" /><category term="messaging" /><category term="opendns" /><category term="song" /><category term="documentary" /><category term="osx" /><category term="consensus" /><category term="dhtml" /><category term="protege" /><category term="bigtable" /><category term="sparql" /><category term="tdd" /><category term="dataweb" /><category term="Crypto" /><category term="Social Networking" /><category term="Agile Programming" /><category term="springframework" /><category term="foaf" /><category term="math" /><category term="charts" /><category term="startup" /><category term="deployment" /><category term="arpanet" /><category term="music" /><category term="xslt" /><category term="cube" /><category term="bookmarks" /><category term="tivo" /><category term="netbeans" /><category term="inference" /><category term="railsconf07" /><category term="switchtower" /><category term="javascript extjs" /><category term="Ruby" /><category term="drools" /><category term="hioug" /><category term="health" /><category term="gmail" /><category term="html5 ruby rails" /><category term="openid" /><category term="magazine" /><category term="html5" /><category term="erlang" /><category term="web" /><category term="coucdb" /><category term="annotations" /><category term="deliciouslinks" /><category term="storage" /><category term="puzzle" /><category term="box2d" /><category term="ontology" /><category term="mapreduce" /><category term="hadoop" /><category term="presentation" /><category term="HTTP" /><category term="reasoner" /><category term="applications" /><category term="css" /><category term="folksonomy" /><category term="spring" /><category term="coordination" /><category term="migrations" /><category term="performance" /><category term="jess" /><category term="eclipse" /><category term="errorlytics" /><category term="chrome web store" /><category term="http://dmoz.org/Computers/Internet/On_the_Web/Weblogs/" /><category term="xp" /><category term="blogs" /><category term="grddl" /><category term="humor" /><category term="vanity" /><category term="contest" /><category term="acegi" /><category term="buttons" /><category term="business" /><category term="scala" /><category term="mysql" /><category term="soundboard" /><category term="semantic web" /><category term="esb" /><category term="XML" /><category term="algorithm" /><category term="cloud" /><category term="olap" /><category term="oracle" /><category term="microformats" /><category term="movie" /><category term="rspec" /><category term="dartlang" /><category term="expertspringmvc" /><category term="filesystem" /><category term="software" /><category term="#io2011" /><category term="html" /><category term="distributedsystems" /><category term="trendline" /><category term="newgame" /><category term="hjug" /><category term="jms" /><category term="xmlschema" /><category term="thespringframework" /><category term="w3c" /><category term="uri" /><category term="web design" /><category term="uh" /><category term="couchdb" /><category term="rules" /><category term="trust" /><category term="javascript" /><category term="fuse" /><category term="mule" /><category term="jena" /><category term="im" /><category term="postfix" /><category term="graphs" /><category term="conference" /><category term="graph" /><category term="Ontologies" /><category term="Programming" /><category term="browsers" /><category term="dart" /><category term="rdf" /><category term="transactions" /><category term="General" /><category term="browser" /><category term="forms" /><category term="aspects" /><category term="windows" /><category term="science" /><category term="database" /><category term="linux" /><category term="hibernate" /><category term="unittests" /><category term="php" /><category term="supermariobrothers" /><category term="ajax" /><category term="patterns" /><category term="tutorial" /><category term="timbl" /><category term="games" /><category term="oop" /><category term="petstore" /><category term="Java" /><category term="blog" /><category term="book" /><category term="springmvc" /><category term="nas" /><category term="regressionline" /><category term="Blogging" /><category term="pagination" /><category term="activeresource" /><category term="hawaii" /><category term="tags" /><category term="tests" /><category term="appengine" /><category term="radrails" /><category term="web2.0" /><category term="food" /><category term="search" /><category term="softwareengineering" /><category term="tagging" /><category term="model" /><category term="HTML amp; CSS" /><category term="spring.net" /><category term="capistrano" /><category term="data" /><category term="metadata" /><category term="pellet" /><title>Seth Ladd's Blog</title><subtitle type="html">We can do better.</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://blog.sethladd.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://blog.sethladd.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>842</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/SethLaddsBlog" /><feedburner:info uri="sethladdsblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;CkYNQX09cCp7ImA9WhRbF08.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-2947787058715503597</id><published>2012-02-07T22:48:00.000-08:00</published><updated>2012-02-08T08:56:30.368-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-02-08T08:56:30.368-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Booleans in Dart</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Cgrh94ZkhQeHMJTahqKnCpxV5lo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Cgrh94ZkhQeHMJTahqKnCpxV5lo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Cgrh94ZkhQeHMJTahqKnCpxV5lo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Cgrh94ZkhQeHMJTahqKnCpxV5lo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;(This is part 11 in an ongoing series about Dart. Read part 10, &lt;a href="http://blog.sethladd.com/2012/02/numbers-in-dart.html"&gt;Numbers in Dart&lt;/a&gt;.)&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Intro&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This post will cover what is true, and false, in Dart. If you are coming from JavaScript, hopefully this short explanation about booleans in Dart will come as welcome relief.&lt;br /&gt;
&lt;br /&gt;
Dart has a formal boolean type, named &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;bool&lt;/span&gt;. There are only two objects of type bool: &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;true&lt;/span&gt; and &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;false&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;bool registered = false;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;var verified &amp;nbsp; &amp;nbsp;= true;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;True&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The only value that is true is the boolean value true. Unlike JavaScript, other values such as 1 or non-null object are not treated as true.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;False&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In a boolean context, everything that is not true is converted to false. JavaScript has six other falsey values such as empty string, zero, null object, undefined, or NaN. In Dart, if an object is not the boolean value true, it is evaluated as false.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Boolean conversion&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
What happens when you write the following code?&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;var name = 'Bob';&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;if (name) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; print("you have a name!");&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In JavaScript, the above will print "you have a name!" because name is a non-null object. However, in Dart, the above will &lt;i&gt;not&lt;/i&gt; print because name is converted to &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;false&lt;/span&gt; due to boolean conversion.&lt;br /&gt;
&lt;br /&gt;
The conversion rules to boolean are quite simple. The spec outlines the logic as:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;(bool v){&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; assert(v != null);&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; return v === true;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;}(o)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In other words, that which is not true is false. If the value is not equal to and of the same type as true, then the value is converted to false.&lt;br /&gt;
&lt;br /&gt;
This means that the following code will work in JavaScript but will not work in Dart:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;// this won't print in Dart&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;if (1) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; print("1 is converted to false in Dart, as is everything that is not the value true");&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Compensating&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Coming from JavaScript, you may be wondering how to check for conditions such as empty string or zero. The long form checks will still work.&lt;br /&gt;
&lt;br /&gt;
Check for empty string:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;var fullName = '';&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;if (fullName.isEmpty()) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; print("Please enter a full name");&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Checking for zero:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;var hitPoints = 0;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;if (hitPoints == 0) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; print("Uh oh! Looks like you died.");&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Checking for null:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;var unicorn = null;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;if (unicorn == null) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; print("You didn't wish hard enough. Wish harder.");&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Checking for NaN:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;var iMeantToDoThis = 0 / 0;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;if (iMeantToDoThis.isNaN()) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; print("it's all on purpose");&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;JavaScript puzzler&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart also differs from JavaScript with respect to its treatment of boolean expressions.&lt;br /&gt;
&lt;br /&gt;
In JavaScript, boolean expressions will resolve to the value of the object that stops the evaluation. For instance:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;var yourName = 'Bob';&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;var myName = '';&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;var someName = yourName &amp;amp;&amp;amp; myName;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;console.log(someName);&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;// prints '' (empty string!)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Both &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;yourName&lt;/span&gt; and &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;myName&lt;/span&gt; will be evaluated, and because &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;yourName&lt;/span&gt; is truthy the next value must be evaluated. &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;myName&lt;/span&gt; is an empty string, which is falsey, but that doesn't matter to the value of &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;someName&lt;/span&gt;. The last value that is evaluated is returned, so &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;someName&lt;/span&gt; is set to an empty string.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Dart answer&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart simplifies the behavior, because the result of a boolean expression is a boolean. Therefore, the following Dart code reads more logically.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;main() {&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &lt;span class="s1"&gt;var&lt;/span&gt; firstName = &lt;span class="s2"&gt;'Bob'&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &lt;span class="s1"&gt;var&lt;/span&gt; middleName = &lt;span class="s2"&gt;''&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &lt;span class="s1"&gt;var&lt;/span&gt; name = firstName &amp;amp;&amp;amp; middleName;&lt;/span&gt;&lt;/div&gt;
&lt;div class="p2"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="s3"&gt;&amp;nbsp; print(name); &lt;/span&gt;// false, because boolean conversion turns&lt;/span&gt;&lt;/div&gt;
&lt;div class="p2"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="s3"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;// firstName into false&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; print(name &lt;span class="s1"&gt;is&lt;/span&gt; bool); &lt;span class="s4"&gt;// true!&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="font-size: large;"&gt;Assigning default values&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
Dart's simplified boolean handling also means a few tricks from JavaScript or Ruby don't work.&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
For example, assigning a default value in Ruby is as simple as:&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;# ruby code which assigns 25 to hit_points if it was null&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;hit_points ||= 25&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
Assigning a default value in JavaScript looks like:&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;// works in JavaScript&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;var hitPoints = hitPoints || 25;&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
However, because Dart boolean expressions return a boolean, and not the last value, we need to write:&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;var hitPoints = (hitPoints != null) ? hitPoints : 25;&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
This is a bit easier in Dart with functions, as default values are supported with function arguments:&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;grabADrink([size='pint']) {&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; print(size);&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"&gt;grabADrink(); // prints 'pint'&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="font-size: large;"&gt;Summary&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
Dart has a true boolean type named bool, with only two values: &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;true&lt;/span&gt; and &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;false&lt;/span&gt; (and, I suppose, &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;null&lt;/span&gt;). Due to Dart's boolean conversion rules, all values expect true are converted to false. Dart's boolean expressions return the actual boolean result, not the last encountered value.&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span style="font-size: large;"&gt;Next Steps&lt;/span&gt;&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;br /&gt;&lt;/div&gt;
Read more of my &lt;a href="http://blog.sethladd.com/search/label/dartlang"&gt;Dart posts&lt;/a&gt;, &lt;a href="http://try.dartlang.org/"&gt;try Dart in your browser&lt;/a&gt;, &lt;a href="http://api.dartlang.org/"&gt;browse the API docs&lt;/a&gt;, or &lt;a href="http://dartbug.com/"&gt;file an issue request&lt;/a&gt;. Dart is getting ready, please send us your feedback!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-2947787058715503597?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/fYV2BBZ5rtk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/2947787058715503597/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=2947787058715503597&amp;isPopup=true" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/2947787058715503597?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/2947787058715503597?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/fYV2BBZ5rtk/booleans-in-dart.html" title="Booleans in Dart" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://blog.sethladd.com/2012/02/booleans-in-dart.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUMAQn47eSp7ImA9WhRbE0U.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-4937361593474350885</id><published>2012-02-04T12:30:00.000-08:00</published><updated>2012-02-04T12:30:43.001-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-02-04T12:30:43.001-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Numbers in Dart</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/JmDjYQ8CdA6oStfMFklUZr5H0KM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/JmDjYQ8CdA6oStfMFklUZr5H0KM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/JmDjYQ8CdA6oStfMFklUZr5H0KM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/JmDjYQ8CdA6oStfMFklUZr5H0KM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;(This is part 10 in an ongoing series about Dart. Check out part 9, a &lt;a href="http://blog.sethladd.com/2012/01/dart-sdk-quick-tour.html"&gt;Dart SDK quick tour&lt;/a&gt;.)&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Intro&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
It's time to look at numbers in Dart. Wait, where are you going? Come back, numbers are cool! This will be interesting, for reals. I can at least promise this will be easy and quick, for numbers in Dart aren't complicated. If you're compiling to JavaScript, though, there are a few edge cases (bugs?) so I encourage you to not skip this episode.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;The Basics&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Like everything else in Dart, numbers are objects. In fact, numbers are specified as interfaces.&lt;br /&gt;
&lt;br /&gt;
Numbers come in two flavors in Dart: &lt;i&gt;integers of arbitrary size&lt;/i&gt; and &lt;i&gt;decimal 64 bit doubles&lt;/i&gt; as specified by the IEEE 754 standard. Both &lt;a href="http://api.dartlang.org/dart_core/int.html"&gt;int&lt;/a&gt; and &lt;a href="http://api.dartlang.org/dart_core/double.html"&gt;double&lt;/a&gt; are subinterfaces of &lt;a href="http://api.dartlang.org/dart_core/num.html"&gt;num&lt;/a&gt;.&lt;br /&gt;
&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-L3BjdXD1YQs/Ty2MANiJnCI/AAAAAAAACaU/7uS_SDIigyA/s1600/Screen+shot+2012-02-04+at+11.50.02+AM.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-L3BjdXD1YQs/Ty2MANiJnCI/AAAAAAAACaU/7uS_SDIigyA/s1600/Screen+shot+2012-02-04+at+11.50.02+AM.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;num is the supertype, int and double extend num.&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
The num interface defines the basics like +, -, /, and *. Num is where you'll also find &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;abs()&lt;/span&gt;, &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;ceil()&lt;/span&gt;, and &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;floor()&lt;/span&gt;, among other methods.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Int&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Integers are numbers without a decimal point. For example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  var x = 1;
  print(x);
  print(x is num); // true in both Dart VM and JS
  print(x is int); // true in both Dart VM and JS&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
You may also specify hexadecimals with the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;0x&lt;/span&gt; prefix.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  var hex = 0xDEADBEEF;
  print(hex); // 873735928559
  print(hex is num); // true in both Dart VM and JS
  print(hex is int); // true in both Dart VM and JS
  print(hex is! double); // true in Dart VM, &lt;b&gt;FALSE in JavaScript&lt;/b&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="background-color: #ea9999;"&gt;Alert: the last line in the above sample, the result is different between the Dart VM and Dart compiled to JavaScript. I've opened a &lt;a href="http://code.google.com/p/dart/issues/detail?id=1533"&gt;bug&lt;/a&gt; to see if this is by design.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Integers can be of arbitrary precision. For example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  var bigInt = 346538465234590347592847298346243459756895347698465298346583746592374652378462347569234765834765947569827346583465243765923847659234765928347659567398475647495873984572947593470294387093493456870849216348723763945678236420938467345762304958724596873045876234572037862934765294365243652548673456705673465273465246734506873456729457623845623456234650457693475603768922346728346256;
  print(bigInt); // correct in Dart VM, &lt;b&gt;Infinity in JavaScript&lt;/b&gt;
  print(bigInt is num); // true in both Dart VM and JS
  print(bigInt is int); // true in both Dart VM and JS
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
While the above will work in the Dart VM, when compiled to JavaScript, the large number becomes Infinity.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #ea9999;"&gt;Alert: JavaScript only supports&amp;nbsp;+/- 9007199254740992 for integers and (at least in V8)&amp;nbsp;1.7976931348623157e+308 for &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;Number.MAX_VALUE&lt;/span&gt;. I've opened a &lt;a href="http://code.google.com/p/dart/issues/detail?id=1534"&gt;bug&lt;/a&gt; to discuss this, as the above code functions differently in Dart VM and JavaScript.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
You might see Dart code that explicitly adds a + prefix to the number, though it isn't necessary.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  var positive = +12345;
  print(positive); // 12345
  print(positive is num); // true
  print(positive is int); // true
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Double&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
If the number includes a decimal, it is a 64 bit double that follows IEEE 754 standard.&lt;br /&gt;
&lt;br /&gt;
A simple example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  var y = 1.1;
  print(y);
  print(y is num); // true in both Dart VM and JS
  print(y is double); // true in both Dart VM and JS
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Exponent notation is supported:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  var exponents = 1.42e5;
  print(exponents); // prints 142000.0 in Dart VM, &lt;b&gt;142000 in JavaScript&lt;/b&gt;
  print(exponents is num); // true in both Dart VM and JS
  print(exponents is! int); // true in both Dart VM, &lt;b&gt;FALSE in JS&lt;/b&gt;
  print(exponents is double); // true in both Dart VM and JS
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Here again you can see slight differences between what Dart VM and JavaScript will do. The generated JavaScript code looks like this:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  // generated JavaScript from Frog, as of 2012-02-04
  var exponents = (142000.0);
  print(exponents);
  print((typeof(exponents) == 'number'));
  print((typeof(exponents) != 'number')); // this is FALSE, see corresponding Dart code above
  print((typeof(exponents) == 'number'));
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;NaN&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dividing zero by zero will result in "not a number" in Dart, similar to JavaScript but unlike Java.&lt;br /&gt;
&lt;br /&gt;
The num interface provides a simple check for &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;isNaN()&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  var nan = 0 / 0;
  print(nan.isNaN()); // true in both Dart VM and JS
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Converting to and from a String&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
A common requirement is to turn a number into a String, or a String into a number.&lt;br /&gt;
&lt;br /&gt;
Parsing a String to an int uses the Math class:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  var one = Math.parseInt("1");
  print(one);  // 1
  print(one is int);  // true
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Parsing a String into a double is similar:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  var onePointOne = Math.parseDouble("1.1");
  print(onePointOne); // 1.1
  print(onePointOne is double); // true
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Attempting to format a string that isn't a number will throw a &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;BadNumberFormatException&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  try {
    var gook = Math.parseInt("foo");
  } catch(BadNumberFormatException e) {
    print(e); // BadNumberFormatException: 'foo'
  }
  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Going the other way, converting numbers to Strings, is also very easy thanks to &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;toString()&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  String oneAsString = 1.toString();
  print(oneAsString); // 1
  print(oneAsString is String);
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
For doubles, toStringAsFixed(int fractionDigits) will truncate the decimal digits:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  print(3.14159.toStringAsFixed(2)); // 3.14
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Misc methods&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
A potentially unfamiliar change is the methods like &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;ceil()&lt;/span&gt; and &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;abs()&lt;/span&gt; are found on the &lt;a href="http://api.dartlang.org/dart_core/num.html"&gt;num interface&lt;/a&gt;, and not in &lt;a href="http://api.dartlang.org/dart_core/Math.html"&gt;Math&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  // absolute value is a method on num
  print((-4)&lt;b&gt;.abs()&lt;/b&gt; == 4);
  
  // ceil and floor are also methods on num
  print(3.25&lt;b&gt;.ceil()&lt;/b&gt; == 4.0);
  print(3.25&lt;b&gt;.floor()&lt;/b&gt; == 3.0);
  
  // round will round up if .5 or greater, otherwise will round down
  print(3.25&lt;b&gt;.round()&lt;/b&gt; == 3);
  print(3.89.round() == 4);
  print(3.5.round() == 4);
  print(3.49.round() == 3);
  
  // truncates the decimal digits
  print(3.24532&lt;b&gt;.truncate()&lt;/b&gt; == 3.0);
  
  // convert a double to an int
  print(3.24&lt;b&gt;.toInt()&lt;/b&gt; == 3);
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Bit manipulating&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The num interface also specifies the traditional bit shifting, and'ing, and or'ing. Some simple examples:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;  print(3&amp;lt;&amp;lt;1 == 6); // 0011 &amp;lt;&amp;lt; 1 == 0110
  print(3&amp;gt;&amp;gt;1 == 1); // 0011 &amp;gt;&amp;gt; 1 == 0001
  
  print((3|4) == 7); // 0011 | 0100 == 0111
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Summary&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart supplies a num interface with two subinterfaces: int and double. Integers are arbitrary precision, meaning they have arbitrary size. Doubles are 64 bit decimals that follow IEEE 754 spec.&lt;br /&gt;
&lt;br /&gt;
Generally speaking, numbers function the same across native Dart and Dart compiled to JavaScript, but there are some subtle differences. For example, watch out for very large numbers and checking the difference between int and double in JavaScript (currently everything is checked against number in JavaScript from the Frog compiler.)&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Next Steps&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Read more of my &lt;a href="http://blog.sethladd.com/search/label/dartlang"&gt;Dart posts&lt;/a&gt;, &lt;a href="http://try.dartlang.org/"&gt;try Dart in your browser&lt;/a&gt;, &lt;a href="http://api.dartlang.org/"&gt;browse the API docs&lt;/a&gt;, or &lt;a href="http://dartbug.com/"&gt;file an issue request&lt;/a&gt;. Dart is getting ready, please send us your feedback!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-4937361593474350885?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/1anDn6hPaUM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/4937361593474350885/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=4937361593474350885&amp;isPopup=true" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/4937361593474350885?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/4937361593474350885?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/1anDn6hPaUM/numbers-in-dart.html" title="Numbers in Dart" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-L3BjdXD1YQs/Ty2MANiJnCI/AAAAAAAACaU/7uS_SDIigyA/s72-c/Screen+shot+2012-02-04+at+11.50.02+AM.png" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://blog.sethladd.com/2012/02/numbers-in-dart.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUcBRX8zfSp7ImA9WhRUFEo.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-6022991191778439663</id><published>2012-01-24T22:30:00.000-08:00</published><updated>2012-01-24T22:30:54.185-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-24T22:30:54.185-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Chromium and Dart with Dev Tools</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/fJJa3kOEQHYvyREqDF6BTyAoBJk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/fJJa3kOEQHYvyREqDF6BTyAoBJk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/fJJa3kOEQHYvyREqDF6BTyAoBJk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/fJJa3kOEQHYvyREqDF6BTyAoBJk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;There is a public branch of Chromium that embeds the Dart virtual machine, allowing you to run native Dart code directly in the browser. Unfortunately, only the &lt;a href="http://code.google.com/p/dart/wiki/BuildingDartium"&gt;Dartium source&lt;/a&gt; is available right now, but if you search hard enough, you might find an enterprisingly fellow providing &lt;a href="http://financecoding.wordpress.com/2012/01/20/dart-updated-dartium-builds-for-linux-and-mac/"&gt;pre-built binaries&lt;/a&gt; (warning: these may blow up your machine.)&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
The engineers have begun to add Developer Tools support with Dart, which starts to tell the debugging story. With a very recent build of Dartium (2012-01-24) you can try this for yourself. I found breakpoints are working, with a few caveats (eg not working with function callbacks yet.)&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
To see Chromium + Dart + Dev tools + breakpoints in action:&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;iframe allowfullscreen="" frameborder="0" height="360" src="http://www.youtube.com/embed/y32f8AD9MBI" width="640"&gt;&lt;/iframe&gt;

&lt;br /&gt;
&lt;br /&gt;
Dart is getting ready, please do try it out by &lt;a href="http://www.dartlang.org/"&gt;learning more&lt;/a&gt;, &lt;a href="http://api.dartlang.org/"&gt;browsing the API&lt;/a&gt;, using the &lt;a href="http://www.dartlang.org/docs/getting-started/editor/index.html"&gt;Editor&lt;/a&gt;, or &lt;a href="http://blog.sethladd.com/2012/01/dart-sdk-quick-tour.html"&gt;downloading the SDK&lt;/a&gt;. I also have a &lt;a href="http://blog.sethladd.com/search/label/dartlang"&gt;series of posts on Dart&lt;/a&gt;. Please let us know what you think!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-6022991191778439663?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/Spypj6zLgtA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/6022991191778439663/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=6022991191778439663&amp;isPopup=true" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/6022991191778439663?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/6022991191778439663?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/Spypj6zLgtA/chromium-and-dart-with-dev-tools.html" title="Chromium and Dart with Dev Tools" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://img.youtube.com/vi/y32f8AD9MBI/default.jpg" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://blog.sethladd.com/2012/01/chromium-and-dart-with-dev-tools.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUIEQHYyeSp7ImA9WhRbE0U.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-5756696316993770863</id><published>2012-01-22T10:57:00.000-08:00</published><updated>2012-02-04T12:31:41.891-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-02-04T12:31:41.891-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Dart SDK Quick Tour</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/i_HAYIkn9ANXSHeNHeULigx632k/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/i_HAYIkn9ANXSHeNHeULigx632k/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/i_HAYIkn9ANXSHeNHeULigx632k/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/i_HAYIkn9ANXSHeNHeULigx632k/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;(This is part 9 in an ongoing series about Dart. Check out part 8, &lt;a href="http://blog.sethladd.com/2012/01/this-is-this-in-dart.html"&gt;This is this in Dart&lt;/a&gt;.)&lt;br /&gt;
&lt;br /&gt;
The Dart team quietly started to provide binary Dart SDKs for download. Without compiling or syncing large source repositories, you can now compile your Dart code to JavaScript with Frog, run the Dart VM, and browse the Dart libraries.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Download&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
There are two options for downloading the Dart SDK: &lt;a href="http://gsdview.appspot.com/dart-editor-archive-continuous/latest/"&gt;continuous nightlies&lt;/a&gt; or &lt;a href="http://gsdview.appspot.com/dart-editor-archive-integration/latest/"&gt;integration releases&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Choose the continuous builds if you want to live on the edge, or choose the integration builds if you want more tested releases.&lt;br /&gt;
&lt;br /&gt;
For the SDK, look for either dart-linux.zip, dart-macos.zip, or dart-win32.zip. The other files (starting with DartBuild) are the Dart Editor.&lt;br /&gt;
&lt;br /&gt;
After you download and unzip the archive, you'll have a &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;dart-sdk&lt;/span&gt; directory. The &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;bin&lt;/span&gt; directory has &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;dart&lt;/span&gt;, which is the Dart VM, and &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;frogc&lt;/span&gt;, the Frog compiler.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;The Frog compiler&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
When Dart was initially announced, we shipped a DartC compiler which turned your Dart code into JavaScript code. DartC is written in Java, and is the compiler behind the Dart Editor. DartC supported incremental compilation (making the editor faster) but generated large JavaScript.&lt;br /&gt;
&lt;br /&gt;
Some time later, the team released an experiment: a Dart to JavaScript compiler written in Dart itself. This compiler would generate essentially human readable, and terse, JavaScript. This Frog compiler (named after the Dart Frog) will generate only the JavaScript required to run the program, resulting in smaller output.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Running Frog&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Assuming you have the following &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;test.dart&lt;/span&gt; file:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() =&amp;gt; print("hello from dart!");
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
you can then compile it to JavaScript with the following command:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;$DART_SDK/bin/frogc test.dart
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
which will generate the following JavaScript code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;//  ********** Library dart:core **************
//  ********** Natives dart:core **************
// ********** Code for Object **************
Object.defineProperty(Object.prototype, "toString$0", { value: function() {
  return this.toString();
}, enumerable: false, writable: true, configurable: true });
// ********** Code for top level **************
function print(obj) {
  return _print(obj);
}
function _print(obj) {
  if (typeof console == 'object') {
    if (obj) obj = obj.toString();
    console.log(obj);
  } else {
    write(obj);
    write('\n');
  }
}
//  ********** Library dart:coreimpl **************
// ********** Code for NumImplementation **************
NumImplementation = Number;
// ********** Code for StringImplementation **************
StringImplementation = String;
// ********** Code for _Worker **************
// ********** Code for top level **************
//  ********** Library test **************
// ********** Code for top level **************
function main() {
  return print("hello from dart!");
}
main();

&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;The Dart virtual machine&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
One of Dart's primary language design constraints is that it must compile to effective and performant JavaScript. However, Dart is also designed to run in its own virtual machine, and a Dart VM is shipped inside the Dart SDK. You can use the Dart VM to run Dart code on the server or directly in a browser (with Chrome + Dart integration coming soon.)&lt;br /&gt;
&lt;br /&gt;
Using the same test.dart file from above, we can run it directly on the command line with:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;$ ~/Downloads/dart-sdk/bin/dart test.dart
hello from dart!
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Startup for the Dart VM is very fast, too.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;The libraries&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The source code for the Dart libraries is also included in the SDK. You can browse the API docs at &lt;a href="http://api.dartlang.org/"&gt;api.dartlang.org&lt;/a&gt;, but it's still fun to check out the actual source.&lt;br /&gt;
&lt;br /&gt;
You'll find directories for the core libs, the DOM and HTML libs, and the frog compiler. You'll also see a directory named &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;builtin&lt;/span&gt;, which includes runtime libraries like directories, file, sockets, and streams.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Summary&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
You can play with Dart today, without having to compile any code. The Dart SDK is now available for download, bundling the Frog compiler, the Dart virtual machine, and the source code for the libraries.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Next steps&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Continue to part 10, &lt;a href="http://blog.sethladd.com/2012/02/numbers-in-dart.html"&gt;Numbers in Dart&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Read more of my &lt;a href="http://blog.sethladd.com/search/label/dartlang"&gt;Dart posts&lt;/a&gt;, &lt;a href="http://try.dartlang.org/"&gt;try Dart in your browser&lt;/a&gt;, &lt;a href="http://api.dartlang.org/"&gt;browse the API docs&lt;/a&gt;, or &lt;a href="http://dartbug.com/"&gt;file an issue request&lt;/a&gt;. Dart is getting ready, please send us your feedback!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-5756696316993770863?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/IyBzxIzc0nI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/5756696316993770863/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=5756696316993770863&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/5756696316993770863?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/5756696316993770863?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/IyBzxIzc0nI/dart-sdk-quick-tour.html" title="Dart SDK Quick Tour" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.sethladd.com/2012/01/dart-sdk-quick-tour.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUQGQHkzeip7ImA9WhRUEkw.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-5056402526890200634</id><published>2012-01-21T22:22:00.000-08:00</published><updated>2012-01-21T22:22:01.782-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-21T22:22:01.782-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>JavaScript WAT moments and Dart</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/vNwV_C97wsqRNdJJgODPf3NKuuA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vNwV_C97wsqRNdJJgODPf3NKuuA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/vNwV_C97wsqRNdJJgODPf3NKuuA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vNwV_C97wsqRNdJJgODPf3NKuuA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-I1luCANnsD8/TxumLoOTS8I/AAAAAAAACXU/1v4700PIATo/s1600/3666d9c352248646722b0f1b7ae8b8b0.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="318" src="http://4.bp.blogspot.com/-I1luCANnsD8/TxumLoOTS8I/AAAAAAAACXU/1v4700PIATo/s320/3666d9c352248646722b0f1b7ae8b8b0.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
There's a very funny 5 minute video by Gary Bernhardt from CodeMash 2012 which highlights a few &lt;a href="https://www.destroyallsoftware.com/talks/wat"&gt;WAT??!?!&lt;/a&gt; moments with some of our favoriate languages like Ruby and JavaScript. If you haven't seen this yet, take a few minutes and give yourself a chuckle.&lt;br /&gt;
&lt;br /&gt;
Back? Cool. Let's map some of those JavaScript WAT moments to Dart and see how we do.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Example 1: Adding two empty arrays&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Behold, the JavaScript:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;[] + []

&amp;gt; "" // ?!?!?!&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Not expected.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Behold, the Dart:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
(note, List does not have a + operator, so here's the best we can do)&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;var result = [];
result.addAll([]);

print(result is List); // true.&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Expected.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Example 2: Adding an empty array and empty map&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Behold, the JavaScript:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;[] + {};

&amp;gt; "[object Object]"  // ?!?!?!&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
That's right, a string. Not expected.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Behold, the Dart:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
(note, Map does not extend collection, so not only can't we use the + operator, it doesn't make sense to directly add a Map to a List.)&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;var result = [];

[].addAll({}); // &lt;span style="color: red;"&gt;NoSuchMethodException: Map is not a Collection! Expected!&lt;/span&gt;&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Not being allowed to add a map and array is expected. I am thankful the program stops if I try to do this.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Example 3: Adding an empty map and empty array&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Behold, the JavaScript:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;{} + []

&amp;gt; 0&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The number zero. Not expected.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Behold, the Dart:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// nothing even close&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
There's no way to simply add a Map and a List. This makes sense, it is expected you can't just add a list to a map.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Example 4: Adding an object to an object&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Behold, the JavaScript:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;{} + {}

&amp;gt; NaN&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Which, as the video says, is probably right. :) &amp;nbsp;However, still unexpected that this is allowed to run.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Behold, the Dart:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// nothing comes close
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Neither Map nor Object have implemented the + operator, so it's impossible to try to add two objects or two maps together. It is expected that you can't simply add two arbitrary objects.&lt;br /&gt;
&lt;br /&gt;
(Note: Dart does support the ability to define an operator method on a class. You are free to add plus or minus, for example, to your classes.)&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Example 5: Subtracting a number from a string&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Behold, the JavaScript:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;"wat" - 1
&amp;gt; NaN
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
One could argue that yes, indeed, the result of subtracting a number from a string is not a number. I will grant you this. However, the fact that this line is even allowed to run and return a result is not expected.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Behold, the Dart:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;var result = "wat" - 1;

// analysis will generate warning: string has no operator method '-'

// attempting to execute this will throw NoSuchMethodException
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Dart will warn you that string does not have an operator method '-', but it will let you execute the program (due to types not affecting the runtime semantics of the code, because Dart embrases optional types.)&lt;br /&gt;
&lt;br /&gt;
If you don't heed the warning, the above code will fail with a NoSuchMethodException, because string doesn't have a minus operator. This is expected.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Summary&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The point of this post isn't to pick on JavaScript specifically, as every language eventually ends up with odd puzzlers. The point of this post is to instead remind you that there are people working very hard to reduce and remove these subtle and confusing situations from your next web programming language.&lt;br /&gt;
&lt;br /&gt;
We can indeed demand more expected results from our languages and tools, and we should demand more early errors from obvious mistakes. &amp;nbsp;We need to demand less WAT?!?! moments in web development.&lt;br /&gt;
&lt;br /&gt;
(BTW if anyone knows of the ES6 aka JavaScript.next aka Harmony versions of the above code, I'll be happy to include them. I'm curious if the latest work on the future of JavaScript addresses the above issues.)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-5056402526890200634?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/Fes5toB8zdQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/5056402526890200634/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=5056402526890200634&amp;isPopup=true" title="6 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/5056402526890200634?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/5056402526890200634?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/Fes5toB8zdQ/javascript-wat-moments-and-dart.html" title="JavaScript WAT moments and Dart" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-I1luCANnsD8/TxumLoOTS8I/AAAAAAAACXU/1v4700PIATo/s72-c/3666d9c352248646722b0f1b7ae8b8b0.jpg" height="72" width="72" /><thr:total>6</thr:total><feedburner:origLink>http://blog.sethladd.com/2012/01/javascript-wat-moments-and-dart.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkENQ3w-fCp7ImA9WhRUEkg.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-8400944799421354229</id><published>2012-01-21T00:20:00.000-08:00</published><updated>2012-01-22T10:58:12.254-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-22T10:58:12.254-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>This is this in Dart</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/I_gzGj4wbh37mAgY-TVqvC8aFmg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/I_gzGj4wbh37mAgY-TVqvC8aFmg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/I_gzGj4wbh37mAgY-TVqvC8aFmg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/I_gzGj4wbh37mAgY-TVqvC8aFmg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;(This is part 8 in an ongoing series about Dart. Check out part 7, &lt;a href="http://blog.sethladd.com/2012/01/strings-in-dart.html"&gt;Strings in Dart&lt;/a&gt;.)&lt;br /&gt;
&lt;br /&gt;
One of the things that's always confused me about JavaScript is the changing definition of &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;this&lt;/span&gt;. This post will show how Dart avoids the confusion with more logical bindings.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;The Problem&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Here's some JavaScript code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;function Awesome() {
}

Awesome.prototype.cool = function() {
&amp;nbsp; &amp;nbsp; alert("inside awesome");
}

Awesome.prototype.init = function(button) {
&amp;nbsp; &amp;nbsp; button.addEventListener("click", function() {
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.cool(); &lt;span style="color: red;"&gt;// this won't work!&lt;/span&gt;
&amp;nbsp; &amp;nbsp; });
}

var button = document.getElementById("b");
var a = new Awesome();
a.init(button);&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
As you can see, I've created a simple Awesome object with two methods. The &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;cool()&lt;/span&gt; method prints a simple message. The &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;init()&lt;/span&gt; method adds an event listener to a button, which calls &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;cool()&lt;/span&gt; when clicked.&lt;br /&gt;
&lt;br /&gt;
At first glance, this seems like it will work. However, because the definition of &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;this&lt;/span&gt; is relative to the current context of executing code, it does not refer to the instance of Awesome. When inside the event callback, &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;this&lt;/span&gt; refers to the button!&lt;br /&gt;
&lt;br /&gt;
A common pattern to address this problem in JavaScript is to declare a new variable in the function which points to &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;this&lt;/span&gt; before the context is changed.&lt;br /&gt;
&lt;br /&gt;
For example, the following code fixes the problem:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;Awesome.prototype.init = function(button) {
&amp;nbsp; &amp;nbsp; &lt;b&gt;var _this = this;&lt;/b&gt;
&amp;nbsp; &amp;nbsp; button.addEventListener("click", function() {
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;b&gt;_this.cool()&lt;/b&gt;; // this works now
&amp;nbsp; &amp;nbsp; });
}&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
This is not intuitive to developers coming from other platforms, and requires more ceremony than is probably necessary.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Dart's Solution&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The following Dart code is more clear and hopefully more logical. You can see the same structure, but the main difference is that there's no changing definition of &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;this&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;#import('dart:html');

class Awesome {

&amp;nbsp; cool() {
&amp;nbsp; &amp;nbsp; window.alert("inside cool");
&amp;nbsp; }

&amp;nbsp; init(button) {
&amp;nbsp; &amp;nbsp; button.on.click.add((e) =&amp;gt; cool());
&amp;nbsp; }

}

void main() {
&amp;nbsp; new Awesome().init(document.query("#button"));
}&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
First, the use of &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;this&lt;/span&gt; is optional. Typing less code is good.&lt;br /&gt;
&lt;br /&gt;
Second, because Dart doesn't change the context, or the definition of &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;this&lt;/span&gt;, the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;cool()&lt;/span&gt; method resolves to the lexical scope.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Next Steps&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Read more of my &lt;a href="http://blog.sethladd.com/search/label/dartlang"&gt;Dart posts&lt;/a&gt;, &lt;a href="http://try.dartlang.org/"&gt;try Dart in your browser&lt;/a&gt;, &lt;a href="http://api.dartlang.org/"&gt;browse the API docs&lt;/a&gt;, or &lt;a href="http://dartbug.com/"&gt;file an issue request&lt;/a&gt;. Dart is getting ready, please send us your feedback!&lt;br /&gt;
&lt;br /&gt;
Read part 9, the &lt;a href="http://blog.sethladd.com/2012/01/dart-sdk-quick-tour.html"&gt;Dart SDK Quick Tour&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-8400944799421354229?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/ZcmPhqjx8dc" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/8400944799421354229/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=8400944799421354229&amp;isPopup=true" title="5 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/8400944799421354229?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/8400944799421354229?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/ZcmPhqjx8dc/this-is-this-in-dart.html" title="This is this in Dart" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>5</thr:total><feedburner:origLink>http://blog.sethladd.com/2012/01/this-is-this-in-dart.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ck8NSHk_fip7ImA9WhRUEUQ.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-5691352940933148358</id><published>2012-01-18T00:15:00.000-08:00</published><updated>2012-01-21T16:08:19.746-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-21T16:08:19.746-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Strings in Dart</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/yOGtT2hbelHTGztXlQNyCV-1XR8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/yOGtT2hbelHTGztXlQNyCV-1XR8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/yOGtT2hbelHTGztXlQNyCV-1XR8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/yOGtT2hbelHTGztXlQNyCV-1XR8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;(This is part 7 in an ongoing series about Dart. Check out part 6, &lt;a href="http://blog.sethladd.com/2012/01/for-loops-in-dart-or-fresh-bindings-for.html"&gt;For loops in Dart&lt;/a&gt;.)&lt;br /&gt;
&lt;br /&gt;
Dart brings some new ideas to Strings. If you think you know Strings, keep reading for a few surprises.&lt;br /&gt;
&lt;br /&gt;
From the API docs: "A string is represented by a list of 32-bit Unicode scalar character codes."&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Immutable&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
String are immutable objects, which means you can create them but you can't change them. You can of course build a new string out of other strings, but once created, the string's contents are fixed.&lt;br /&gt;
&lt;br /&gt;
This is an optimization, as two strings with the same characters in the same order can be the same object.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;var s11 = "strings are immutable";
var s21 = "strings are immutable";

print(s11 == s21); // true, contain the same characters
print(s11 === s21); // true, are the same object in memory&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
If you look closely at the &lt;a href="http://api.dartlang.org/dart_core/String.html"&gt;String API docs&lt;/a&gt;, you'll notice that none of the methods on String actually change the state of a String. For example, the method replaceAll() returns a new String and doesn't exchange the original String.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;var greetingTemplate = 'Hello NAME, nice to meet you!';
var greeting = greetingTemplate.replaceFirst(new RegExp("NAME"), 'Bob');

print(greetingTemplate != greeting); // true&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Note: As of 2011-01-18, the above code doesn't work in Dart VM. See &lt;a href="http://code.google.com/p/dart/issues/detail?id=429"&gt;bug 429&lt;/a&gt; for the status. The above code should work in Dartboard, see&amp;nbsp;&lt;a href="http://try.dartlang.org/s/yhoo"&gt;http://try.dartlang.org/s/yhoo&lt;/a&gt;&amp;nbsp;for an example.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Creating&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Both single and double quotes can be used to declare a String.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;print('single quotes work easily');

print("double quotes work just as well");&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Multi-line strings are now supported! Just use a triple quote (''' or """), with either single quote or double quote. Note the initial newline is ignored, but the last newline is not ignored!&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;print( '''
the initial newline is ignored
but the last newline is not ignored
''');

// is the same as

print("""the initial newline is ignored
but the last newline is not ignored\n""");&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Escaping&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Some languages interpret single quotes as a "literal" string, but not Dart.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;print('single quotes \n don\'t make a string literal');&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;&amp;gt; single quotes 
 don't make a string literal&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
You can make a "raw" string by prefixing it with @. For example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;print(@'prefixing with a @ turns a string into a \n literal or "raw" string');&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; color: black; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&amp;gt; prefixing with a @ turns a string into a \n literal or "raw" string&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Interpolation&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart improves on other web programming languages by introducing string interpolation. Building and joining strings is much easier now:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;var s = 'string interpolation';

print('dart has ${s} which is very handy');

print("you can nest ${s + ' for fun and ${"profit"}'} with Dart");

print("concatenating strings" + " " + "is also possible with the plus sign, but we don't recommend it");&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; color: black; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&amp;gt; dart has string interpolation which is very handy
&amp;gt; you can nest string interpolation for fun and profit with Dart
&amp;gt; concatenating strings is also possible with the plus sign, but we don't recommend it
&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Building from parts&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Programmatically generating a String is best accomplished with a &lt;a href="http://api.dartlang.org/dart_core/StringBuffer.html"&gt;StringBuffer&lt;/a&gt;. A StringBuffer doesn't generate a new String object until &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;toString()&lt;/span&gt; is called.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;var sb = new StringBuffer();

sb.add("Use a StringBuffer");
sb.addAll(["for ", "efficient ", "string ", "creation "]);
sb.add("if you are ").add("building lots of strings");

var fullString = sb.toString();

print(fullString); // Use a StringBufferfor efficient string creation if you are building lots of strings

sb.clear(); // all gone!&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
You might be thinking, "Pffft, typing StringBuffer is so many characters. The performance difference can't be that big of a deal!" to which I reply, "The numbers don't lie."&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// use new hotness: StringBuffer
buildWithStringBuffer() {
&amp;nbsp; var sb = new StringBuffer();

&amp;nbsp; for (var i = 9999; i &amp;gt; 0; i--) {
&amp;nbsp; &amp;nbsp; sb.add(i.toString());
&amp;nbsp; &amp;nbsp; sb.add(" beers on the wall\n");
&amp;nbsp; }

&amp;nbsp; var finalString = sb.toString();
}

// use old school + for string concatenation
buildWithConcat() {
&amp;nbsp; var s = '';

&amp;nbsp; for (var i = 9999; i &amp;gt; 0; i--) {
&amp;nbsp; &amp;nbsp; s += i.toString();
&amp;nbsp; &amp;nbsp; s += " beers on the wall\n";
&amp;nbsp; }

&amp;nbsp; var finalString = s;
}

// wrap a function fn with the Stopwatch class to time execution
measure(fn) {
&amp;nbsp; var sw = new Stopwatch.start();
&amp;nbsp; fn();
&amp;nbsp; return sw.elapsedInMs();
}

// the following timings are from the Dart VM
// smaller is faster is better

measure(buildWithStringBuffer); // 20

measure(buildWithConcat); // 3574 !!&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
As you can see, using old school + concatenation for Strings is much slower than using StringBuffer.&lt;br /&gt;
&lt;br /&gt;
Note: the above times were measured in the Dart VM. When compiled to JavaScript, you will see different numbers. There is a &lt;a href="http://code.google.com/p/dart/issues/detail?id=1216"&gt;bug for JavaScript and StringBuffer performance&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Matching&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Thanks to the core Dart libraries, it's fairly easy to determine if a strings starts with, ends with, or contains another string.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;var fullName = 'Cuthbert Musgrave Girdlestone, III';


fullName.startsWith('Cuthbert'); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// true

fullName.endsWith('III'); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // true

fullName.contains(new RegExp('Musgrave')); &amp;nbsp;// true&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Notice how the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;contains()&lt;/span&gt; method takes a &lt;a href="http://api.dartlang.org/dart_core/RegExp.html"&gt;RegExp&lt;/a&gt; object for the pattern.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Summary&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Strings are immutable lists of 32 bit Unicode scalar character codes. They can be multi-line with triple quotes, or "raw" and unfiltered with a @ prefix.&lt;br /&gt;
&lt;br /&gt;
Strings can be interpolated, which is preferable to concatenation via the + operator. If you must programmatically build up a String, do so with StringBuffer, which is much faster.&lt;br /&gt;
&lt;br /&gt;
Strings can do a lot more than we showed here. For instance, you can&amp;nbsp;&lt;a href="http://api.dartlang.org/dart_core/String.html#split"&gt;split&lt;/a&gt;&amp;nbsp;a string, create a&amp;nbsp;&lt;a href="http://api.dartlang.org/dart_core/String.html#substring"&gt;substring&lt;/a&gt;,&amp;nbsp;&lt;a href="http://api.dartlang.org/dart_core/String.html#trim"&gt;trim&lt;/a&gt;&amp;nbsp;it, and&amp;nbsp;&lt;a href="http://api.dartlang.org/dart_core/String.html"&gt;lots more&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Next Steps&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Like what you see? &lt;a href="http://www.dartlang.org/"&gt;Learn more about Dart&lt;/a&gt;, its &lt;a href="http://api.dartlang.org/html.html"&gt;HTML libraries&lt;/a&gt;, and &lt;a href="http://www.dartlang.org/support/index.html"&gt;join the discussion&lt;/a&gt;. &lt;a href="http://code.google.com/p/dart/issues/list"&gt;File a new issue&lt;/a&gt; if you see something missing.&lt;br /&gt;
&lt;br /&gt;
Also, follow &lt;a href="http://twitter.com/dart_lang"&gt;@dart_lang&lt;/a&gt; and &lt;a href="https://plus.google.com/106846248004202631982/posts"&gt;+Dart Bits&lt;/a&gt; for more info!&lt;br /&gt;
&lt;br /&gt;
Read Part 8, &lt;a href="http://blog.sethladd.com/2012/01/this-is-this-in-dart.html"&gt;This is this in Dart&lt;/a&gt;, in which we no longer have to assign &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;this&lt;/span&gt; to &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;_this&lt;/span&gt; for closures.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-5691352940933148358?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/ke8Ipenm_20" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/5691352940933148358/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=5691352940933148358&amp;isPopup=true" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/5691352940933148358?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/5691352940933148358?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/ke8Ipenm_20/strings-in-dart.html" title="Strings in Dart" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://blog.sethladd.com/2012/01/strings-in-dart.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkEFQHc8eip7ImA9WhRVGEo.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-2470491900101396738</id><published>2012-01-11T01:46:00.000-08:00</published><updated>2012-01-18T00:16:51.972-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-18T00:16:51.972-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>For loops in Dart, or, Fresh bindings for sane closures</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/FdtefKHVp6P3MBjdnJYmjVFPNNI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/FdtefKHVp6P3MBjdnJYmjVFPNNI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/FdtefKHVp6P3MBjdnJYmjVFPNNI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/FdtefKHVp6P3MBjdnJYmjVFPNNI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;(This is Part 6 of an ongoing series about &lt;a href="http://www.dartlang.org/"&gt;Dart&lt;/a&gt;. Check out Part 5, &lt;a href="http://blog.sethladd.com/2012/01/generics-in-dart-or-why-javascript.html"&gt;Generics in Dart&lt;/a&gt;.)&lt;br /&gt;
&lt;br /&gt;
Dart has two forms of the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;for&lt;/span&gt; loop. While you may familiar with this form of iteration, the JavaScript developer will find one (surprisingly refreshing) difference. Read on!&lt;br /&gt;
&lt;br /&gt;
Dart supports the typical &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;for&lt;/span&gt; loop form:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;&lt;b&gt;for&lt;/b&gt; (var i = 0; i &amp;lt; 3; i++) {
  print(i);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
As well as the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;for-in&lt;/span&gt; form:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;var collection = [0,1,2];
&lt;b&gt;for&lt;/b&gt; (var x &lt;b&gt;in&lt;/b&gt; collection) {
  print(x);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;for-in&lt;/span&gt; form is sugar for the longer code which involves an iterator:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// this is the long form of for-in

var n0 = collection.iterator();

while (n0.hasNext()) {
   final x = n0.next();
   print(x);
}

// where n0 is an identifier that does not occur anywhere in the program.
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
This means that any object which implements &lt;a href="http://api.dartlang.org/dart_core/Iterable.html"&gt;Iterable&lt;/a&gt; can be used via &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;for-in&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
Hopefully this looks familiar so far. You might even be yawning. &lt;b&gt;&lt;i&gt;But wake up!&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
There is a subtle semantic difference between how Dart runs the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;for&lt;/span&gt; loop and how JavaScript runs the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;for&lt;/span&gt; loop.&lt;br /&gt;
&lt;br /&gt;
To start, let's look at the following JavaScript code:&lt;br /&gt;
&lt;br /&gt;
&lt;iframe allowfullscreen="allowfullscreen" frameborder="0" src="http://jsfiddle.net/sethladd/CVB6D/embedded/" style="height: 300px; width: 100%;"&gt;&lt;/iframe&gt;
&lt;br /&gt;
When this runs, what do you expect to be printed to the console? If you said &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;01&lt;/span&gt;&amp;nbsp;you'd be incorrect. This will, in fact, print &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;22&lt;/span&gt;. Go ahead, give it a shot.&lt;br /&gt;
&lt;br /&gt;
What is happening here? JavaScript is binding the same variable to each of the anonymous functions inside the first for loop. Every callback sees the same &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;i&lt;/span&gt;. This is certainly non-obvious to the beginning JavaScript developer.&lt;br /&gt;
&lt;br /&gt;
The solution is to remember that JavaScript has function level scoping (not block level scoping). Wrapping the anonymous functions inside their own anonymous, yet immediately executed, function will create a "private" copy of the variable. For example:&lt;br /&gt;
&lt;br /&gt;
&lt;iframe allowfullscreen="allowfullscreen" frameborder="0" src="http://jsfiddle.net/sethladd/xB78t/1/embedded/" style="height: 300px; width: 100%;"&gt;&lt;/iframe&gt;
&lt;br /&gt;
&lt;b&gt;We can do better! We should demand better!&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Dart makes this a whole lot easier and arguably more approachable. Consider the following Dart code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var callbacks = [];
  for (var i = 0; i &amp;lt; 2; i++) {
    callbacks.add(() =&amp;gt; print(i));
  }
  callbacks.forEach((c) =&amp;gt; c());
}
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;&amp;gt; 0, 1&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The above code is syntacticly similar to our first JavaScript example, yet behaves like the second JavaScript example. Yes, that means this prints 0 and then 1! Hurray!&lt;br /&gt;
&lt;br /&gt;
Note: the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;for-in&lt;/span&gt; style loop will also get a fresh binding for &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;i&lt;/span&gt;&lt;span style="font-family: inherit;"&gt;, just like the standard for loop.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
If you squint, the &lt;a href="http://www.dartlang.org/docs/spec/index.html"&gt;Dart language spec&lt;/a&gt; specifies this exact behavior, with the following quote:&lt;br /&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;i&gt;[it is]&amp;nbsp;intended to prevent the common error where users create a closure inside a for loop, intending to close over the current binding of the loop variable, and find (usually after a painful process of debugging and learning) that all the created closures have captured the same value - the one current in the last iteration executed.&lt;/i&gt;&lt;/blockquote&gt;
&lt;i&gt;Warning&lt;/i&gt;: As of the time of this writing (2012-01-10), only the Dart VM does the right thing for the for loop. Unfortunately, DartC and Frog (the two Dart -&amp;gt; JavaScript compilers) are broken. Track &lt;a href="http://code.google.com/p/dart/issues/detail?id=333"&gt;bug 333&lt;/a&gt; to follow this bug.&lt;br /&gt;
&lt;br /&gt;
Further good news: ECMAScript 6 should allow for simplified syntax thanks to block scoping:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;let callbacks = [];
for (let i = 0; i &amp;lt; 10; i++) {
  let j = i;
  callbacks.push(function() { print(j) });
}
callbacks.forEach(function(c) { c() });&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Summary&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart has two familiar forms of the for loop for iteration. In both forms, a fresh copy of the variable created by the loop is generated. Closures and anonymous functions inside of for loops will see individual variables, which is generally a more expected behavior.&lt;br /&gt;
&lt;br /&gt;
Special thanks to &lt;a href="https://plus.google.com/100798142896685420545/posts"&gt;+Bob Nystrom&lt;/a&gt; and his informative Dart posts on Reddit, such as &lt;a href="http://www.reddit.com/r/programming/comments/nmtmu/the_problem_with_implicit_scoping_in_coffeescript/c3afpgh"&gt;this gem&lt;/a&gt; which provided the inspiration for this post.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Next Steps&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Learn more about Dart, &lt;a href="http://try.dartlang.org/"&gt;play with Dart&lt;/a&gt; in your browser, &lt;a href="http://api.dartlang.org/"&gt;browse the API docs&lt;/a&gt;, &lt;a href="http://code.google.com/p/dart/issues/list"&gt;enter feature requests and bugs&lt;/a&gt;, and &lt;a href="https://groups.google.com/a/dartlang.org/group/misc/topics"&gt;join the discussion&lt;/a&gt;. And remember, Dart is in Technology Preview right now, things are changing and we'd like to hear your feedback!&lt;br /&gt;
&lt;br /&gt;
Also, read Part 7: &lt;a href="http://blog.sethladd.com/2012/01/strings-in-dart.html"&gt;Strings in Dart&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-2470491900101396738?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/eEOfu58_vqE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/2470491900101396738/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=2470491900101396738&amp;isPopup=true" title="7 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/2470491900101396738?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/2470491900101396738?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/eEOfu58_vqE/for-loops-in-dart-or-fresh-bindings-for.html" title="For loops in Dart, or, Fresh bindings for sane closures" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>7</thr:total><feedburner:origLink>http://blog.sethladd.com/2012/01/for-loops-in-dart-or-fresh-bindings-for.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUcFSH04eCp7ImA9WhRVE04.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-6256097392979607354</id><published>2012-01-05T18:06:00.001-08:00</published><updated>2012-01-11T17:50:19.330-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-11T17:50:19.330-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Comparing Dart, jQuery, CoffeeScript, CoffeeScript+jQuery, and JavaScript</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/bYg7BV04FRu7ONQA-R87MqG1xDg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/bYg7BV04FRu7ONQA-R87MqG1xDg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/bYg7BV04FRu7ONQA-R87MqG1xDg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/bYg7BV04FRu7ONQA-R87MqG1xDg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;UPDATE: Thanks to&amp;nbsp;&lt;a href="https://github.com/ayosec"&gt;Ayose Cazorla&lt;/a&gt; we now have CoffeeScript+jQuery examples. And thanks to js2coffee.org we have vanilla CoffeeScript.&lt;br /&gt;
&lt;br /&gt;
A &lt;a href="http://sharedfil.es/js-48hIfQE4XK.html"&gt;neat comparison between jQuery and vanilla JavaScript&lt;/a&gt; &lt;a href="https://plus.google.com/118075919496626375791/posts/SDAxHjtJwcq"&gt;hit Google+&lt;/a&gt; via &lt;a href="https://plus.google.com/118075919496626375791/posts"&gt;+Eric Bidelman&lt;/a&gt;, so I thought it would be fun to add vanilla &lt;a href="http://www.dartlang.org/"&gt;Dart&lt;/a&gt; to the mix.&lt;br /&gt;
&lt;br /&gt;
This is a fun experiment, but please don't base your decision to use one option or another based on this isolated blog post. Take a holistic approach to your evaluation.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Document is ready&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// jQuery

$(document).ready(function() {
  // code…
});
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla JavaScript

document.addEventListener("DOMContentLoaded", function() {
  // code…
});
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla Dart

window.on.contentLoaded.add((e) =&amp;gt; code);
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// CoffeeScript+jQuery

$ -&amp;gt; code
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla CoffeeScript

document.addEventListener "DOMContentLoaded", -&amp;gt;
  # code
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Find all divs&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// jQuery

var divs = $("div");&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla JavaScript

var divs = document.querySelectorAll("div");&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla Dart

var divs = document.queryAll("div");
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// CoffeeScript+jQuery

divs = $ "div"
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla CoffeeScript

divs = document.querySelectorAll("div")
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Create a new div&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// jQuery

var newDiv = $("&amp;lt;div/&amp;gt;");
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla JavaScript

var newDiv = document.createElement("div");
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla Dart

var newDiv = new Element.tag("div");
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// CoffeeScript+jQuery

divs = $ "&amp;lt;div&amp;gt;"
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla CoffeeScript

newDiv = document.createElement("div")
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Add a class&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// jQuery

newDiv.addClass("foo");
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla JavaScript

newDiv.classList.add("foo");
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla Dart

newDiv.classes.add("foo");
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// CoffeeScript+jQuery

newDiv.addClass "foo"
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla CoffeeScript

newDiv.classList.add "foo"
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Toggle a class&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// jQuery

newDiv.toggleClass("foo");
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla JavaScript

newDiv.classList.toggle("foo");
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla Dart

// doesn't work yet, see bug &lt;a href="http://code.google.com/p/dart/issues/detail?id=1063"&gt;http://code.google.com/p/dart/issues/detail?id=1063&lt;/a&gt;
// newDiv.classes.toggle("foo");
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// CoffeeScript+jQuery

newDiv.toggleClass "foo"
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla CoffeeScript

newDiv.classList.toggle "foo"
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Add a click handler for each &amp;lt;a&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// jQuery

$("a").click(function() {
  // code…
})
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla JavaScript

[].forEach.call(document.querySelectorAll("a"), function(el) {
  el.addEventListener("click", function() {
    // code…
  });
});
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla Dart

document.queryAll("a").forEach((el) {
    el.on.click.add((e) =&amp;gt; code);
});
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// CoffeeScript+jQuery

$("a").click -&amp;gt; code
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla CoffeeScript

[].forEach.call document.querySelectorAll("a"), (el) -&amp;gt;
  el.addEventListener "click", -&amp;gt;
    # code
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Append a new &amp;lt;p&amp;gt; to &amp;lt;body&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// jQuery

$("body").append($("&amp;lt;p/&amp;gt;"));
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla JavaScript

document.body.appendChild(document.createElement("p"));
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla Dart

document.body.nodes.add(new Element.tag("p"));
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// CoffeeScript+jQuery

$("body").append $("&amp;lt;p&amp;gt;")
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla CoffeeScript

document.body.appendChild document.createElement("p")
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Add attribute to an element&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// jQuery

$("img").filter(":first").attr("alt", "My image");
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla JavaScript

document.querySelector("img").setAttribute("alt", "My image");
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla Dart

document.query("img").attributes['alt'] = 'My image';
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// CoffeeScript+jQuery

$("img:first").attr "alt", "My image"
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla CoffeeScript

document.querySelector("img").setAttribute "alt", "My image"
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Get parent node&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// jQuery

var parent = $("#about").parent();
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla JavaScript

var parent = document.getElementById("about").parentNode;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla Dart

var parent = document.query("#about").parent;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// CoffeeScript+jQuery

parent = $("#about").parent()
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla CoffeeScript

parent = document.getElementById("about").parentNode
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Clone an element&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// jQuery

var clonedElement = $("#about").clone();
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla JavaScript

var clonedElement = document.getElementById("about").cloneNode(true);
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla Dart

var clonedElement = document.query("#about").clone(true);
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// CoffeeScript+jQuery

clonedElement = $("#about").clone()
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla CoffeeScript

clonedElement = document.getElementById("about").cloneNode(true)
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Clear child nodes&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// jQuery

$("#wrap").empty();
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla JavaScript

var wrap = document.getElementById("wrap");
while(wrap.firstChild) wrap.removeChild(wrap.firstChild);
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla Dart

document.query("#wrap").nodes.clear();
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// CoffeeScript+jQuery

$("#wrap").empty()
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla CoffeeScript

wrap = document.getElementById("wrap")
wrap.removeChild wrap.firstChild  while wrap.firstChild
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Check if element has child nodes&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// jQuery

if($("#wrap").is(":empty")) { ... }
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla JavaScript

if(!document.getElementById("wrap").hasChildNodes()) { ... }
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla Dart

if (document.query("#wrap").nodes.isEmpty()) { ... }
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// CoffeeScript+jQuery

if $("#wrap").is(":empty")
  code
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla CoffeeScript

code  unless document.getElementById("wrap").hasChildNodes()
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Get next sibling element&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// jQuery

var nextElement = $("#wrap").next();
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla JavaScript

var nextElement = document.getElementById("wrap").nextSibling;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla Dart

var nextElement = document.query("#wrap").nextNode;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// CoffeeScript+jQuery

nextElement = $("#wrap").next()
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;
// vanilla CoffeeScript

nextElement = document.getElementById("wrap").nextSibling
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Next Steps&lt;br /&gt;
&lt;br /&gt;
Like what you see? &lt;a href="http://www.dartlang.org/"&gt;Learn more about Dart&lt;/a&gt;, its &lt;a href="http://api.dartlang.org/html.html"&gt;HTML libraries&lt;/a&gt;, and &lt;a href="http://www.dartlang.org/support/index.html"&gt;join the discussion&lt;/a&gt;. &lt;a href="http://code.google.com/p/dart/issues/list"&gt;File a new issue&lt;/a&gt; if you see something missing.&lt;br /&gt;
&lt;br /&gt;
Also, follow &lt;a href="http://twitter.com/dart_lang"&gt;@dart_lang&lt;/a&gt; and &lt;a href="https://plus.google.com/106846248004202631982/posts"&gt;+Dart Bits&lt;/a&gt; for more info!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-6256097392979607354?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/oTuLX7A6lvk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/6256097392979607354/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=6256097392979607354&amp;isPopup=true" title="16 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/6256097392979607354?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/6256097392979607354?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/oTuLX7A6lvk/vanilla-dart-ftw.html" title="Comparing Dart, jQuery, CoffeeScript, CoffeeScript+jQuery, and JavaScript" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>16</thr:total><feedburner:origLink>http://blog.sethladd.com/2012/01/vanilla-dart-ftw.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0UER344eyp7ImA9WhRVEko.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-7914351595013799403</id><published>2012-01-03T23:22:00.000-08:00</published><updated>2012-01-11T01:46:46.033-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-11T01:46:46.033-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Generics in Dart, or, Why a JavaScript programmer should care about types</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/zBuM_FFTiOUAJZtrlYA_SP8m2yU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zBuM_FFTiOUAJZtrlYA_SP8m2yU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/zBuM_FFTiOUAJZtrlYA_SP8m2yU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zBuM_FFTiOUAJZtrlYA_SP8m2yU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;(This is Part 5 in an ongoing series on Dart. Check out Part 4, &lt;a href="http://blog.sethladd.com/2012/01/maps-and-hashes-in-dart.html"&gt;Maps and hashes in Dart&lt;/a&gt;.)&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Intro&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart is, by most accounts, designed to be a familiar programming language. One of Dart's most interesting (and daring features for a mainstream language) is optional typing. Optional static types allow &lt;b&gt;you&lt;/b&gt; to choose when to add the static types.&lt;br /&gt;
&lt;br /&gt;
Dart has optional typing because it's a web programming language, and it's designed to scale from interactive scripts (which don't necessarily need lots of declared types) to complex, large web applications (which probably do need lots of declared types, which serve as a documentation annotation for tools and humans.)&lt;br /&gt;
&lt;br /&gt;
An example of untyped Dart code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;smush(a, b) =&amp;gt; a + b;

main() {
  var msg = "hello";
  var rcpt = "world";
  print( smush(msg, rcpt) );
}
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;&amp;gt; helloworld&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
As you can see in the example above, the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;var&lt;/span&gt; keyword is used to denote a variable. &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;var&lt;/span&gt; is synonymous with the Dynamic, or "unknown", type. The above code is is not statically typed, yet it runs just fine.&lt;br /&gt;
&lt;br /&gt;
Note the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;smush(a, b)&lt;/span&gt; method, which smushes two objects together and returns the result. Looking at this method signature, and even its definition, it's not clear exactly what that intent is or what kind of objects it works with. The original programmer's intent isn't clear, which is a problem if the development team is large or if the application itself is complex.&lt;br /&gt;
&lt;br /&gt;
Nothing is stopping a developer from passing in two numbers to smush, as the example below illustrates:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;smush(a, b) =&amp;gt; a + b;

main() {
  var x = 3;
  var y = 4;
  print( smush(x, y) );
}
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; color: black; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&amp;gt; 7 // technically true, but is this what we wanted to allow?&lt;/pre&gt;
&lt;br /&gt;
In some sense, this is the beauty of dynamic scripting languages. However, Dart is designed to support large, complex apps as well, and when you add hundreds of classes, thousands of functions, and tens of developers, this kind of ambiguity can be costly.&lt;br /&gt;
&lt;br /&gt;
The typical JavaScript developer would probably first try to solve this problem by just remembering what the intent of smush is, but this doesn't scale. Second, the function would be renamed to something more clear. Third, comments would probably be added, with a note like "this methods takes two strings and concatenates them." I reckon the combination of a renamed function name and descriptive comments would get you pretty far, but &lt;i&gt;they still don't express your intent to the program&lt;/i&gt;.&lt;br /&gt;
&lt;br /&gt;
But we can do better than longer function names and more comments, for we want to say less and do more! We want our code to be our comments, and have our systems work for us and understand us!&lt;br /&gt;
&lt;br /&gt;
We must demand a better solution. We must demand that our tools scale to meet our complex app needs. We must demand that our language allow us to express our intent tersely and with minimal friction!&lt;br /&gt;
&lt;br /&gt;
Enter types!&lt;br /&gt;
&lt;br /&gt;
Luckily, Dart gives us a safety net as we scale up our program: optional static types. By adding in simple annotations to our program code, we can express certain expectations and assertions in a way that both a human and the machine can quickly and easily grok. Once our system understands these type assertions, a whole new world of early error detection is now available. Every error caught in development is one less error caught by our users.&lt;br /&gt;
&lt;br /&gt;
Let's declare that the smush method is expecting Strings:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;smush(&lt;b&gt;String&lt;/b&gt; a, &lt;b&gt;String&lt;/b&gt; b) =&amp;gt; a + b;

main() {
  var x = 3;
  var y = 4;
  print( &lt;span style="color: red;"&gt;smush(x, y) ); // in Checked Mode, this results in a Failed type check&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
When two numbers are passed to smush, and the program is run in Checked Mode (aka "type assertions turned on"), the system will halt execution with a "Failed type check" error.&lt;br /&gt;
&lt;br /&gt;
This is &lt;i&gt;awesome&lt;/i&gt;! Our tools caught an error for us, simply because we added some short annotations. Our tools are now working for us.&lt;br /&gt;
&lt;br /&gt;
By simply adding the type declarations to the smush signature, the program understands the developer's intent and expectations and can check them for us. No comment has that power, and even a descriptive and clear function name doesn't have that power.&lt;br /&gt;
&lt;br /&gt;
Think of those type annotations as assertions and documentation, all rolled up in one.&lt;br /&gt;
&lt;br /&gt;
We can go further and give types to our variables x and y. This has the added benefit of generating warnings with or without Checked Mode. For example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;smush(String a, String b) {
  return a + b;
}

main() {
  &lt;b style="color: black;"&gt;num&lt;/b&gt; x = 3;  // note the type
  &lt;b style="color: black;"&gt;num&lt;/b&gt; y = 4;
  print( smush(x, y) ); // &lt;span style="color: red;"&gt;two warnings are generated here, re: type mismatch!&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;&amp;gt; 7 // yet the program still runs! types don't affect the program semantics&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Giving type annotations to x and y, and keeping the type annotations in the smush method, allows our program to detect and warn us about this type mismatch.&lt;br /&gt;
&lt;br /&gt;
This is &lt;i&gt;super&lt;/i&gt; awesome! We don't even need to run in Checked Mode for our program to let us know something is afoot. All we had to do was add an annotation that was probably shorter than any comment you would have added to clarify just what x and y were supposed to be.&lt;br /&gt;
&lt;br /&gt;
Optional types give us the powerful, yet terse, expressiveness when we need them for larger, more complex web apps, and we can omit them for our simple one page scripts. Or heck, type everything and catch nearly all type warnings early!&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Yo dawg, I heard you like types, so I added types to your collections, so you can put Strings in your Lists of Strings&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
We've shown the helpfulness and expressiveness of types for variable declarations and function signatures. Let's take it one step farther and add another type annotation which can further tell your program, and your fellow developers, what your intentions are (and catch errors early!)&lt;br /&gt;
&lt;br /&gt;
As covered in previous posts, Dart has support for collections such as &lt;a href="http://blog.sethladd.com/2011/12/lists-and-arrays-in-dart.html"&gt;Lists&lt;/a&gt; (arrays) and &lt;a href="http://blog.sethladd.com/2012/01/maps-and-hashes-in-dart.html"&gt;Maps&lt;/a&gt; (hashes). Lists store ordered collections of objects that can be retrieved via an index, and Maps store key/value pairs.&lt;br /&gt;
&lt;br /&gt;
A simple example of a List:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var digits = [0,1,2,3];
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Adding a new element to a List is also simple:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var digits = [0,1,2,3];
  digits.&lt;b&gt;add&lt;/b&gt;(4);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
However, even though I'm clearly expressing a List of numbers, the following code will still execute:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var digits = [0,1,2,3];
  digits.add("four"); &lt;span style="color: red;"&gt;// executes, but is this what we want?&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
A couple of options spring to mind. We could rename digits to digitsOfNumbers, but that's verbose. Worst of all, not matter what we rename the variable to, we still can't express our intent to the program.&lt;br /&gt;
&lt;br /&gt;
We must demand better! We must be able to tell our program that collections of things only hold certain things. We must demand that our program will warn us if an object of a different type is being added to a collection. We must demand that our programs catch errors early and help us, not hinder us!&lt;br /&gt;
&lt;br /&gt;
Enter generics!&lt;br /&gt;
&lt;br /&gt;
Think of generics as type annotations for your collections. With generics, you can tell your program, and other developers sharing the code, that a List of numbers is, well, actually a List of numbers.&lt;br /&gt;
&lt;br /&gt;
Check it out:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var digits = &lt;b style="color: black;"&gt;&amp;lt;num&amp;gt;&lt;/b&gt;[0,1,2,3];
  &lt;span style="color: red;"&gt;digits.add("four"); // BOOM, Failed type check. Program stops.&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Thanks to the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;lt;num&amp;gt;&lt;/span&gt; annotation, the program can catch when an object with a mismatched type is added. Best of all, the error is caught &lt;i&gt;early&lt;/i&gt;, and not when some other method tries to actually do something (incorrect) with the different object.&lt;br /&gt;
&lt;br /&gt;
Dart's List interface is really a List&amp;lt;E&amp;gt; which you can read as "List of E, where E is some type." That E is a placeholder, and stand-in for a type that is declared external to the List interface definition.&lt;br /&gt;
&lt;br /&gt;
These generics also help when extracting objects from a List, not just adding objects. For example, consider the following code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;add(&lt;b style="color: black;"&gt;num&lt;/b&gt; a, &lt;b style="color: black;"&gt;num&lt;/b&gt; b) =&amp;gt; a + b;

main() {
  var names = &amp;lt;&lt;b style="color: black;"&gt;String&lt;/b&gt;&amp;gt;['alice', 'bob'];
  print( &lt;span style="color: red;"&gt;add(names[0], names[1]) ); // in Checked Mode, this BOOMs. Failed type check!&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Wow, Dart is smart! It knows that &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;add&lt;/span&gt; takes two numbers, and that &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;names&lt;/span&gt; is a List of Strings. It also knows that objects inside of &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;names&lt;/span&gt;&amp;nbsp;are Strings, so when we reference &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;names[0]&lt;/span&gt; (which the program knows is a String) and attempt to pass it to &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;add(num, num)&lt;/span&gt;, it errors with a Failed type check.&lt;br /&gt;
&lt;br /&gt;
This is &lt;i&gt;super duper cool&lt;/i&gt;! Adding a simple type annotation to our collection gives our program a tremendous amount of information, and the ability to fail quickly at the source of the bug (and not later in the program when it's too late.) &amp;nbsp;Simply being able to catch these sorts of bugs when they occur is enough to make you consider adding type annotations to your collections.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;A List of Strings is a List, which sounds obvious when you say it out loud&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Warning: the following section is for those who want to dive in deeper to generics, reification, and covariance. If you are new to these terms, the below might blow your mind. It's OK to stop reading here. The below isn't always the best way to write code.&lt;br /&gt;
&lt;br /&gt;
One more cool part about Dart's collections and generics (aka type annotations.) &amp;nbsp;Dart will reify, or remember, the generic types for its collections. Unlike in Java, Dart's generic type annotation is &lt;b&gt;not&lt;/b&gt;&amp;nbsp;thrown away.&lt;br /&gt;
&lt;br /&gt;
This means you can perform a check at runtime:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var names = &amp;lt;String&amp;gt;['alice', 'bob'];
  print( names is List&amp;lt;String&amp;gt; ); // true!
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
OK, in retrospect, this is a totally obvious example. Trust me when I say, if you're a Java developer, your head is spinning right now.&lt;br /&gt;
&lt;br /&gt;
Remember how we keep repeating that Dart is an optionally typed language? Someone (I don't know who) is going to write a function that does &lt;b&gt;not&lt;/b&gt;&amp;nbsp;specify any generic types, but the program should work anyway.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// we really recommend you don't omit the generic types
// for illustration only

awesome(List stuff) {
  // something awesome, if only I knew what was inside of stuff
  // but I was too lazy to add my generic type annotation
}

main() {
  var names = &amp;lt;String&amp;gt;['alice', 'bob'];
  awesome(names); &lt;b&gt;// works! Dart lets a List&amp;lt;String&amp;gt; act as a List&lt;/b&gt;

  &lt;b&gt;// which means&lt;/b&gt;
  print( new List&amp;lt;String&amp;gt;() is List );  // true!
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
When you say it out loud, it sounds obvious: a List of String is a List.&lt;br /&gt;
&lt;br /&gt;
Here's the best part, and a true reflection of Dart's optional typing. If List&amp;lt;String&amp;gt; is a List, is the inverse true? &amp;nbsp;Can a List be passed to a function expecting a List&amp;lt;String&amp;gt; ?&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// hurray! you added generic type annotations. your intention is clear.
awesome(List&amp;lt;String&amp;gt; stuff) {
  // something more awesome, because I know it's a List of Strings
}

main() {
  var names = ['alice', 'bob']; // I got lazy and didn't specify!
  awesome(names); // works!

  // which means
  print( new List() is List&amp;lt;String&amp;gt; );
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Yes, the above works (though we don't recommend it!) because Dart's generics are covariant. This means &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;List&amp;lt;String&amp;gt; is List&lt;/span&gt;, and &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;List is List&amp;lt;String&amp;gt;&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
But why oh why would Dart design a system that is so, well, unsound?!?!!&lt;br /&gt;
&lt;br /&gt;
Remember that Dart has optional typing. This means a library author can write classes and functions without specifying any types at all. Heck, there's an army of JavaScript developers that have never seen a static type and many probably never want to see one. Hopefully the first part of this article convinced you that types are terse and useful, but Dart is built for a wide range of developers.&lt;br /&gt;
&lt;br /&gt;
Now imagine that there is an untyped library that is AWESOME, and you want to use it. Your code, because you are a true Dart Viking, is fully typed. &amp;nbsp;Should you be able to mix your fully typed code with an arbitrary library that is untyped? &amp;nbsp;Yes!&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// from the SuperLazyLib with zero types

awesome(stuff) {
  // you are brave
}

// from the Dart Viking, who writes clean and typed code

main() {
  var names = &amp;lt;String&amp;gt;['alice', 'bob'];
  awesome(names); // works!
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Dart allows a mix of typed and untyped code, and as such the generics must be covariant.&lt;br /&gt;
&lt;br /&gt;
If you are authoring a library, please add types. Your machines and humans will both thank you.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Summary&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Demand more from your web programming language! Demand terse type annotations which help your program catch errors earlier, and your fellow developers more easily understand your intent.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Dart is a mostly familiar language by design, however it does have novel features like optional types. &amp;nbsp;Types should be thought of as annotations, or documentation, for both the machine and your fellow programmer. &amp;nbsp;Types are terse, often smaller than the comments you'd use to specify what a thing is or what can be passed to a method.&lt;br /&gt;
&lt;br /&gt;
Once added, types can be interpreted by the program, allowing for bugs resulting from type mismatches to be detected early. Types also act to convey your intention and assertions to your fellow developer. This is A Good Thing (tm).&lt;br /&gt;
&lt;br /&gt;
Types can be added to variables and function signatures. Types can also be given to collections such as Lists and Maps. These generics tell the program what objects are expected to be placed inside of, and retrieved from, a collection. Generics also help with tersely expressing programmer intent to both the machine and the fellow developer, and helps to catch bugs early.&lt;br /&gt;
&lt;br /&gt;
Generics in Dart are covariant, which means a List of Strings is a List, and a List is a List of Strings. This is due to the fact that Dart's optional typing allows for a mix of types and untyped code to be used in the same program.&lt;br /&gt;
&lt;br /&gt;
We recommend using types for method signatures, we don't recommend authoring libraries without types.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Next Steps&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Learn more about Dart, &lt;a href="http://try.dartlang.org/"&gt;play with Dart in your browser&lt;/a&gt;, &lt;a href="http://api.dartlang.org/"&gt;browse the API docs&lt;/a&gt;, &lt;a href="http://code.google.com/p/dart/issues/list"&gt;enter feature requests and bugs&lt;/a&gt;, and &lt;a href="http://www.dartlang.org/support/index.html"&gt;join the discussion&lt;/a&gt;. And remember, Dart is in Technology Preview right now, things are changing and we'd like to hear your feedback!&lt;br /&gt;
&lt;br /&gt;
Also, check out Part 6, &lt;a href="http://blog.sethladd.com/2012/01/for-loops-in-dart-or-fresh-bindings-for.html"&gt;For loops in Dart, or, Fresh bindings for sane closures&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-7914351595013799403?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/ga3V4yOQncQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/7914351595013799403/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=7914351595013799403&amp;isPopup=true" title="7 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/7914351595013799403?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/7914351595013799403?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/ga3V4yOQncQ/generics-in-dart-or-why-javascript.html" title="Generics in Dart, or, Why a JavaScript programmer should care about types" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>7</thr:total><feedburner:origLink>http://blog.sethladd.com/2012/01/generics-in-dart-or-why-javascript.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkEFQX8yfip7ImA9WhRbFUg.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-2495547938557979155</id><published>2012-01-02T21:26:00.000-08:00</published><updated>2012-02-06T12:03:30.196-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-02-06T12:03:30.196-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Maps and hashes in Dart</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/OaOPI9Z8-jR5KfwOpiAewy9cUvk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/OaOPI9Z8-jR5KfwOpiAewy9cUvk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/OaOPI9Z8-jR5KfwOpiAewy9cUvk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/OaOPI9Z8-jR5KfwOpiAewy9cUvk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;(This is Part 4 of our ongoing series about Dart. Check out Part 3: &lt;a href="http://blog.sethladd.com/2011/12/lists-and-arrays-in-dart.html"&gt;Lists and arrays in Dart&lt;/a&gt;.)&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: red;"&gt;Warning&lt;/span&gt;: We expect the Dart libraries to undergo potentially sweeping changes before Dart goes to alpha. This document is relevant as of 2012-01-02.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Intro&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart is not just a language, but a full "batteries included" effort to create a productive, familiar, and fun environment for the modern web app developer. The bundled libraries are one such "battery", including many common classes and utilities such as date manipulation, regular expressions, even asynchronous constructs like Future and Promise.&lt;br /&gt;
&lt;br /&gt;
Probably the most used set of libraries will be the Collections, such as List, Map, and Set. In this post, we'll take a look at &lt;a href="http://api.dartlang.org/dart_core/Map.html"&gt;Map&lt;/a&gt;, which is a mapping of keys to values.&lt;br /&gt;
&lt;br /&gt;
In Dart, a Map is an interface designed to manipulate a collection of keys which point to values. Maps can have null values, and can have parameterized types. Note, Maps do not subclass the Collection interface.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Dart != JavaScript&lt;/b&gt;: Unlike JavaScript, Dart objects themselves aren't maps or hashes.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Create, read, count, copy, and remove examples&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart supports map literals, such as:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var stuff = &lt;b&gt;{"hello": "world"}&lt;/b&gt;;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
In the above example, &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;hello&lt;/span&gt; is the key and &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;world&lt;/span&gt; is the value.&lt;br /&gt;
&lt;br /&gt;
Map literals, those declared with the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;{}&lt;/span&gt; syntax, must have Strings as keys.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;var map = {};

// is the same as

var map = new Map&amp;lt;String, Dynamic&amp;gt;();
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
However, if you declare your Map without the literal syntax (eg&amp;nbsp;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;new Map()&lt;/span&gt;), you can use any object that implements the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;hashCode&lt;/span&gt; method as the key. Curious what makes a good hashCode? Josh Bloch covers hashCode in his book Effective Java, and luckily that particular topic is covered in the free &lt;a href="http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf"&gt;Chapter 3&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Of course, the values don't have to be strings:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var stuff = {"hello": &lt;b&gt;4&lt;/b&gt;};
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Dart Maps can contain null values, too:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var stuff = {"hello": &lt;b&gt;null&lt;/b&gt;};
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Retrieving a value from a map is familiar:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var stuff = {"hello": "world"};
  print( &lt;b&gt;stuff['hello']&lt;/b&gt; ); // world
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
If you look for a key that isn't in a Map, you will get a &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;null&lt;/span&gt; in return:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var stuff = {"hello": "world"};
  print( &lt;b&gt;stuff['&lt;span style="color: red;"&gt;foo&lt;/span&gt;']&lt;/b&gt; ); // &lt;span style="color: red;"&gt;null&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Adding a new key/value pair to an existing map is also familiar:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var stuff = {"hello": "world"};
  &lt;b&gt;stuff['Dart'] = 'Is Cool';&lt;/b&gt;
  print( stuff['Dart'] ); // Is Cool
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Use the length getter to return the number of key/value pairs in the Map:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var stuff = {"hello": "world"};
  stuff['Dart'] = 'Is Cool';
  print( &lt;b&gt;stuff.length&lt;/b&gt; ); // 2
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Removing a value from a map, which also removes its key, is straight forward:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var stuff = {"hello": "world"};
  stuff['Dart'] = 'Is Cool';
  print( stuff['Dart'] ); // Is Cool
  &lt;b&gt;stuff.remove('hello');&lt;/b&gt;
  print( stuff.length ); // 1
  print( stuff['hello'] ); // null
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
You can copy a Map with a named constructor:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var stuff = &lt;b&gt;new Map.from({"hello": "world"});&lt;/b&gt;
  print( stuff['hello'] ); // world
}&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Iterating&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
There are a few ways to iterate through the contents of a Map. If you want to access both the key and value at the same time, the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;forEach&lt;/span&gt; method will work nicely.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var stuff = {"hello": "world", 'Dart': 'Is Cool'};
  &lt;b&gt;stuff.forEach((k,v) =&amp;gt; print(k));&lt;/b&gt;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Notice how, in the above example, that both the key and the value as passed to the callback function for every iteration of &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;forEach&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
No order is implied, do not depend on a certain order of keys and values being returned via &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;forEach&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
If you are only interested in just the keys, or just the values, use &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;getKeys()&lt;/span&gt; and &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;getValues()&lt;/span&gt;, respectively. Both methods return a Collection object.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var stuff = {"hello": "world", 'Dart': 'Is Cool'};
  var keys = stuff.getKeys();
  print( keys.length ); // 2
  keys.forEach((k) =&amp;gt; print(k)); // hello, Dart
}&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;putIfAbsent&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
A fun method on Map is &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;putIfAbsent(K key, V ifAbsent())&lt;/span&gt;. It will first if the key exists, and if not, will run the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;ifAbsent&lt;/span&gt; function and will use the return value as the value for the key. For example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var stuff = {"hello": "world", 'Dart': 'Is Cool'};
&lt;b&gt;  stuff.putIfAbsent('foo', () {
    // calculate something awesome
    return 'bar';
  });&lt;/b&gt;
  print( stuff['foo'] ); // bar
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Parameterized types, aka Generics&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
I haven't written a post on generics yet, so please bear with me if you don't know what generics are. If you do, you'll be pleasantly surprised at how they've been simplified. If you don't know what generics are, think of them as annotations to help you and your tools understand more about the types you'll use with objects such as collections.&lt;br /&gt;
&lt;br /&gt;
Maps can be specified with parameterized types, which allow you to say what kind of types you want to store inside the map. For example, perhaps you want a Map of Strings to integers. Without generics, you'd have to just hope everyone (including you) puts in String/int pairs, and you'd probably have to add a bunch of assertions to ensure that they (or you) did. However, with generics, the tools and runtime can help ensure that the code maps Strings to integers.&lt;br /&gt;
&lt;br /&gt;
(The examples in the post so far have not specified any parameterized types.)&lt;br /&gt;
&lt;br /&gt;
In Dart, you can be more specific about your Map, and actually say "A Map that maps Strings to integers" in this way:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var stuff = &lt;b&gt;&amp;lt;String, int&amp;gt;&lt;/b&gt;{'four': 4, 'five': 5};
  print( stuff['four'] ); // &lt;b&gt;4&lt;/b&gt;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The above is the same as:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var stuff = &lt;b&gt;new Map&amp;lt;String, int&amp;gt;()&lt;/b&gt;;
  stuff['four'] = 4;
  stuff['five'] = 5;
  print( stuff['four'] ); // 4
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Our tools come to the rescue when someone (definitely not you, because you're a Dart&amp;nbsp;viking)&amp;nbsp;accidentally&amp;nbsp;assigns the wrong type. For example, if you run the below in Checked Mode:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;&lt;span style="font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace;"&gt;&lt;span style="font-size: 12px; line-height: 14px;"&gt;// try it yourself at &lt;a href="http://try.dartlang.org/s/InMl"&gt;http://try.dartlang.org/s/InMl&lt;/a&gt;
main() {
  var stuff = new Map&amp;lt;String, int&amp;gt;();
  &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px;"&gt;stuff['four'] = "oh uh!"; // &lt;/span&gt;&lt;span style="color: red;"&gt;&lt;span style="font-size: 12px; line-height: 14px;"&gt;Failed type check: type String is not assignable to type int&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace;"&gt;&lt;span style="font-size: 12px; line-height: 14px;"&gt;
  print( stuff['four'] ); // won't get this far
}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Now that's what I'm talking about! For the small cost of specifying the expected types at object instantiation time, the system (in checked mode) will warn you the wrong type is being used, and the &lt;i&gt;program will stop running&lt;/i&gt;. (again, if you're in checked mode, this is true) &lt;a href="http://try.dartlang.org/s/InMl"&gt;Check out the above example&lt;/a&gt; and play with the "checked mode" toggle.&lt;br /&gt;
&lt;br /&gt;
You can go further and even specify the parameterized types statically, such as:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; color: black; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;&lt;span style="background-color: #eeeeee;"&gt;main() {
  &lt;/span&gt;&lt;b style="background-color: #eeeeee;"&gt;Map&amp;lt;String, int&amp;gt; stuff&lt;/b&gt;&lt;span style="background-color: #eeeeee;"&gt; = new Map&amp;lt;String, int&amp;gt;();
  stuff['four'] = "oh uh!"; &lt;/span&gt;&lt;span style="background-color: yellow;"&gt;// warning here if NOT in checked mode&lt;/span&gt;&lt;span style="background-color: #eeeeee;"&gt;
  print( stuff['four'] ); // won't get this far
}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Now the system has even more information, so you will get further warnings &lt;b&gt;even if you're not in checked mode&lt;/b&gt;! The program will still run if you have checked mode turned off, since the above program still works. However, by specifying the static type for stuff, you've added enough annotations to give you warnings that something isn't quite right. &lt;a href="http://try.dartlang.org/s/d2Ml"&gt;Try the above code yourself&lt;/a&gt;, with and without "checked mode" enabled.&lt;br /&gt;
&lt;br /&gt;
As mentioned above, if you are using the non-literal syntax to declare a Map (such as &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;new Map()&lt;/span&gt;) then you can use any object that implements &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;hashCode&lt;/span&gt; as the key.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Summary&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart's bundled libraries includes a Map interface, which can map a key (as long as it implements &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;hashCode&lt;/span&gt;) to a value for easy lookup. The interface has the standard methods for creating, reading, updating, and deleting key/value pairs, as well as some new methods like &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;putIfAbsent()&lt;/span&gt;. Just like in JavaScript, Dart Maps can be created with a literal, terse syntax (however, this restricts you to using only Strings as keys.) Unlike JavaScript, though, Dart objects aren't themselves Maps.&lt;br /&gt;
&lt;br /&gt;
Just as Dart has optional types, parameterized types are optional as well. They are quite useful when you're writing method signatures and interfaces, as they convey even more intention and documentation about the expected containers and objects. The more type annotations you can add, the more rich documentation can be extracted and the more the tools can warn you when things don't go as planned.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Next Steps&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Read Part 6 on &lt;a href="http://blog.sethladd.com/2012/01/generics-in-dart-or-why-javascript.html"&gt;Generics in Dart, or Why JavaScript developers should care about types&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
The core Dart libraries will almost certainly undergo sweeping changes now that Josh Bloch has joined the team. For now, though, there's still a lot you can do with Dart's lists. As you work with the libraries, remember that Dart is in Technology Preview mode, and we really want to hear your feedback.&lt;br /&gt;
&lt;br /&gt;
What do you need from the libraries? Let us know at the&amp;nbsp;&lt;a href="https://groups.google.com/a/dartlang.org/group/misc/topics"&gt;mailing list&lt;/a&gt;&amp;nbsp;or please file an&amp;nbsp;&lt;a href="http://code.google.com/p/dart/issues/list"&gt;issue&lt;/a&gt;. Thanks!&lt;br /&gt;
&lt;br /&gt;
Read more about the &lt;a href="http://api.dartlang.org/dart_core/Map.html"&gt;Map interface at the docs&lt;/a&gt;, and check out all the libraries at &lt;a href="http://api.dartlang.org/"&gt;api.dartlang.org&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-2495547938557979155?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/uzRynncSJqo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/2495547938557979155/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=2495547938557979155&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/2495547938557979155?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/2495547938557979155?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/uzRynncSJqo/maps-and-hashes-in-dart.html" title="Maps and hashes in Dart" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.sethladd.com/2012/01/maps-and-hashes-in-dart.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEECSHw8eCp7ImA9WhRWF0w.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-2958161538376133565</id><published>2011-12-30T22:37:00.000-08:00</published><updated>2012-01-04T14:37:49.270-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-04T14:37:49.270-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>10 lessons from porting JavaScript to Dart</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/deFdzt3FsBhpHQp-wN315vjl9lk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/deFdzt3FsBhpHQp-wN315vjl9lk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/deFdzt3FsBhpHQp-wN315vjl9lk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/deFdzt3FsBhpHQp-wN315vjl9lk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;I wrote a simple HTML5 game for my &lt;a href="http://blog.sethladd.com/2011/05/source-code-slides-and-video-for-html5.html"&gt;Google IO 2011 Introduction to HTML5 Game Development&lt;/a&gt; talk. The original game was written in JavaScript, and as an exercise I just ported the game to Dart. This post contains some lessons learned from the process.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Background&lt;/i&gt;: The &lt;a href="https://github.com/sethladd/Bad-Aliens"&gt;original JavaScript code&lt;/a&gt; for the game is open source, as is the new &lt;a href="https://github.com/sethladd/bad-aliens-dart"&gt;Dart code&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Note&lt;/i&gt;: The Dart version doesn't have sound working, as the Web Audio API isn't currently working in Dart.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Straight forward port&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The executive summary of the porting process is: it's pretty dang easy. Dart had two primary design constraints: it must be familiar to JavaScript developers, and it must compile to JavaScript. Having just ported over 574 lines of JavaScript to 554 lines of Dart, I can say that those two design constraints are being satisfied quite well.&lt;br /&gt;
&lt;br /&gt;
To be fair, I wrote the first JavaScript version with structured classes in mind (using JavaScript's prototype system) so it was easy to port to Dart's native classes. &amp;nbsp;For example, compare the following JavaScript class:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// old JavaScript code
function Timer() {
    this.gameTime = 0;
    this.maxStep = 0.05;
    this.wallLastTimestamp = 0;
}

Timer.prototype.tick = function() {
    var wallCurrent = Date.now();
    var wallDelta = (wallCurrent - this.wallLastTimestamp) / 1000;
    this.wallLastTimestamp = wallCurrent;
    
    var gameDelta = Math.min(wallDelta, this.maxStep);
    this.gameTime += gameDelta;
    return gameDelta;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
to the new equivalent Dart class:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// new Dart code
class Timer {

  num gameTime = 0;
  static final num MAX_STEP = 0.05;
  num wallLastTimestamp = 0;
  
  num tick() {
    num wallCurrent = new Date.now().value;
    num wallDelta = (wallCurrent - wallLastTimestamp) / 1000;
    wallLastTimestamp = wallCurrent;
    
    num gameDelta = Math.min(wallDelta, MAX_STEP);
    gameTime += gameDelta;
    return gameDelta;
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
I think the Dart class syntax is easier to read, and it's a welcome relief to be able to omit all of those &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;this&lt;/span&gt; qualifiers that are required in JavaScript. Regardless, I think it's safe to say that a developer versed in one language can grok the other.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;So what did I learn?&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
1) &lt;b&gt;Types rule.&lt;/b&gt; I know there are a bunch of JavaScript developers that have never seen a static type, and don't want to touch them. To that I say, don't be too quick to judge. Types made the porting process much quicker, because my editor and compiler caught errors much earlier for me. Did I use types for everything? Nope, but I will be using types in Dart for most things.&lt;br /&gt;
&lt;br /&gt;
2) &lt;b&gt;The Dart editor is very handy.&lt;/b&gt; With it's ability to warn me of real and potential errors as I code, I'm able to code faster. I think web developers are going to have an epiphany when they realize what they've been missing: real-time analysis of their code as they write it is a real time saver and will result in better code.&lt;br /&gt;
&lt;br /&gt;
3) &lt;b&gt;Frog compiler generates very logical JavaScript.&lt;/b&gt; Dart code will run on modern browsers via JavaScript, and Frog is the Dart to JavaScript compiler that is currently recommended due to its terse and human readable output. The Frog compiler turned my Dart code back into what was essentially the same JavaScript code that I originally wrote. &amp;nbsp;For example, check out the following JavaScript code that came from Frog:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// this JavaScript code came from Frog
function Timer() {
  this.gameTime = 0
  this.wallLastTimestamp = 0
  // Initializers done
}
Timer.prototype.tick = function() {
  var wallCurrent = new DateImplementation.now$ctor().value;
  var wallDelta = (wallCurrent - this.wallLastTimestamp) / 1000;
  this.wallLastTimestamp = wallCurrent;
  var gameDelta = Math.min(wallDelta, 0.05/*Timer.MAX_STEP*/);
  this.gameTime += gameDelta;
  return gameDelta;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
4) &lt;b&gt;Dart doesn't let me get sloppy.&lt;/b&gt; Thanks to types, real classes, inheritance, modifiers like static and final, and real lexical scoping, my Dart code feels more robust. I'm not allowed to slack.&lt;br /&gt;
&lt;br /&gt;
5) &lt;b&gt;The &lt;a href="http://api.dartlang.org/"&gt;api.dartlang.org&lt;/a&gt; docs are helpful.&lt;/b&gt; Thanks to the new API docs, and the Dart Editor, it was easy to find my way around the core libraries.&lt;br /&gt;
&lt;br /&gt;
6) &lt;b&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;Math.abs()&lt;/span&gt; is now &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;num.abs()&lt;/span&gt;.&lt;/b&gt; This makes more sense to me, I'm glad they did it. For example, instead of &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;Math.abs(-4)&lt;/span&gt; you will write &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;-4.abs()&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
7) &lt;b&gt;Grab the number of milliseconds since the epoch with &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;new Date.now().value&lt;/span&gt;.&lt;/b&gt; Slightly more verbose than JavaScript, but this might change as the whole core libraries are going to get some major love in the beginning of 2012.&lt;br /&gt;
&lt;br /&gt;
8) &lt;b&gt;Dart has one falsy value: &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;false&lt;/span&gt;.&lt;/b&gt; This is such a blessing compared to JavaScript's six falsy values. This means I had to rewrite:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// JavaScript
if (something) {
  // do stuff
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
to:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// Dart
if (something != null) {
  // do stuff
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
9) &lt;b&gt;Real lexical scoping means I can drop &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;this&lt;/span&gt; from instance variables.&lt;/b&gt; I no longer needed to write &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;this.foo&lt;/span&gt; in my objects, I could just reference &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;foo&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
10) &lt;b&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;console.log()&lt;/span&gt; is now &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;print()&lt;/span&gt;.&lt;/b&gt;&amp;nbsp;The new &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;print()&lt;/span&gt; is more terse, and works both on the server via the VM and in the browser. I'll allow it!&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Summary&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
If your JavaScript is half way logical, you'll probably find it very straight forward to convert to Dart. There are differences, but most of the work can be handled by some regular expressions and some search and replaces. The resulting code felt more stable, as types and the tools like the Dart Editor didn't allow me to slack.&lt;br /&gt;
&lt;br /&gt;
Check out the resulting &lt;a href="https://github.com/sethladd/bad-aliens-dart"&gt;Dart code for a simple HTML5 game&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-2958161538376133565?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/Nqk6XR9LSOI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/2958161538376133565/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=2958161538376133565&amp;isPopup=true" title="10 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/2958161538376133565?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/2958161538376133565?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/Nqk6XR9LSOI/10-lessons-from-porting-javascript-to.html" title="10 lessons from porting JavaScript to Dart" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>10</thr:total><feedburner:origLink>http://blog.sethladd.com/2011/12/10-lessons-from-porting-javascript-to.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C08CQ3w4eyp7ImA9WhRWFks.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-6676632484046791729</id><published>2011-12-22T15:58:00.000-08:00</published><updated>2012-01-03T23:24:22.233-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-03T23:24:22.233-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Lists and arrays in Dart</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/KqPq1Ia7eTJCnhAYcTR8aVx-hg4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KqPq1Ia7eTJCnhAYcTR8aVx-hg4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/KqPq1Ia7eTJCnhAYcTR8aVx-hg4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KqPq1Ia7eTJCnhAYcTR8aVx-hg4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;(This is Part 3 in a series about Dart. Check out Part 2, &lt;a href="http://blog.sethladd.com/2011/12/learning-functions-for-dart.html"&gt;Function in Dart&lt;/a&gt;.)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="color: red;"&gt;Warning&lt;/span&gt;&lt;/b&gt;: We expect the Dart libraries to undergo potentially sweeping changes before Dart goes to alpha. This document is relevant as of 2011-12-22.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Intro&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart is a "batteries included" effort to help app developers build modern web apps. An important "battery" is the bundled core Dart libraries, providing common and rich functionality. Dart is building a solution for large, complex web apps, and providing well tested, integrated, and common libraries is key to helping a web app developer be more productive out of the box.&lt;br /&gt;
&lt;br /&gt;
The Collection libraries are a crucial set of APIs that Dart developers get for free. Much more than simple arrays and maps, the Collection library includes standard ways to filter, iterate, inspect, compose, and sort your data. This post specifically looks at &lt;a href="http://api.dartlang.org/dart_core/List.html"&gt;List&amp;lt;E&amp;gt;&lt;/a&gt;, Dart's ordered, indexable collection of objects.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Aside: The Dart project is lucky to welcome Josh Bloch to the team, who will be leading the library design efforts. Expect some great things for the libraries and Dart!&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: x-large;"&gt;Arrays are Lists&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Perhaps the most common collection in nearly every programming language is the array, or ordered set of objects. Dart arrays are Lists, so look for &lt;a href="http://api.dartlang.org/dart_core/List.html"&gt;List&lt;/a&gt; in the documentation. A Dart List will compile to a JavaScript array.&lt;br /&gt;
&lt;br /&gt;
Fun fact: As of 2011-12-22, the word "array" does not appear in the &lt;a href="http://www.dartlang.org/docs/spec/index.html"&gt;Dart spec&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;List basics&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart supports List literals like JavaScript.&amp;nbsp;A simple Dart list:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var list = [1,2,3];
  print( list is List ); // true
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Get the list's length, or number of elements inside of the list:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var list = [1,2,3];
  print( &lt;b&gt;list.length&lt;/b&gt; ); // 3
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Access the second element in the list: (notice how Dart list indexes are 0 based, i.e. 0 is the first element, 1 is the second element, etc)&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var list = [1,2,3];
  print( &lt;b&gt;list[1]&lt;/b&gt; ); // 2
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
If you try to reference an element that is outside the length of the list, you'll get an &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;IndexOutOfRangeException&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var list = [1,2,3];
  print( &lt;b&gt;&lt;span style="color: red;"&gt;list[5]&lt;/span&gt;&lt;/b&gt; );  // &lt;span style="color: red;"&gt;Unhandled exception: IndexOutOfRangeException&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Add an element to a list constructed from a list literal:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  List list = [1,2,3];
  &lt;b&gt;list.add(4);&lt;/b&gt;
  print( list.length ); // 4
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Now is a good time to point out that the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;add(object)&lt;/span&gt; method is optional. That is, not all implementations of List have to support it. I don't personally like optional methods, but this is where we stand as of 2011-12-22.&lt;br /&gt;
&lt;br /&gt;
An example of a List that you can't add to is the fixed size List, as constructed by:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var list = &lt;b style="color: black;"&gt;new List(3)&lt;/b&gt;;
  &lt;span style="color: red;"&gt;list.add(4);&lt;/span&gt; // &lt;span style="color: red;"&gt;this throws an UnsupportedOperationException: Cannot add to a non-extendable array&lt;/span&gt;
               // exception thrown for Lists constructed with an initial size
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Unfortunately, the docs right now don't clue you in that the new List(size) constructor returns a fixed size List, but follow &lt;a href="http://code.google.com/p/dart/issues/detail?id=948"&gt;bug 948&lt;/a&gt; for the comment fix.&lt;br /&gt;
&lt;br /&gt;
It's important to know that when you construct a fixed size List, that the List itself is allocated to that size and filled with nulls for the size of the List. For example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var list = new List(5); // [null,null,null,null,null]
  print(list[0]);  // null
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Also important to know for JavaScript developers: Lists in Dart are not associative arrays. That is, the following code will NOT work in Dart:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var list = new List(); // no initial size! aka EMPTY
  &lt;span style="color: red;"&gt;list[3] = 'hello';&lt;/span&gt; // &lt;span style="color: red;"&gt;IndexOutOfRangeException&lt;/span&gt;
}&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Remove an element from a list: (I know, I know, we're lacking a simple removeAt(index) function (see &lt;a href="http://code.google.com/p/dart/issues/detail?id=947"&gt;bug 947&lt;/a&gt;). See comment about Josh Bloch above and just hang tight :)&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  List list = [1,2,3];
  list.&lt;b&gt;removeRange(1, 1)&lt;/b&gt;; // remove only the second element
  print( list.length ); // 2
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
To print the elements of a List, we need to add a &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;map()&lt;/span&gt; function to eventually convert a &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;List&lt;/span&gt; to a &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;List&amp;lt;String&amp;gt;&lt;/span&gt; because &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;Strings.join&lt;/span&gt; only takes a &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;List&amp;lt;String&amp;gt;.&lt;/span&gt;&lt;span style="font-family: inherit;"&gt;&amp;nbsp;T&lt;/span&gt;he Dart libs are lacking a map function on collections (see &lt;a href="http://code.google.com/p/dart/issues/detail?id=945"&gt;bug &amp;nbsp;945&lt;/a&gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;List map(List list, converter(x)) {
  final List result = [];
  for (final x in list) {
    result.add(converter(x));
  }
  return result;
}

List&amp;lt;String&amp;gt; listToStrings(List list) {
  return map(list, (x) =&amp;gt; x.toString());
}

main() {
  List list = [1,2,3];
  &lt;b&gt;print( Strings.join( listToStrings(list), ',' )); // 1,2,3&lt;/b&gt;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Sorting&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Sorting a List takes advantage of Dart's function support.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var list = [4,1,2];
  list.sort(&lt;b&gt;compare(a,b)&lt;/b&gt; {
    if (a == b) {
      return 0;
    } else if (a &amp;gt; b) {
      return 1;
    } else {
      return -1;
    }
  });
  // list is now 1,2,4
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Iterating&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Iterating over a List can be done in at least four ways. The standard, most familiar way is the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;for&lt;/span&gt; loop:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var list = [4,1,2];
  &lt;b&gt;for (var x = 0; x &amp;lt; list.length; x++)&lt;/b&gt; {
    print(x);
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
There's a more terse way of using &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;for&lt;/span&gt; if you don't need the current iteration index:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var list = [4,1,2];
  &lt;b&gt;for (final x in list)&lt;/b&gt; {
    print(x);
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The verbose way to say the above is to use a formal &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;Iterator&lt;/span&gt; from the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;List&lt;/span&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var list = [4,1,2];
  &lt;b&gt;Iterator i = list.iterator()&lt;/b&gt;;
  while (&lt;b&gt;i.hasNext()&lt;/b&gt;) {
    print(i.next());
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
And finally, if you just want to apply a function to each element of the List, use &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;forEach&lt;/span&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var list = [4,1,2];
  list.&lt;b&gt;forEach(f(e) =&amp;gt; print(e))&lt;/b&gt;;
}&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Filtering&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The Collection interface, which List extends, provides a filter method, which returns a new collection with only the elements that satisfy a condition.&lt;br /&gt;
&lt;br /&gt;
An example of filtering:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var list = [4,1,2];
  var evens = &lt;b&gt;list.filter(f(e) =&amp;gt; e % 2 == 0)&lt;/b&gt;;
  printList(evens); // 4,2
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Of course, you don't need to inline the filtering function. Passing in a function also works:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;&lt;b&gt;isEven(x) =&amp;gt; x % 2 == 0;&lt;/b&gt;

main() {
  var list = [4,1,2];
  var evens = list.filter(&lt;b&gt;isEven&lt;/b&gt;);
  printList(evens);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Next steps&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Check out the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;a href="http://api.dartlang.org/dart_core/Collection.html#every"&gt;every(bool f(E element))&lt;/a&gt;&lt;/span&gt; and &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;a href="http://api.dartlang.org/dart_core/Collection.html#some"&gt;some(bool f(E element))&lt;/a&gt;&lt;/span&gt; methods to check if the List matches every condition or at least one condition, respectively.&lt;br /&gt;
&lt;br /&gt;
Of course, please review the full &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;a href="http://api.dartlang.org/dart_core/Collection.html"&gt;Collection&amp;lt;E&amp;gt;&lt;/a&gt;&lt;/span&gt; docs and the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;a href="http://api.dartlang.org/dart_core/List.html"&gt;List&amp;lt;E&amp;gt;&lt;/a&gt;&lt;/span&gt; docs.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Summary&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart has Lists which are ordered sequences of objects. There is no class or interface called Array, though Lists act extremely similar to Arrays you might have encountered in other programming languages.&lt;br /&gt;
&lt;br /&gt;
Dart Lists are not associative arrays like JavaScript, but Dart does have list literals for easy declaration.&lt;br /&gt;
&lt;br /&gt;
The core Dart libraries will almost certainly undergo sweeping changes now that Josh Bloch has joined the team. For now, though, there's still a lot you can do with Dart's lists. As you work with the libraries, remember that Dart is in Technology Preview mode, and we really want to hear your feedback.&lt;br /&gt;
&lt;br /&gt;
What do you need from the libraries? Let us know at the &lt;a href="https://groups.google.com/a/dartlang.org/group/misc/topics"&gt;mailing list&lt;/a&gt; or please file an &lt;a href="http://code.google.com/p/dart/issues/list"&gt;issue&lt;/a&gt;. Thanks!&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Next steps&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Check out Part 4: &lt;a href="http://blog.sethladd.com/2012/01/maps-and-hashes-in-dart.html"&gt;Maps and hashes in Dart&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-6676632484046791729?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/p8PRwtYeAc0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/6676632484046791729/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=6676632484046791729&amp;isPopup=true" title="8 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/6676632484046791729?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/6676632484046791729?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/p8PRwtYeAc0/lists-and-arrays-in-dart.html" title="Lists and arrays in Dart" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>8</thr:total><feedburner:origLink>http://blog.sethladd.com/2011/12/lists-and-arrays-in-dart.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkMHQ3Yzfyp7ImA9WhRQGUs.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-3168987270965926218</id><published>2011-12-15T00:34:00.000-08:00</published><updated>2011-12-15T07:53:52.887-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-15T07:53:52.887-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Response to Coffeescript and Dart article on nettuts</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/MmJz47ScuemDYC1Y-iNlFcurCpI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/MmJz47ScuemDYC1Y-iNlFcurCpI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/MmJz47ScuemDYC1Y-iNlFcurCpI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/MmJz47ScuemDYC1Y-iNlFcurCpI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;span style="background-color: white; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px;"&gt;Some slight clarifications, if I may, following a &lt;/span&gt;&lt;a href="http://net.tutsplus.com/articles/interviews/should-you-learn-coffeescript/" style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px;"&gt;recent article on nettuts.com&lt;/a&gt;&lt;span style="background-color: white; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px;"&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
Jeremy said: ” In addition, Dart retreats from the dynamism of JavaScript, and instead exists as a somewhat static language, where types can be checked at compile time, but are erased at runtime. ”&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
While there are many ways to interpret “dynamism”, it’s true that Dart doesn’t have extremely dynamic features like “eval” or the ability to arbitrarily change object structure.&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
However, it’s important to point out that Dart is an optionally typed language, allowing a developer to write as much or as little static type information in the code as they wish. This was a pragmatic decision (as there are plenty of web developers that have been writing great untyped code, and who probably don’t want to see a type), however we also think optional typing allows a program to grow over time (as complexity increases, types can help with documentation and tools.)&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
Types aren’t erased at runtime, values still have their types. A string is a string, a number is a num. In fact, collections have reified types, so you can ask if a List of Strings is really a List of Strings. I think the misconception is that a few presentations out there (mine included) have said, “types don’t affect the runtime semantics of your code.” Admittedly this is something best left for language designers to debate, as real life developers want to know if types help them or just get in the way.&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
Dart programs can run in something called “checked mode”, which I find quite useful. It can use the types to catch errors earlier in the program, something always helpful.&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
James said: “Dart doesn’t seem to bring anything to the table other than a more Java-esque syntax, which many developers aren’t too fond of, myself included.”&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
This is a fair assessment from the early samples and demos we released. However, Dart code can be written to look nothing like Java. Remember, Dart is just a technology preview, and we are still figuring out how to write idiomatic Dart code. The community will probably evolve into something that doesn’t quite look like Java, and something that doesn’t quite look like JavaScript. It will be Dart code, and that’s good. :)&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
Remember that Dart code doesn’t even have to use classes, and doesn’t have to look anything like Java. For example:&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;hello(msg, to, wrapper) {&lt;br /&gt;&amp;nbsp; return '${wrapper(msg)} to ${to}';&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;importantify(msg) =&amp;gt; '!!! ${msg} !!!';&lt;/span&gt;&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;main() =&amp;gt; print(hello('world', 'Seth', importantify));&lt;/span&gt;&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
Granted that’s a contrived example, but we’re doing some non-Java things in there like string interpolation, untyped code, and passing functions as first class objects. Not to mention the syntax sugar for a one line function.&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
Nathan said: “the end-game for Dart is to get developers writing an entirely different language altogether (preferably interpreted in a browser’s VM, instead of being precompiled to JS).” and Ryan said “Dart aims to replace JavaScript”&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
The end game of Dart, and CoffeeScript, and modern JavaScript, is to help developers write more complex, full featured, high fidelity, larger modern web apps. These languages are a means to an end: a better modern web. Dart isn’t out to replace anything, any more than CoffeeScript is out to replace JavaScript. Dart is out there to help show developers on other platforms that the web is a compelling and amazing place to build apps.&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
Finally, Alex said: “Google took rather a walled garden approach to Dart, and I get the impression they didn’t really look outside the confines of their company for inspiration.”&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
It’s true, Dart was launched into Technology Preview with some ideas written down. Having some code written is better than just abstract ideas, in my opinion, because shipping code wins. But Dart is not done, and it’s open source, and there’s a lot more to do. It’s incredibly open, in fact, with all the code in the open, along with the bug tracker and mailing list. I do not believe Dart is a walled garden. The real test is how the team will accept and integrate outside patches, but I don’t sense this will be an issue.&lt;/div&gt;
&lt;div style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;
As for inspiration, there has been plenty of outside and historical influence. Lars Bak, the lead of the project, has been working on virtual machines for many years. His team was responsible for V8, so they must have learned a lot there. Gilad Bracha, leading the spec, worked on the Java language spec and generics, and definitely learned a lot of good and bad lessons there. Many on the team are former Smalltalkers, like Peter Ahe. Bob Nystrom even built his own language. Josh Bloch, of Effective Java fame, knows a thing or two about designing libraries. I’m sure I’m missing people, and I apologize. And we’d be remiss if we didn’t mention CoffeeScript as part of the inspiration, for it is clear evidence that developers are hungry for options and alternatives.&lt;br /&gt;
&lt;br /&gt;
&lt;br class="Apple-interchange-newline" /&gt;(update: this is a repost of my&amp;nbsp;&lt;a href="http://net.tutsplus.com/articles/interviews/should-you-learn-coffeescript/comment-page-1/#comment-391469"&gt;original comment&lt;/a&gt;&amp;nbsp;on the article. if you'd like to respond, we should probably carry on the conversation over at the &lt;a href="http://net.tutsplus.com/articles/interviews/should-you-learn-coffeescript/"&gt;original article&lt;/a&gt;)&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-3168987270965926218?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/h3C3cF4nJRM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/3168987270965926218/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=3168987270965926218&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/3168987270965926218?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/3168987270965926218?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/h3C3cF4nJRM/response-to-coffeescript-and-dart.html" title="Response to Coffeescript and Dart article on nettuts" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.sethladd.com/2011/12/response-to-coffeescript-and-dart.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0cHRX85fSp7ImA9WhRXEU0.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-1017364757168615712</id><published>2011-12-14T23:34:00.000-08:00</published><updated>2011-12-17T00:03:54.125-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-17T00:03:54.125-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Learning Functions for Dart</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/wkg-11Vn7ACsl952AzvFjbVCDdM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/wkg-11Vn7ACsl952AzvFjbVCDdM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/wkg-11Vn7ACsl952AzvFjbVCDdM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/wkg-11Vn7ACsl952AzvFjbVCDdM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;(This is Part 2 in a series on Dart. See Part 1: &lt;a href="http://blog.sethladd.com/2011/11/variables-and-optional-types-in-dart.html"&gt;Variables and optional types in Dart&lt;/a&gt;)&lt;br /&gt;
&lt;br /&gt;
While it's true that Dart is an object oriented, class based, single inheritance language with interfaces, there's nothing forcing you to use classes. If you haven't seen a class in your life, or you don't want to use classes, you will be pleased to see that Dart can function quite nicely with, well, just functions.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Basics&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The most simple function I could come up with:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello() =&amp;gt; 'world';
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The&amp;nbsp;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;=&amp;gt; e&lt;/span&gt;&amp;nbsp;syntax is shorthand for a function body of the form&amp;nbsp;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;{return e;}&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
The above is the same as:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello() {
  return 'world';
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Notice how the return type is omitted. Dart is an optionally typed language, so the programmer is free to omit static type declarations.&lt;br /&gt;
&lt;br /&gt;
The above is the same as:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;Dynamic hello() {
  return 'world';
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The Dynamic type is the "unknown" type. From the spec: "If no static type annotation has been provided the type system assumes the declaration has the type Dynamic." You probably won't see real life code actually statically specify the Dynamic type, because the system will insert it if it needs to.&lt;br /&gt;
&lt;br /&gt;
The above can also be written as:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;String hello() {
  return 'world';
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The above example statically declares &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;String&lt;/span&gt; as the return type. Compare this version to the very first one-line example.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Parameters&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Adding parameters to functions is familiar:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello(msg) =&amp;gt; msg;
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Like all Dart code, types are optional for function parameters. For example, the above is the same as the below:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello(String msg) =&amp;gt; msg;
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
We think types are useful for interfaces, methods, functions, and any place you need to either communicate with your editing tools or larger team. If you haven't seen a type in your life, Dart isn't going to force types on you. However, they are useful for documentation and catching some errors early.&lt;br /&gt;
&lt;br /&gt;
Multiple parameters are supported as well:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello(msg, to) =&amp;gt; msg + ' for ' + to;
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
We can do better than the overloaded &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;+&lt;/span&gt; sign to concatenate strings together. The above is the same as the below:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello(msg, to) =&amp;gt; '${msg} for ${to}';
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Ahhh, much better! Not only does the above string interpolation read better (for me, at least) there's a subtle potential performance improvement. Due to Dart's optional typed nature, using the + concatenation means the running program must first determine if it's adding two numbers or two strings. However, using the string interpolation method (eg, above) means the running program can make assumptions.&lt;br /&gt;
&lt;br /&gt;
Of course the above is the same as the below, with types specified:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello(String msg, String to) =&amp;gt; '${msg} for ${to}';
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Optional Parameters&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart has little gems to help you write programs quickly and easily. One of those gems is optional parameters. For example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello(msg, to, &lt;b&gt;[from]&lt;/b&gt;) =&amp;gt; '${from} sent ${msg} to ${to}';
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The brackets around the parameter names indicate that parameter is optional. &amp;nbsp;For example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello(msg, to, [from]) =&amp;gt; '${from} sent ${msg} to ${to}';

main() =&amp;gt; print(hello('world', 'Seth')); &lt;b&gt;// doh! forgot my optional parameter&lt;/b&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;"&lt;b&gt;null&lt;/b&gt; sent world to Seth"&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Of course, the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;null&lt;/span&gt; in the above example appears because I never passed in a value for &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;from&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
The following example does pass in a value for the last (optional) parameter:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello(msg, to, [from]) =&amp;gt; '${from} sent ${msg} to ${to}';

main() =&amp;gt; print(hello('world', 'Seth', &lt;b&gt;'Bob'&lt;/b&gt;));
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;"Bob sent world to Seth"&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Named Parameters&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Turns out this gem has a gem of its own. Optional parameters are also &lt;i&gt;named&lt;/i&gt; parameters. Check this out:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello(msg, to, [from]) =&amp;gt; '${from} sent ${msg} to ${to}';

main() =&amp;gt; print(hello('world', 'Seth', &lt;b&gt;from:'Bob'&lt;/b&gt;));
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
This implies that named parameters can also be reordered when calling the function. For the below example, I added two optional and named parameters:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello(msg, to, &lt;b&gt;[from, rate]&lt;/b&gt;) =&amp;gt; '${from} sent ${msg} to ${to} via ${rate}';

main() =&amp;gt; print(hello('world', 'Seth', &lt;b&gt;from:'Bob', rate:'First Class'&lt;/b&gt;));
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Because named parameters can be reordered, the above can be rewritten as:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello(msg, to, [from, rate]) =&amp;gt; '${from} sent ${msg} to ${to} via ${rate}';

main() =&amp;gt; print(hello('world', 'Seth', &lt;b&gt;rate:'First Class', from:'Bob'&lt;/b&gt;));
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
This is certainly flexible stuff.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Default Parameters&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Yet another bonus of using optional parameters is that they can be given default values. For example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello(msg, to, [from, &lt;b&gt;rate='First Class'&lt;/b&gt;]) =&amp;gt; '${from} sent ${msg} to ${to} via ${rate}';

main() =&amp;gt; print(hello('world', 'Seth', from:'Bob')); &lt;b&gt;// doh! forgot rate&lt;/b&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;"Bob sent world to Seth via First Class"&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
From the spec: "If no default is explicitly specified for an optional parameter, but a default could legally be provided, an implicit default of &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;null&lt;/span&gt; is provided."&lt;br /&gt;
&lt;br /&gt;
It should also be noted that default values must be compile time constants. (we'll talk more about what this means in a future blog post) That is, you can't do something like&amp;nbsp;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;strike&gt;hello(msg, to, [&lt;span style="color: red;"&gt;from=new foo()&lt;/span&gt;, rate='First Class'])&lt;/strike&gt;&lt;/span&gt;&lt;span style="font-family: inherit;"&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit; font-size: large;"&gt;Inner Functions&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;Dart understands that there are plenty of developers that don't want to see a class yet still want some composability. One tool that the seasoned web developer will find useful is the "inner function" (what is the proper name here?) &amp;nbsp;Yes, you can nest functions!&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;An example of a nested (aka inner) function:&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello(msg, to) {
  &lt;b&gt;importantify(msg) =&amp;gt; '!!! ${msg} !!!';&lt;/b&gt;

  return '${&lt;b&gt;importantify(msg)&lt;/b&gt;} to ${to}';
}

main() =&amp;gt; print(hello('world', 'Seth'));
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; color: black; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;"!!! world !!! to Seth"&lt;/pre&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
I've seen developers use these nested functions as a way to document and organize their function bodies. With proper use of nested functions, you probably don't even need comments inside a function.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Functions are First Class&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart treats functions as first class objects, further reinforcing the notion that you don't need classes if you don't want them. For example, you can pass a function to a function:&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;hello(msg, to, &lt;b&gt;wrapper&lt;/b&gt;) {
  return '${&lt;b&gt;wrapper&lt;/b&gt;(msg)} to ${to}';
}

importantify(msg) =&amp;gt; '!!! ${msg} !!!';

main() =&amp;gt; print(hello('world', 'Seth', &lt;b&gt;importantify&lt;/b&gt;));
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;"!!! world !!! to Seth"&lt;/code&gt;&lt;/pre&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit; font-size: large;"&gt;Summary&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
Even though Dart is a object oriented, class based, single inheritance language with interfaces, developers can be quite productive and happy using only functions. In fact, if a developer never wants to see a class, they will still find Dart useful and interesting.&lt;br /&gt;
&lt;br /&gt;
Dart supports simple functions with optional and named parameters, with optional default values. Dart functions are first class objects, and can be nested inside other functions.&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit; font-size: large;"&gt;Epilogue&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
Dart is not done, and has been launched as a Technology Preview in an effort to solicit early feedback from the community. Please check out this new open source structured web programming language, libraries, VM, and editor and let us know what you think. Learn more at &lt;a href="http://dartlang.org/"&gt;dartlang.org&lt;/a&gt; and join the &lt;a href="http://www.dartlang.org/support/index.html"&gt;conversation&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
The&lt;a href="http://try.dartlang.org/"&gt; easiest way to try Dart is the browser based Dartboard&lt;/a&gt;, allowing you to write and run Dart code without downloading anything.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-1017364757168615712?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/9aEOByuLdjg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/1017364757168615712/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=1017364757168615712&amp;isPopup=true" title="7 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/1017364757168615712?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/1017364757168615712?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/9aEOByuLdjg/learning-functions-for-dart.html" title="Learning Functions for Dart" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>7</thr:total><feedburner:origLink>http://blog.sethladd.com/2011/12/learning-functions-for-dart.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUAGRXc7fip7ImA9WhRQEEk.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-8660460518043849149</id><published>2011-12-04T16:21:00.001-08:00</published><updated>2011-12-04T17:15:24.906-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-04T17:15:24.906-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Dart's Sweet Spot is Web Apps</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/yK8ok7EXZVpwvt5JRki8A1YKk3Q/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/yK8ok7EXZVpwvt5JRki8A1YKk3Q/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/yK8ok7EXZVpwvt5JRki8A1YKk3Q/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/yK8ok7EXZVpwvt5JRki8A1YKk3Q/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;I've been talking to different people interested in &lt;a href="http://www.dartlang.org/"&gt;Dart&lt;/a&gt;, and a pattern is emerging. It's becoming more clear that Dart is appealing to application developers who are interested in building modern web apps. My theory is that Dart's adoption will accelerate most quickly from developers looking to build apps.&lt;br /&gt;
&lt;br /&gt;
Bringing more apps to the web is a very good thing and is crucial for the long term success of the web. Right now, it's easy to argue that when software developers hear the word "apps" they think of mobile native apps. This is dangerous for the web, because if developer mindshare shifts away from the web as a place to deliver high fidelity and engaging application experiences, the web runs the risk of slowing its evolution and remaining stuck as a document platform.&lt;br /&gt;
&lt;br /&gt;
Of course, the web has done tremendously well as a document platform. It's clearly the most successful and widely distributed information platform we've seen. To be fair, the web isn't a slouch for apps, either. Thanks to features like fast JavaScript engines, XMLHttpRequest, local storage, web workers, Canvas, and a slew of modern HTML5 APIs, the web can deliver relatively high performance applications to modern browsers.&lt;br /&gt;
&lt;br /&gt;
Unfortunately, the typical application developer has a set of requirements and expectations that aren't easily answered by the web platform that we know and love today. For example, the application developer needs IDEs with powerful features like refactoring, integrated debugging, and comprehensive project visibility. The app developer also needs standard and integrated libraries, helping save time and increase commonality across projects. The app developer also needs a structured programming language, with the right mix of familiar features and unique optimizations for the platform.&lt;br /&gt;
&lt;br /&gt;
The Dart effort is a comprehensive approach at addressing all of the above concerns: structured language, common libraries, optimized virtual machine, modularity, and editors. Dart is also a chance to take a fresh look at the web platform and build an integrated ecosystem without some of the baggage that has built up over the past 15 years or so.&lt;br /&gt;
&lt;br /&gt;
While I believe that some of the web's current designers and developers will take to Dart (and certainly ActionScript developers will find much to like), I don't think web pages and web sites are the current sweet spot for Dart. It's becoming more and more clear that Dart's sweet spot is modern web apps.&lt;br /&gt;
&lt;br /&gt;
The Dart team is working hard to make it easy and exciting for the app developer to build modern web apps. It's no wonder that app developers on other platforms are taking an interest in Dart, for they can see the parallels between their current environments and Dart.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Epilogue&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://dart.googlecode.com/"&gt;Dart is open source&lt;/a&gt; and is currently in Technology Preview, so now is the perfect time to check it out and suggest features and direction. The &lt;a href="https://plus.google.com/115427174005651655317/posts/FpMTm9aBDii"&gt;team and mailing list are friendly&lt;/a&gt;, so don't be shy. You can &lt;a href="http://try.dartlang.org/"&gt;try Dart&lt;/a&gt; without installing anything, with (what else?) a browser based app.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-8660460518043849149?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/lpJNfU8-n1M" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/8660460518043849149/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=8660460518043849149&amp;isPopup=true" title="7 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/8660460518043849149?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/8660460518043849149?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/lpJNfU8-n1M/darts-sweet-spot-is-web-apps.html" title="Dart's Sweet Spot is Web Apps" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>7</thr:total><feedburner:origLink>http://blog.sethladd.com/2011/12/darts-sweet-spot-is-web-apps.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0YFQHw7fSp7ImA9WhRXEU0.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-6747682931147769263</id><published>2011-11-29T19:57:00.001-08:00</published><updated>2011-12-17T00:05:11.205-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-17T00:05:11.205-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Variables and Optional Types in Dart</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/0Wt0NtYbZ5sfyynXQyfEuR0HG6Q/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0Wt0NtYbZ5sfyynXQyfEuR0HG6Q/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/0Wt0NtYbZ5sfyynXQyfEuR0HG6Q/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0Wt0NtYbZ5sfyynXQyfEuR0HG6Q/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;span style="font-size: x-small;"&gt;&lt;b&gt;Update&lt;/b&gt;: Reworked some of the optional types section based on comments.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Intro&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.dartlang.org/"&gt;Dart&lt;/a&gt; is a structured web programming language, complete with libraries, a virtual machine, and an editor. Dart is designed to help developers create more complex and feature rich apps for the modern web.&lt;br /&gt;
&lt;br /&gt;
In this post, we look at variables and their types in Dart.&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Variables&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The most simple example declares a variable without an explicit type:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var msg = 'hello';
  print(msg);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; color: black; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&amp;gt; hello&lt;/pre&gt;
&lt;br /&gt;
The most common way to declare a variable is to use the &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;var&lt;/span&gt; keyword. This is truly a variable, as shown here:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  var msg = 'hello';
  msg = 'world';
  print(msg);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;&amp;gt; world&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-size: large;"&gt;final&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
So far, this is a lot like JavaScript. Let's see what Dart can do differently. Dart allows you to mark variables as&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;final&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;&lt;span class="Apple-style-span" style="background-color: #eeeeee;"&gt;main() {
  &lt;/span&gt;&lt;b style="background-color: #eeeeee;"&gt;final&lt;/b&gt;&lt;span class="Apple-style-span" style="background-color: #eeeeee;"&gt; msg = 'hello';
  &lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: red; color: white;"&gt;msg = 'world';  // ERROR: cannot assign value to final variable&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: #eeeeee;"&gt;
  print(msg);
}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
This all looks simple, but let's reflect on what we've seen. In the above examples, there are no classes. While Dart supports classes, single inheritance, and interfaces, they are not required to write Dart code. Plenty of Dart code is written with minimal or zero classes.&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-size: large;"&gt;static&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Having said that, we're going to introduce a class to illustrate the next concept.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;class Bar {
  static x = 4;
}

main() {
  print(Bar.x);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;&amp;gt; 4&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
In Dart, variables can also be &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;static&lt;/span&gt; to a class. Like in Java, a static variable is associated to the class and not the instance. Notice how, in the above example, we don't need an instance of the Bar class in order to access the &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;x&lt;/span&gt; variable.&lt;br /&gt;
&lt;br /&gt;
A common pattern is to make static variables final, usually to declare enumerations or human readable names for codes.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;class States {
  static final ON = 1;
  static final OFF = 0;
}

main() {
  print(States.ON);
}&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;&amp;gt; 1&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
We will cover classes in depth in a future blog post.&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-size: large;"&gt;Privacy&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart has two levels of privacy for its variables: public and private &lt;i&gt;to its library&lt;/i&gt;. We will cover how private works when we introduce libraries in another blog post. For now, everything you see is public.&lt;br /&gt;
&lt;br /&gt;
You might see variables with names prefixed with underscore like &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;_foo&lt;/span&gt;. The underscore marks the variable as private to the library it was declared in. More on this soon.&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Optional Types&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
All the variables so far have not declared a type. Dart supports optional typing, and treats type declarationss basically like annotations for tools and documentation. Type declarations do not affect the running code.&lt;br /&gt;
&lt;br /&gt;
To start, let's look at a simple untyped example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;multiply(x, factor) {
  return x * factor;
}

main() {
  print( multiply(4, 2) );
}
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;&amp;gt; 8&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
This of course works, but imagine importing this code into your project. It would be helpful to know what exactly you can pass to the multiply method, giving tools the option to help you or get warnings earlier. So we can add types:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;multiply(&lt;b&gt;num&lt;/b&gt; x, &lt;b&gt;num&lt;/b&gt; factor) {
  return x * factor;
}

main() {
  print( multiply(4, 2) );
}
&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// 8&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Now the expectations of multiply are more clear, and with more clarity comes easier collaboration with larger teams and more useful tools. We can even catch errors earlier. Consider this code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; color: black; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;&lt;span style="background-color: #eeeeee;"&gt;multiply(num x, num factor) {
  return x * factor;
}

main() {
  print( multiply(4, &lt;/span&gt;&lt;b style="background-color: #eeeeee;"&gt;"hello"&lt;/b&gt;&lt;span style="background-color: #eeeeee;"&gt;) ); &lt;/span&gt;&lt;span style="background-color: yellow;"&gt;// WARNING at this line, string isn't assignable to num&lt;/span&gt;&lt;span style="background-color: #eeeeee;"&gt;
}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; color: black; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;span style="font-family: monospace;"&gt;&amp;gt; NaN // because you can't multiple a string with a number&lt;/span&gt;&lt;/pre&gt;
&lt;br /&gt;
With the types specified, we can see a warning at the line with the problem. If we didn't have the types specified in &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;multiply&lt;/span&gt;, we would still get an error, but we wouldn't get the early warning.&lt;br /&gt;
&lt;br /&gt;
Remember, though, that while types are great annotations for tools and teams, the type declarations don't affect running code.&lt;br /&gt;
&lt;br /&gt;
For example, consider this wacky code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; color: black; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;&lt;span class="Apple-style-span" style="background-color: #eeeeee;"&gt;main() {
  &lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: yellow;"&gt;num msg = 'hello'; // WARNING: String is not assignable to num&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: #eeeeee;"&gt;
  print(msg);
}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;&amp;gt; hello  // still runs!&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
We've replaced the &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;var&lt;/span&gt; keyword with the &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;num&lt;/span&gt; type declaration. We also did a crazy and assigned a string to what we declared was a number. In more traditional languages, this would have stopped the program in its tracks. However, for Dart, a warning can be reported by the compiler but &lt;b&gt;the program runs just fine&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
Does this mean that Dart can run any program no matter how messed up it is? Of course not, if you write code that makes no sense, it will fail to run:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"&gt;&lt;code&gt;&lt;span class="Apple-style-span" style="background-color: #eeeeee; color: black;"&gt;main() {
  num x = "hello";
  &lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: red;"&gt;&lt;span class="Apple-style-span" style="color: white;"&gt;x = x / 2; // NoSuchMethodException&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: #eeeeee; color: black;"&gt;
  print(x);
}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
What's important to remember is that if you write code that can run, it will run, even if the type declarations aren't correct.&lt;br /&gt;
&lt;br /&gt;
To language purists, this sounds awful, for how can you have a programming language with an unsound type system!?!?! Back in the real world, though, we encounter languages with unexpressive type systems everywhere. Every time you perform a cast in your favorite language, you are expressing something your type system can't handle.&lt;br /&gt;
&lt;br /&gt;
Dart is a fan of optional types because they reduce programmer friction. Also important to remember, the web programmer is very much used to a dynamic type system, so forcing static mandatory types in a language bound for the web would hinder adoption.&lt;br /&gt;
&lt;br /&gt;
Types aren't just ephemeral concepts, though. Running Dart programs actually do know what the types of its variables are. For example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;main() {
  print(4 is String);        // false
  print("hello" is String);  // true
  print(4 is Dynamic);       // true
  print(4 is num);           // true

  var x = 1;
  print(x is Dynamic);       // true
  print(x is num);           // true
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;is&lt;/span&gt; check is not affected by the declared types, instead it is checking what the type of the variable actually is.&lt;br /&gt;
&lt;br /&gt;
As you can see in the above code, variables always appear as Dynamic, in order to interoperate with code that did not specify its types.&amp;nbsp;The&amp;nbsp;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;var&lt;/span&gt;&amp;nbsp;keyword is shorthand for the Dynamic type, aka the unknown type.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;Built-in types&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart provides Object as the super type. You'll also find:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Dynamic (used when no static type is provided)&lt;/li&gt;
&lt;li&gt;String&lt;/li&gt;
&lt;li&gt;bool&lt;/li&gt;
&lt;li&gt;num&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;int&lt;/li&gt;
&lt;li&gt;double&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Collection&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;List&lt;/li&gt;
&lt;li&gt;Set&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Map&lt;/li&gt;
&lt;li&gt;others...&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Summary&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Dart supports variables with optional typing, defaulting to the Dynamic type if no static type annotation is declared. Variables can be marked as final, which means its value cannot be altered. Classes themselves can have variables if they are marked as static.&lt;br /&gt;
&lt;br /&gt;
Dart's optional typing means the program will run with or without declared types, and even if the type declarations are incorrect. Dart variables and objects have types at runtime, however.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: x-large;"&gt;Next Steps&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Learn about &lt;a href="http://blog.sethladd.com/2011/12/learning-functions-for-dart.html"&gt;functions in Dart&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-6747682931147769263?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/qmlLiYkpIpY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/6747682931147769263/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=6747682931147769263&amp;isPopup=true" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/6747682931147769263?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/6747682931147769263?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/qmlLiYkpIpY/variables-and-optional-types-in-dart.html" title="Variables and Optional Types in Dart" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>3</thr:total><feedburner:origLink>http://blog.sethladd.com/2011/11/variables-and-optional-types-in-dart.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUUFRH07eSp7ImA9WhRRFU8.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-8828464467736769077</id><published>2011-11-27T21:08:00.001-08:00</published><updated>2011-11-28T15:33:35.301-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-28T15:33:35.301-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Why Dart Excites Me</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/aOoApj5CVLhfcUWrPovoSdxBP_c/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/aOoApj5CVLhfcUWrPovoSdxBP_c/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/aOoApj5CVLhfcUWrPovoSdxBP_c/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/aOoApj5CVLhfcUWrPovoSdxBP_c/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;Intro&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.dartlang.org/"&gt;Dart&lt;/a&gt;, the new open source structured web programming language currently in technology preview, is making me excited about web programming again. I'm watching it closely, fully understanding it's extremely early in the game to make any predictions. However, at least I can say I'm hopeful that there's a team out there attacking the very real problem of "not enough complex and full featured web apps" by working on an a language/libraries/VM &lt;i&gt;option&lt;/i&gt; for modern web apps.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-size: large;"&gt;My background&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
I am a Java developer in a past life, where I grew to love the rich libraries, composable and modular applications, helpful tools, and refactoring techniques. I am a web developer at heart, with a true love for browser based client side web apps. This dichotomy was a bit of an uneasy fit, as part of me wanted a language with more structure (enabling tooling and modularity), and the other part of me wanted the insanely quick iterations of web development and the native-to-the-browser experience.&lt;br /&gt;
&lt;br /&gt;
Dart really strikes a chord with me as it seems like it can be a development environment that answers to both desires: &lt;i&gt;structured development&lt;/i&gt; and &lt;i&gt;web development&lt;/i&gt;. I can build modular, structured web apps which are assisted by tools while still targeting the browser for modern web apps.&lt;br /&gt;
&lt;br /&gt;
Just the pure fact that I can refactor my web app is enough to get me to pay attention.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-size: large;"&gt;How does Dart help?&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Dart has a familiar syntax&lt;/b&gt; to any Java or JavaScript developer. I know some people wanted Dart to be an esoteric and unique language, but Dart was built for mass adoption. It needed to be accessible by the average app developer.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Dart is a class based&lt;/b&gt;, single inheritance, language with interfaces and libraries. You can build a structured web app and compose it in a standard and well defined way. Most of those patterns and practices you learned in other class based languages are available to you. Perhaps best of all, the libraries built by one developer are accessible to another developer in a standard way. It's important to point out that classes are essentially optional, as Dart supports bare functions.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Dart is single threaded, but supports concurrency&lt;/b&gt; through isolates. This means Dart avoids the complexity of multi-threaded languages, yet can take advantage of ever prevalent multiple core machines. Isolates are inspired by Erlang actors.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Dart is optionally typed&lt;/b&gt;, perhaps the most contentious design decision. Understanding the constraints under which Dart is developed makes it clear why this is the right choice. Dart needs to compile to JavaScript and needs to be accessible to the millions upon millions of JavaScript developers. Forcing a type system, which would almost certainly be unsound anyway, introduces friction for the developer and the JavaScript compiler. However, types are very useful as annotations to tools, e.g. to allow refactoring, and for documenting libraries and interfaces. The types don't affect the running program, which is why they are optional.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Dart can start fast&lt;/b&gt;, due to snapshots. A Dart app's heap can be snapshotted and sent over the wire, significantly reducing application startup time. A browser, for instance, doesn't have to re-parse the application's code on page reload or startup.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Dart is batteries included&lt;/b&gt;, delivering much more than just a language. The Dart effort is a full attempt at making web developers more productive. The Dart ecosystem is working on a language, libraries, concurrency, a virtual machine, an editor, a documentation tool, a compiler to JavaScript, and even a UI framework. These are coordinated efforts, intended to address developer productivity from many different angles.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Dart is novel&lt;/b&gt;, introduces fun concepts like "classes are interfaces", isolate based concurrency, named constructors, factory constructors, and of course optional types.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Dart fixes the DOM&lt;/b&gt; with a &lt;a href="http://www.dartlang.org/articles/improving-the-dom/"&gt;Dart friendly DOM library&lt;/a&gt; that feels natural and refreshing. The Dart team has created a new HTML library for adding events, finding elements, adding elements, that takes advantage of Dart's syntax.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Dart is built by smart people&lt;/b&gt;. The team is lead by Lars Bak, who lead the V8 JavaScript VM team and previously worked on the HotSpot VM machine for Sun. The language spec is lead by Gilad Bracha, who previously worked on the Java Language Spec. Other team members have worked on different languages, virtual machines, web browsers, and development environments. This is a focused team with a serious drive to make web developers more productive.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-size: large;"&gt;How to get started&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Dart is currently (as of 2011-11) in "technology preview", but you can play with it today. I recommend these steps:&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Read the &lt;a href="http://www.dartlang.org/docs/technical-overview/index.html"&gt;Technical Overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Download the &lt;a href="http://www.dartlang.org/docs/getting-started/editor/index.html"&gt;Dart Editor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Read about the &lt;a href="http://www.dartlang.org/articles/improving-the-dom/"&gt;new DOM libraries for Dart&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Write some &lt;a href="http://www.dartlang.org/samples/index.html"&gt;sample code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Send feedback to the &lt;a href="https://groups.google.com/a/dartlang.org/group/misc/topics"&gt;mailing list&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Subscribe to the &lt;a href="http://www.dartlang.org/atom.xml"&gt;News and Updates Atom feed&lt;/a&gt;&amp;nbsp;and follow &lt;a href="http://twitter.com/dart_lang"&gt;@dart_lang&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Write more code&lt;/li&gt;
&lt;li&gt;Send more feedback&lt;/li&gt;
&lt;li&gt;Repeat!&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-size: large;"&gt;Summary&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
As a software engineer with exposure to many different languages and environments, Dart seems to hit a sweet spot for me. Dart is what I want to build my modern, structured web applications: a class based, single inheritance language with interfaces, optional typing, just enough sugar, and isolates based concurrency. Dart is a "batteries included" effort, aiming to deliver a language, libraries, editor, and virtual machine. Dart's ultimate goal is to help modern web developers more productive and deliver bigger and better web applications.&lt;br /&gt;
&lt;br /&gt;
I am excited because Dart is an effort to make "programming in the large" easier for both structured and unstructured developers across the entire web. Anything that tries to make the modern web compelling for modern apps is exciting to me.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-size: large;"&gt;Resources&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
If you are interested in building modern web apps for modern browsers, and are looking for a more structured "batteries included" ecosystem, now is the time to look at Dart. It's early in the game, so your feedback has the chance to make an impact. Here's how you get started:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.dartlang.org/"&gt;The official Dart website&lt;/a&gt;&amp;nbsp;- hosting the library reference, language spec, articles, etc&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.dartlang.org/docs/getting-started/editor/index.html"&gt;The Dart editor&lt;/a&gt;&amp;nbsp;- start with this tool&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.dartlang.org/articles/index.html"&gt;Dart articles&lt;/a&gt; - good for getting perspective&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.dartlang.org/slides/index.html"&gt;Dart presentations&lt;/a&gt; - slides from talks and presentations&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.sethladd.com/2011/11/transcription-of-quick-tour-of-dart-by.html"&gt;Transcript of a Quick Tour of Dart&lt;/a&gt; - talk by Gilad Bracha&lt;/li&gt;
&lt;li&gt;&lt;a href="http://dartwatch.com/"&gt;Dart Watch&lt;/a&gt; - Dart blog&lt;/li&gt;
&lt;li&gt;&lt;a href="https://plus.google.com/106846248004202631982/posts"&gt;Dart News&lt;/a&gt; - G+ stream of all things Dart&lt;/li&gt;
&lt;li&gt;&lt;a href="http://try.dartlang.org/"&gt;Dartboard&lt;/a&gt; - try writing Dart code in the browser, no downloads required!&lt;/li&gt;
&lt;li&gt;&lt;a href="https://groups.google.com/a/dartlang.org/group/misc/topics"&gt;Dart Mailing List&lt;/a&gt; - general Dart discussion. Post your feedback here.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-8828464467736769077?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/anLULazntTU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/8828464467736769077/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=8828464467736769077&amp;isPopup=true" title="6 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/8828464467736769077?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/8828464467736769077?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/anLULazntTU/why-dart-excites-me.html" title="Why Dart Excites Me" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><thr:total>6</thr:total><feedburner:origLink>http://blog.sethladd.com/2011/11/why-dart-excites-me.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CU8FRn88fCp7ImA9WhRSEkg.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-3171894127794614597</id><published>2011-11-13T22:50:00.001-08:00</published><updated>2011-11-13T22:56:57.174-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-13T22:56:57.174-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dartlang" /><category scheme="http://www.blogger.com/atom/ns#" term="dart" /><title>Transcription of A Quick Tour of Dart by Gilad Bracha</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/AoZOCnBnDYoj7Cm0fKuVq7SC4iU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/AoZOCnBnDYoj7Cm0fKuVq7SC4iU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/AoZOCnBnDYoj7Cm0fKuVq7SC4iU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/AoZOCnBnDYoj7Cm0fKuVq7SC4iU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;On November 2nd, 2011, &lt;a href="http://bracha.org/"&gt;Gilad Bracha&lt;/a&gt; gave a talk at Stanford University titled "A Walk on the Dart Side: A Quick Tour of Dart". I transcribed nearly all of the video so you can more easily skim and study the material. &amp;nbsp;Thanks to &lt;a href="https://plus.google.com/u/1/106846248004202631982/posts"&gt;Dart News&lt;/a&gt; for pushing to YouTube. Learn more about the &lt;a href="http://dartlang.org/"&gt;Dart language&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;iframe allowfullscreen="" frameborder="0" height="315" src="http://www.youtube.com/embed/-IavVtOE_Fg" width="420"&gt;&lt;/iframe&gt;

&lt;br /&gt;
&lt;br /&gt;
(disclaimer, I did my best to capture what was said, any errors are my own)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style="background-color: transparent;"&gt;
&lt;ul id="internal-source-marker_0.6702709852252156"&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;a href="http://youtu.be/-IavVtOE_Fg?t=1m49s"&gt;&lt;span style="background-color: transparent; color: #000099; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;We’re talking about dart.&lt;/span&gt;&lt;/a&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; It’s a new language we’re working on at Google. It’s really not my work. I joined the team the 3 months ago, but this project has been running for about a year. I’ve just started to contribute to this in any way I can. Overall, it’s the work of the Dart team, which is a fairly significant team.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;a href="http://youtu.be/-IavVtOE_Fg?t=2m19s"&gt;&lt;span style="background-color: transparent; color: #000099; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;At 50,000 ft,&lt;/span&gt;&lt;/a&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; the overall summary is that it’s a language for programming the web. &amp;nbsp;What is special about the web? In principle there’s nothing special about the web, but in practice there’s a lot that’s special about the web. And we’ve had an arguably had a regression in terms of tooling and the overall environment that we use to program the web applications in the browser. The fact is that making a significant application that runs in the browser that is competitive with what are considered good applications that are written natively is a very very challenging thing. This happens once in a while but it’s much harder and people jump through a lot of hoops. &amp;nbsp;We want to make it easier.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;a href="http://youtu.be/-IavVtOE_Fg?t=3m13s"&gt;&lt;span style="background-color: transparent; color: #000099; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;There are two really tough constraints&lt;/span&gt;&lt;/a&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; on this design. One is that there are a lot of people out there programming the web, not necessarily all Stanford grads or Google employees. We want something that is essentially on-site familiar to them, because we’re interested in adoption. I’ve been involved in languages that got adoption, but I’ve also been involved in languages that are more fun but got no adoption. We want this to be unsurprising to the mainstream programmer. We have another constraint in that it must effeciently compile to JavaScript. Which constraint is harder I’ll leave up to you. They are fairly challenging constraints, really. Look at everything that comes in the light of these two constraints.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;a href="http://youtu.be/-IavVtOE_Fg?t=4m16s"&gt;&lt;span style="background-color: transparent; color: #000099; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;The actually technical summary of what Dart is&lt;/span&gt;&lt;/a&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;: an object oriented, optionally typed, class based, single inheritance language with actor-based concurrency of some form. So you can yawn now and say, “What’s so special about that, that’s done a lot of times before.” Largely that’s true, though some of these things haven’t hit the mainstream yet in very wide usages. We hope to do our part to bring our technologies to a very wide audience. The one thing that’s a bit unusual is this business about optionally typed. It’s also gotten people all fired up because there’s nothing better than a good religious argument about typing to keep people interested. The only thing better is to argue about syntax, which is even more fun!&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Optional types are probably the main thing I’ll be talking about. I’ll also be talking about supporting abstract data types without types or dynamically typed languages as it were, which is slightly interesting, and built-in factory support. All of things are what’s modestly new in Dart that hasn’t been as widely circulated as the rest, by and large. The language is designed as I said to be familiar and unsurprising.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Optional types. What do we mean? First of all, a bit of terminology. Optional types as opposed to mandatory types. Mandatory types are the types you know and love or hate as the case may be in most “statically typed” programming languages. The idea is that the type’s are required, and a legal program has to pass the type checker, otherwise it can’t compile and it can’t run. There are many examples of that: . By no means intending to disparage them, they have a philosophy about types. But you can read the color scheme as “leaving the tyranny of type theory behind and into the sun lit plains of the wild west and optional typing.” &amp;nbsp;Depending on your religion, of course. :)&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;So, we don’t want this (mandatory types). Both for our own experience, I’ve worked with both statically typed and dynamically typed languages and I have a preference but much more important the web is already used to dynamically typed languages. For better or for worse, you can argue about it but that’s where our demographic is. &lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Brief history of non-mandatory types: there’s been many many years on this idea. Many people want to have the advantage of static typing but don’t want the disadvantages. I’m not doing an academic talk so not spending a lot of time on prior work, but here are some examples: Common Lisp, Scheme, Cecil, Erlang, Strongtalk, BabyJ. As usual, it’s all been done first in Lisp, I’m not even going to contest that. The Scheme people did an approach called soft typing, perhaps the most academic work. Strongtalk was a language I was involved with many years ago, worked with Lars Bak, who is perhaps the father of Dart, as it were. Strongtalk was significant perhaps more for the engineering work done, which eventually became the precursor to HotSpot. It had lots of other innovations at the time, including an optional type system. The one that I’m interested in, of course, is the one I worked on (Strongtalk). Of course, how could it be different. :) I some ways it’s the precursor to the work we’re doing on Dart.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Optional Types: The idea of optional types as we see them is A) of course they are syntactically optional, Everyone gets that. The important thing is that they don’t affect the run-time semantics. This is a surprisingly difficult thing to grasp, because every language that does have mandatory types somehow or another the actual run-time behavior starts to depend on what those types were. Which is very different from say a lambda calculus where the thing works just fine with or without, or works better without, the types. Types don’t change anything. They tell you something might be a problem but they don’t change the reduction rules or anything. So we’re more in that spirit.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;What does it look like? (shows Point example on Dartboard) This is Dart running in the browser. It should be fairly obvious what this thing does. It has instance variables X and Y. It has an overloaded plus operator that let’s you add points. There’s a main function which is the entry point. This is a completely dynamically typed program, you’ll notice that there aren’t any types here. There are constructors that are creating Points, and you can notice some sugar here with immediately taking args and assigning them to the corresponding fields. We can run this program and sure enough it runs. What’s unusual is we can start to put in types here (changes var to num). Nothing has really changed, and now we have a partially typed program. It doesn’t matter how many types we put in, it works the same. These things (the types) are annotations. Our purpose with types is largely documentation for the humans who read the programs and for the tools that inspect the program. People love name completion and various features like that in the development environment, and it’s a lot easier to provide those features if we provide the machine with this information. And we’re not hung up if that particular information is right or wrong, which is the strange thing. So if for example I was to say this (some point) was a string, which is nonsense, it can still do it. (runs program, it works!) You don’t know how quite evil this gets. (changes return type of scale to be string. program runs but generates one warning) Changing the types here doesn’t change the semantics, and that’s on purpose. The idea here is that we have a large body of programmers who haven’t seen a type in their life and are in no hurry to meet one. They should be able to ignore typing all together. On the other hand, we should be able to give people some meaningful messages about errors, name completion, so there’s positive value with these declarations. From my experience, most of the value is in having the documentation as opposed to the common approach of saying it’s great for detecting errors. A good programming environment will detect these errors early on. I don’t generally believe the assertion that there’s a difference in robustness in these languages.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Mandatory Types: pros. So far you’ve seen it’s a very vanilla looking language, except for this strange business of the types that give you messages but don’t actually stop the program from running. &amp;nbsp;The order of importance for mandatory types: machine checkable docs, types provide conceptual framework, early error detection, and performance advantages. Never the less, we’re now at a point where we can get good performance from completely untyped code, and for the kinds of applications we’re seeing on the web, it’s not particularly critical. Early error detection is what everyone talks about, but it’s not entirely crucial. What is crucial is machine checkable docs. Types also give you a useful mental framework. These are the things that are actually important to us.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Mandatory Types: Cons. What’s less talked about is the downsides of types. This usually deteriorates to a religious war of one kind or another. There are things that you find difficult to express with a particular type system. If you build a type system that lets you express that, it’s lovely but the system is some complex that only a few logicians are comfortable dealing with it. There are many people that are quite happy to live in a world that a given type system provides them, but we’re not among those. I can give a few examples that would be handy to do engineering wise that are difficult to express. Another issue is that type languages tend to impose a work flow, they tend to make you work in a certain order. For example, “things are missing, things are undeclared, things are inconsistent and please make them consistent before you proceed.” On the other hand, we find it’s much more fun to work with something that is less constrained. Starting with old “read eval print” loop, to environments where you run something, the debugger pops up, and then you fill in the missing method. You build your program from the outside in. You can write a test, there’s nothing there, it immediately crashes, and it doesn’t immediately force you to go back to the beginning. It lets you incrementally interactively build your program. People who have worked with these environments get addicted to them, and people who haven’t don’t seem to get it. We don’t want a language that imposes a work flow. There is an issue with the birttleness of mandatory typing. There is an overwhelming temptation to rely on it for everything, for security, for optimization, and the experience I had on the Java platform where everything relies on that thin layer of typing and that thing collapses and it doesn’t actually work. That’s a different talk, but the point is that there are downsides to traditional typing.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Optional Types: Can we have our Cacke and Eat it Too? Optional types are a kind of “have your cake and eat it too” approach, at least to some degree. We get the documentation, and we admit that the documentation might not actually be correct because we are no longer necessarily in a position to prove it. We still get the conceptual framework, and we often do get the early error detection (and again not fully guaranteed). Sometimes we can even squeeze from performance from these things because sometimes it easier to prove something given these annotations than to infer them and try to optimize. That’s really very much attenuated and not our focus.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Optional Types Precludes... There are a lot of things you can’t do with optional types, there are certain language constructs you can’t have. For example, type based overloading in Java (what you actually call depends on the static types that you wrote). We can’t decide how things should be initialized based on the type information. For example, you can’t just write “int i” and we can’t initialize it to zero for you. We can’t do typed classes, like Haskell, one of the few examples of a construct that I’d want to have if I had types. Most of the others I’m very happy to dispense with. It is a sort of austere discipline.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;So what’s actually new? So at this point, somebody might ask, “Ok, this is fine, but what’s actually new? Didn’t you do this 18 years ago?” To a degree we did, but not many people paid attention. There’s always a gap between research/startups and hitting the mainstream and we think we’re ready for the mainstream. &lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Type Assertion Support. This is really a type assertion system. We support a mode where we can use these modes to drive assertions in what we call “checked mode” so we can find out dynamically if some of these assertions are being violated. This is a very pragmatic thing, something we didn’t have before. This is really the way to think about this. The other thing is, for various reasons, the type system is deliberately unsound. This of course drives some people to high levels of anger, but they just need to view this as something else. This is not a conventional static type system. &lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Dart Types at Runtime. Checked mode lets you interpret type annotations as adding assertions. T x = o ======&amp;gt; assert(o === null || o is T) This is intended for development, not production because it’s too expensive. Every time you transfer a value, pass a parameter, assign, return from a method, we do this check. This is handy for the developer because we localize the error quickly.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;(goes back to the point example, turns on checked mode in Dartboard, generated a “failed type check”) We’re driving this more by ergonomics than mathematical logic. As a practical matter, we’re not building this as a traditional type system. This is a tool in the service of a programmer and what works heuristically in practice most of the time in a dynamically typed language is what we’re concerned with rather than some abstract notion of mathematical correctness. Which btw you never get out of a type system anyway (you get a very partial notion of correctness). One finds that in these common situations that casting goes on. Remember the main audience is not used to casting, and will not fight this beast.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Not your grandfather’s type system. It’s not a type system at all, rather a static analysis tool based on heuristics, coupled to a type assertion mechanism. &lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;What about a real, sound, type system? You can write a tool that will scream bloody murder about these things, but what you can’t do is stop people from running their programs.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Runtime independent of type system. Traditionally, the kinds of types you can check depend on the types you created, but I find that the opposite which is what your program does depends on the type annotations is perverse. Decoupling the two means we can have the type system evolve independently of the language. If someone adds a new way to check types, we don’t have to rejigger everything because the execution engine doesn’t care. &lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;What about type inference? This question comes up a lot. People are very lazy, they hate typing stuff. They would like the system to do this for them and figure out what the types are. I’m happy for tools to do this, I’m not happy for the language to do this. What types you can infer depend on what types you have, but making the type system that you have depend on what you can infer means your type system won’t be good at certain things. We don’t want to restrict things.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Don’t get boxed in. Execution -&amp;gt; Type checking -&amp;gt; Type inference. Where do you want to live? We’re trying to be very very forgiving.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Interfaces. Interfaces are roughly what you would expect from Java. What’s different is that the type system works exclusively with interfaces. There are no types that tell you something is a particular implementation. Every class implicitly induces an interface, we use this to type check. Interfaces are reified at runtime. We use “is” which is like “instanceof”. You can implement the interface of another class without subclassing it. You may not want its code or implementation, but you want to easily write mocks to test it for example. This is what’s good about OO, IMO, in that you only depend on the externally visible interface, and as long as you can fake that behavior you can produce any kind of thing, whether that’s a proxy or a mock for testing.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Generics. I can’t really give a talk about types without my long and tourtured history with generics. I’ve done generics several times, and maybe eventually we’ll get it right, who knows. (shows a simple generic collection, checking List&amp;lt;String&amp;gt;() is List&amp;lt;Object&amp;gt;, etc) This is one of the places that we and the theologians have parted company, because this is an unsound type system. List of string for our purposes is a List of object. Covarient generics, this is well known to be logically wrong. We can afford to have a rule that isn’t correct, because it’s a dynamic and pointer safe language. That means that in some cases we won’t be able to give you absolutely correct assurances about your program, but it also means we won’t have a programmer scratching his head over “what does contravarience mean?” So yes, a List&amp;lt;String&amp;gt; is a List&amp;lt;Object&amp;gt;, a List&amp;lt;Object&amp;gt; is NOT a List&amp;lt;String&amp;gt;, a List&amp;lt;String&amp;gt; is NOT a List&amp;lt;int&amp;gt;, a List&amp;lt;string is a List, and a List is a List&amp;lt;String&amp;gt;. &amp;nbsp;These last two are interesting. What does it mean? It means that List is a list of Dynamic, which basically means “I don’t care about the type”. By covariance, because Dynamic happens to be a supertype of everything, a List&amp;lt;String&amp;gt; is a List of Dynamic. It isn’t necessarily safe, but we only want to present errors when we are very sure of errors, and only with relatively ground and concrete entities. We don’t want people reasoning about higher order things. So a List&amp;lt;String&amp;gt; is a List. Explain this to a guy on the street. If you can’t explain it at that level you have a problem. The last one is perhaps most puzzling, because you can certainly argue that not every List is a List&amp;lt;String&amp;gt; , but practicality has this nasty habit of introducing. We expect to have a lot of libraries with fully typed interfaces, we think that it’s a good thing, it gives people documentation. &amp;nbsp;However what shall the programmer do when they pass a List without a particular type to a function that require a List&amp;lt;String&amp;gt; ? They will probably find out it requires strings and they will produce a List that contains strings. That’s generics in a nutshell. We’re trying something new.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Optional Types and Reified Types. So you might ask, “you’re reifying these types. we actually have these type parameters for generics represented, so they do seem to affect something being executed. We reify the type arguments to constructors, and we also reify the interfaces, but they are optional. Under the covers we pass this magical Dynamic type and it’s accessed at runtime. BTW I have to emphasize that this is very early in the game. This is by no means a product, this a a very early stage of development and we put it out there for people to comments and complain and experiment and so forth.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Summary: Optional Types. There is a static checker that provides warnings, there are no type errors and it is tuned to be unobtrusive. That is, it doesn’t try to catch every possible error. The types don’t affect the semantics. During development you can view this as a special kind of debugging mode where you can turn on these assertions. &lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;a href="http://youtu.be/-IavVtOE_Fg?t=45m49s"&gt;&lt;span style="background-color: transparent; color: #000099; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;But is it Dynamic?&lt;/span&gt;&lt;/a&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; (question from the audience, unable to decypher) &amp;nbsp;Programming without types is not bad coding. (from audience: I’m not saying you should have types. But if you have types, using them correctly would seem like a wise move.) That’s a wide spread opinion among a lot of smart people. We disagree. (audience: so it’s OK to write bad code.) I don’t think it’s bad code. I think that getting in people’s way when they are developing is a bad idea, because that encourages bad code. (audience: nobody would disagree with that.) Obviously, that’s why we’re doing this. We’re not imposing a workflow on people, we’re not imposing a static discipline that will inherently always means that some things cannot be effectively expressed. It’s a question of what these annotations are. Every practical language has holes in its type system, whether it’s a cast. We are building a system for people that would otherwise use JavaScript build programs gradually, if they feel types are valuable to them, they can use them, but they don’t have to. We’re not in a position to impose it on anyone, and we wouldn’t even if we could. (question from audience: is it really expense to run everything in debug mode?) Yeah. We call it checked mode, and right now it’s way more expensive. But that might change. But remember that there are programs that will run just fine even if the checked mode craps out. (question from audience: what is the intended purpose of these annotations?) The intended purpose is documentation, they give you an idea of what it’s aboute. They give your tools an idea of what it’s about. And yes it’s not a formally verified guarantee. It’s not at all about trying to guarantee correctness. The type checker will give you warning which you may care to ignore because you know something it doesn’t. We don’t prevent you from running your program. Now in a forum like this I expect to get questions like these. We also get a lot of feedback along the lines of “is it dynamic enough? Can I do all the strange wonderfully warped things that I’d like to do in a dynamically types programming language?” For example, noSuchMethod. It’s this catch all that lets you define a trap, and when a method that is not defined is called you will then, rather than throwing an exception which is the default, you have the option of writing a method that will respond to that. It will get information about what was called, what arguments, what name, and you can do all kinds of whacky things. You can for example write forwarders or proxies. Things that are bloody awkward to express in a type system. So we do support this feature because we think it’s enormously useful. There’s also the ability to do reflection, and we don’t have it yet but it will probably materialize in a couple of months at the most. So we definitely are a “dynamic” language. However we want to do it via layering. We don’t like how JavaScript, Python, etc do it. We prefer a layered approach where there is an API to do reflection, introspection.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;a href="http://youtu.be/-IavVtOE_Fg?t=52m44s"&gt;&lt;span style="background-color: transparent; color: #000099; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Some Modest Innovations.&lt;/span&gt;&lt;/a&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; Abstract Data Types without types. Dart has this notion about libraries, it’s a very simple minded notion. A library is a collection of top-level classes, interfaces, and functions. They can import each other, they can refer to each other in a mutually recursive fashion, and they act as units of encapsulation. So essentially, privacy in Dart is something that following the conventions of these communities which they are quite comfortable with, even though you might question them. Privacy is based on names. Naming and privacy are not orthogonal and I can understand the objections to that. Basically if you prefix a name with an underscore, it’s private to the library in which it appears. This has some advantages, it’s context free. Anyone looking at this code can tell it’s private without going to its definition. On the other hand it’s not so pretty that I’m constrained on the naming. But what’s really interesting is how this relates to the interface based types system. (showing a code example on the slide) &lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;a href="http://youtu.be/-IavVtOE_Fg?t=1h7s"&gt;&lt;span style="background-color: transparent; color: #000099; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Factories.&lt;/span&gt;&lt;/a&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; This is kind of nice. Constructors have well known problems. Constructors have failure of abstraction. With a classical constructor you are saying you want a new thing allocated. The problem is you don’t always want a new thing allocated. Maybe you want to look it up in a cache. Maybe you want to allocate it but with a different type. There are design patterns, but then people have to read the books and be more knowledgeable. So the idea is we support factory constructors, which we like to call constructors without tears. It reduces the need for things like dependency injection frameworks, not perhaps entirely. (demos example from Dartboard)&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;a href="http://youtu.be/-IavVtOE_Fg?t=1h3m37s"&gt;&lt;span style="background-color: transparent; color: #000099; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Dart is not Done.&lt;/span&gt;&lt;/a&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; Not by any means. Every known programming construct has been proposed on the mailing list, and most of them we had already looked at. We might do them later, we’ll see how things go. Mixins? Reflection. We have a very low level framework for actors. Looking at higher level things on top of that, like erlang style pattern matching, promise pipelining? We’re experimenting. Should classes nest? Should libraries be first class? Non-nullable types? Metadata? Pluggable types? There’s all sorts of things we might do or not do. We’re trying to get feedback from the community. And that’s about everything I had for you.&lt;/span&gt;&lt;/li&gt;
&lt;li style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Questions from audience (though hard to hear). We don’t want to over innovate. If you want adoption you can’t get too far ahead of where the wider programming public is. People who tie their performance to types have had disappointing results generally with dynamically typed languages. Optimizing performance based on types isn’t very high on the list. &lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-3171894127794614597?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/wexbAtK4zy8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/3171894127794614597/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=3171894127794614597&amp;isPopup=true" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/3171894127794614597?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/3171894127794614597?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/wexbAtK4zy8/transcription-of-quick-tour-of-dart-by.html" title="Transcription of A Quick Tour of Dart by Gilad Bracha" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://img.youtube.com/vi/-IavVtOE_Fg/default.jpg" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://blog.sethladd.com/2011/11/transcription-of-quick-tour-of-dart-by.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEYDRn84cCp7ImA9WhdUEko.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-2943165997817832418</id><published>2011-09-24T02:38:00.002-07:00</published><updated>2011-09-28T23:02:57.138-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-09-28T23:02:57.138-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="games" /><category scheme="http://www.blogger.com/atom/ns#" term="html5" /><title>Mouse Lock for HTML5 FPS games</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ygKR0YlhgeygwQ2jqFtCXXhNkBU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ygKR0YlhgeygwQ2jqFtCXXhNkBU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ygKR0YlhgeygwQ2jqFtCXXhNkBU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ygKR0YlhgeygwQ2jqFtCXXhNkBU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-IOTwoG0gjyE/TnGoz_KcZAI/AAAAAAAAA2A/jcrnAhINo44/s1600/new-spaceship.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-IOTwoG0gjyE/TnGoz_KcZAI/AAAAAAAAA2A/jcrnAhINo44/s1600/new-spaceship.png" /&gt;&lt;/a&gt;&lt;/div&gt;
Sponsor: Register today for &lt;a href="http://www.newgameconf.com/"&gt;New Game, the conference for HTML5 game developers&lt;/a&gt;. Learn from Mozilla, Opera, Google, Spil, Bocoup, Mandreel, Subsonic, Gamesalad, EA, Zynga, and others at this intimate and technically rich conference. Join us for two days of content from developers building HTML5 games today. Nov 1-2, 2011 in San Francisco. &lt;a href="http://newgame.eventbrite.com/"&gt;Register now&lt;/a&gt;!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Good news, everyone! &amp;nbsp;Work is progressing on the Mouse Lock API, a new JavaScript API which will allow for playable "First Person Shooter" (aka FPS) games, and other &amp;nbsp;use cases, for HTML5 games.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.scheib.net/"&gt;Vince Scheib&lt;/a&gt;, Chrome engineer and veteran of the games development industry, has kicked off work back in June 2011 with an &lt;a href="http://lists.w3.org/Archives/Public/public-webapps/2011AprJun/1237.html"&gt;email to the public-webapps list&lt;/a&gt;. &amp;nbsp;A &lt;a href="https://lists.webkit.org/pipermail/webkit-dev/2011-September/017945.html"&gt;recent update from Vince&lt;/a&gt;, sent in Sept 22, 2011, hints at a work in progress implementation for Chrome.&lt;br /&gt;
&lt;br /&gt;
The &lt;a href="https://docs.google.com/a/google.com/document/d/1uV4uDVIe9-8XdVndW8nNGWBfqn9ieeop-5TRfScOG_o/edit?hl=en_US&amp;amp;authkey=CM-dw7QG"&gt;draft specification for Mouse Lock API&lt;/a&gt; is available for review. &amp;nbsp;It is &lt;a href="http://lists.w3.org/Archives/Public/public-webapps/2011JulSep/1424.html"&gt;proposed&lt;/a&gt; that the Web Events Working Group adopt the Mouse Lock spec.&lt;br /&gt;
&lt;br /&gt;
Tracking progress for the spec is easy, with bugs available at &lt;a href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=9557"&gt;W3C bug tracker&lt;/a&gt;, &lt;a href="http://code.google.com/p/chromium/issues/detail?id=72754"&gt;Chromium issue tracker&lt;/a&gt;, &lt;a href="https://bugs.webkit.org/show_bug.cgi?id=68468"&gt;WebKit bugzilla&lt;/a&gt;, and the &lt;a href="https://bugzilla.mozilla.org/show_bug.cgi?id=633602"&gt;Mozilla bugzilla&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
From the draft spec:&lt;br /&gt;
&lt;br /&gt;
"""&lt;br /&gt;
The Mouse Lock API provides for input methods of applications based on the movement of the mouse, not just the absolute position of a cursor. A popular example is that of first person movement controls in three dimensional graphics applications such as games. Movement of the mouse is interpreted for rotation of the view-port, there is no limit to how far movement can go, and no mouse cursor is displayed.&lt;br /&gt;
&lt;br /&gt;
Mouse Lock is related to Mouse Capture. Capture provides continued event delivery to a target element while a mouse is being dragged, but ceases when the mouse button is released. Mouse Lock differs by being persistent, not limited by screen boundaries, sending events regardless of mouse button state, hiding the cursor, and not releasing until an API call or specific release gesture by the user.&lt;br /&gt;
"""&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example code&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;x = document.getElementById("x");
x.addEventListener("click", mouseClick);
x.addEventListener("mousemove", mouseMove);

function mouseClick(e) {
    // Request that mouse be locked. 
    document.lockMouse(x);

    // If successful the mouseMove handler will continue 
    // to be called no matter how the mouse is moved.
    // The cursor will be hidden. No other elements or
    // system applications will be the target of mouse events.
}

function mouseMove(e) { 
    // .movementX/Y are valid normally as the mouse hovers
    // over an element. But when the mouse is locked the
    // mousemove event continues to fire whenever the mouse
    // moves.
    console.log(e.movementX + ", " + e.movementY); 
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
A call to &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;lockMouse&lt;/span&gt; may trigger a user agent to prompt the user for permission.&lt;br /&gt;
&lt;br /&gt;
Both &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;movementX&lt;/span&gt; and &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;movementY&lt;/span&gt; provide the change, or delta, in position of the mouse. &amp;nbsp;For example, &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;movementX&lt;/span&gt; would equal &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;eNow.screenX - ePrevious.screenX&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Summary&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Adding Mouse Capture to the open web platform will enable first class FPS games. Along with WebGL and Fullscreen APIs, we'll see the emergence of a new class of web connected games.&lt;br /&gt;
&lt;br /&gt;
I encourage you to follow along with the issues in the bug trackers and voice your support for Mouse Capture API!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-2943165997817832418?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/Eg-PjTklmvg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/2943165997817832418/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=2943165997817832418&amp;isPopup=true" title="6 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/2943165997817832418?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/2943165997817832418?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/Eg-PjTklmvg/mouse-lock-for-html5-fps-games.html" title="Mouse Lock for HTML5 FPS games" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-IOTwoG0gjyE/TnGoz_KcZAI/AAAAAAAAA2A/jcrnAhINo44/s72-c/new-spaceship.png" height="72" width="72" /><thr:total>6</thr:total><feedburner:origLink>http://blog.sethladd.com/2011/09/mouse-lock-for-html5-fps-games.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkUMSHs-eip7ImA9WhdVE0s.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-4512383750506174553</id><published>2011-09-17T23:13:00.002-07:00</published><updated>2011-09-18T10:51:29.552-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-09-18T10:51:29.552-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="javascript" /><category scheme="http://www.blogger.com/atom/ns#" term="games" /><category scheme="http://www.blogger.com/atom/ns#" term="box2d" /><category scheme="http://www.blogger.com/atom/ns#" term="html5" /><title>Box2D, Collision, Damage, for JavaScript</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/fB_nQts35XRStFFCG212KDmNODU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/fB_nQts35XRStFFCG212KDmNODU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/fB_nQts35XRStFFCG212KDmNODU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/fB_nQts35XRStFFCG212KDmNODU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://newgameconf.com/" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-IOTwoG0gjyE/TnGoz_KcZAI/AAAAAAAAA2A/jcrnAhINo44/s1600/new-spaceship.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;Sponsor:&amp;nbsp;&lt;/b&gt;Register today for&amp;nbsp;&lt;a href="http://newgameconf.com/"&gt;New Game, the conference for HTML5 game developers&lt;/a&gt;. Learn from Mozilla, Opera, Google, Spil, Bocoup, Mandreel, Subsonic, Gamesalad, EA, Zynga, and others at this intimate and technically rich conference. Join us for two days of content from developers building HTML5 games today. Nov 1-2, 2011 in San Francisco.&amp;nbsp;&lt;a href="http://newgameconf.com/"&gt;Register now&lt;/a&gt;!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
After exploring &lt;a href="http://blog.sethladd.com/2011/09/box2d-impulse-and-javascript.html"&gt;Box2D and Impulses&lt;/a&gt;, which can make a body jump and move, we will turn our attention to detecting and dealing with body collisions.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Intro&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Box2D of course knows when and how each body and fixture is contacting other bodies and fixtures, but so far we haven't reacted to these events nor done anything interesting. Luckily, there is an easy way to be notified when two bodies contact each other, when they stop contacting, and even how much impulse is felt by the bodies. We will explore these concepts in this blog post.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Can you feel it?&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
The &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;b2ContactListener&lt;/span&gt; class from Box2D provides the four callbacks you can use to be notified of contact related events. These events are:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;BeginContact&lt;/span&gt; - fired when two fixtures start contacting (aka touching) each other&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;EndContact&lt;/span&gt; - fired when two fixtures cease contact&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;PreSolve&lt;/span&gt; - fired before contact is resolved. you have the opportunity to override the contact here.&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;PostSolve&lt;/span&gt; - fired once the contact is resolved. the event also includes the impulse from the contact.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
Note that all of the above events are fired during the world Step. This means you need to be very careful not to manipulate the world inside of these events, as the Box2D simulation isn't finished for the step, and you don't want to alter the world at this point.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
EndContact can be fired outside of the Step, in the case of a body being removed from the world.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
An example listener follows:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;    var listener = new Box2D.Dynamics.b2ContactListener;
    listener.BeginContact = function(contact) {
        // console.log(contact.GetFixtureA().GetBody().GetUserData());
    }
    listener.EndContact = function(contact) {
        // console.log(contact.GetFixtureA().GetBody().GetUserData());
    }
    listener.PostSolve = function(contact, impulse) {
        
    }
    listener.PreSolve = function(contact, oldManifold) {

    }
    this.world.SetContactListener(listener);
&lt;/code&gt;&lt;/pre&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Each of the events take a contact parameter, which has details of the contact, most of important are &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;GetFixtureA()&lt;/span&gt; and &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;GetFixtureB()&lt;/span&gt; which return the fixtures involved in the contact. From the fixtures you can reference the bodies. This implies that collisions occur between fixtures, not bodies.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;How big was the impact?&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
You might be wondering, "Great, I see I can tell which two fixtures collided, but what was the force at the collision?" Knowing how hard or soft the collision was is important for lots of game logic.&lt;br /&gt;
&lt;br /&gt;
Looking closely, you'll notice that Box2D doesn't hand you the force or impulse inside &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;BeginContact()&lt;/span&gt;. You can of course calculate your own value for the magnitude of the collision, using &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;linearVelocity&lt;/span&gt; and &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;mass&lt;/span&gt;, both found on the body object. We might even do this in a future article.&lt;br /&gt;
&lt;br /&gt;
Notice, though, that the &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;PostSolve()&lt;/span&gt; method includes an impulse object, which includes an array of impulse values for the collision. This value is the easiest way to access the collision's magnitude.&lt;br /&gt;
&lt;br /&gt;
If only things were so easy!&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;PostSolve()&lt;/span&gt; is called seemingly every time a body feels an impulse from another body. If object A hits object B which is connected to object C, then object C will feel the impulse from object A's initial hit (because the impulse "bumps" object B which "bumps" object C). This may or may not be what you want, depending on your game logic.&lt;br /&gt;
&lt;br /&gt;
For example, if you have a ball rolling across the ground, this creates an impulse&amp;nbsp;(albeit tiny)&amp;nbsp;for every frame, and &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;PostSolve()&lt;/span&gt; will be fired every frame. If you have other objects touching the ground, those objects will feel an impulse from the ground (which is "rumbled" by the ball rolling.)&lt;br /&gt;
&lt;br /&gt;
If you are intending to calculate damage to an object from an impact, be very aware the PostSolve() event will be fired a LOT. If you are subtracting damage from an object's total "hit points" during every PostSolve() be sure you understand that your object will incur damage from basically every impulse.&lt;br /&gt;
&lt;br /&gt;
You can of course create initial "hit points" high enough to withstand impulses from everywhere, or dismiss impulses below a threshold. Your needs will vary.&lt;br /&gt;
&lt;br /&gt;
The important point to remember is that &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;PostSolve()&lt;/span&gt; will be a called a LOT.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
For a simple example, I extended a previous example to add support for collisions. The below example will color objects black if they are feeling a contact (i.e. a PostSolve() event is fired) and will apply damage from the impulse.&lt;br /&gt;
&lt;br /&gt;
&lt;iframe height="520" src="http://box2d-javascript-fun.appspot.com/12/index.html" width="680"&gt;&lt;/iframe&gt;
&lt;br /&gt;
&lt;br /&gt;
You can view a &lt;a href="http://box2d-javascript-fun.appspot.com/12/index.html"&gt;stand alone version&lt;/a&gt; and see the &lt;a href="https://github.com/sethladd/box2d-javascript-fun/tree/master/static/12"&gt;source code&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
I added a simple wrapper for the callback registration to my simple Box2D wrapper class:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;bTest.prototype.addContactListener = function(callbacks) {
    var listener = new Box2D.Dynamics.b2ContactListener;
    if (callbacks.BeginContact) listener.BeginContact = function(contact) {
        callbacks.BeginContact(contact.GetFixtureA().GetBody().GetUserData(),
                               contact.GetFixtureB().GetBody().GetUserData());
    }
    if (callbacks.EndContact) listener.EndContact = function(contact) {
        callbacks.EndContact(contact.GetFixtureA().GetBody().GetUserData(),
                             contact.GetFixtureB().GetBody().GetUserData());
    }
    if (callbacks.PostSolve) listener.PostSolve = function(contact, impulse) {
        callbacks.PostSolve(contact.GetFixtureA().GetBody().GetUserData(),
                             contact.GetFixtureB().GetBody().GetUserData(),
                             impulse.normalImpulses[0]);
    }
    this.world.SetContactListener(listener);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
I register a callback in the main page like this:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;      box.addContactListener({
        BeginContact: function(idA, idB) {
        },
        
        PostSolve: function(idA, idB, impulse) {
          if (impulse &amp;lt; 0.1) return; // playing with thresholds
          var entityA = world[idA];
          var entityB = world[idB];
          entityA.hit(impulse, entityB);
          entityB.hit(impulse, entityA);
        }
      });
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Note that the above code is not standard Box2D code, it is a simplification I've created for these demos.&lt;br /&gt;
&lt;br /&gt;
I added a new method to Entity for the collision:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;    Entity.prototype.hit = function(impulse, source) {
      this.isHit = true;
      if (this.strength) {
        this.strength -= impulse;
        if (this.strength &amp;lt;= 0) {
          this.dead = true
        }
      }
      
      //console.log(this.id + ", " + impulse + ", " + source.id + ", " + this.strength);
    }
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
For my purposes, entities may or may not have a strength (aka "hit points"). If my entity runs out of strength, it is "dead" and will be removed from the world:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;    function update(animStart) {
      box.update();
      bodiesState = box.getState();
      
      var graveyard = [];
      
      for (var id in bodiesState) {
        var entity = world[id];
        
        &lt;b&gt;if (entity &amp;amp;&amp;amp; world[id].dead) {
          box.removeBody(id);
          graveyard.push(id);&lt;/b&gt;
        } else if (entity) {
          entity.update(bodiesState[id]);
        }
      }
      
      &lt;b&gt;for (var i = 0; i &amp;lt; graveyard.length; i++) {
        delete world[graveyard[i]];
      }&lt;/b&gt;
    }
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
And my simple Box2D wrapper implements removeBody() as such:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;bTest.prototype.removeBody = function(id) {
    this.world.DestroyBody(this.bodiesMap[id]);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Summary&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
The Box2D &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;b2ContactListener&lt;/span&gt; provides four events for you to be notified when two fixtures contact/collide/touch. The &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;PostSolve()&lt;/span&gt; method includes an impulse object which you can use to calculate damage or otherwise measure the intensity of the collision. However, &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;PostSolve()&lt;/span&gt; is fired whenever a fixture "feels" another fixture, which means it will be fired quite a lot.&lt;br /&gt;
&lt;br /&gt;
If you're at this point in your development, you'll probably notice that you're now involved with game tuning as much as raw coding. That is, with aspects like damage, the "feel" of the game now becomes quite important. It's not enough to figure out how the APIs work, you need to tune and tweak your game with quite a few "magic" values like damages, thresholds, etc.&lt;br /&gt;
&lt;br /&gt;
Case in point is Angry Birds, which seems easy to copy until you start wanting to capture the essence of the game itself. That essence is wrapped up in many different small values that all add up to the "feel" of the game.&lt;br /&gt;
&lt;br /&gt;
In other words... game balance is hard! :)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Next Steps&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
More details on Box2D contacts can be found in the&amp;nbsp;&lt;a href="http://www.box2d.org/manual.html#_Toc258082975"&gt;Box2D manual&lt;/a&gt;. A &lt;a href="http://blog.allanbishop.com/box-2d-2-1a-tutorial-part-6-collision-strength/"&gt;Box2D collision tutorial from Alan Bishop&lt;/a&gt; covers PostSolve() but only deals with an&amp;nbsp;instantaneous&amp;nbsp;collisions and therefore doesn't run into the "PostSolve() gets called more frequently than you might think" issue. The Box2D forums feature a &lt;a href="http://www.box2d.org/forum/viewtopic.php?f=19&amp;amp;t=7065"&gt;post on finding the maximum impulse from a collision&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Please do leave questions and comments below, and continue to let me know what else you'd like to see!&lt;/div&gt;
&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-4512383750506174553?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/ZGwjmYX80rE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/4512383750506174553/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=4512383750506174553&amp;isPopup=true" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/4512383750506174553?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/4512383750506174553?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/ZGwjmYX80rE/box2d-collision-damage-for-javascript.html" title="Box2D, Collision, Damage, for JavaScript" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-IOTwoG0gjyE/TnGoz_KcZAI/AAAAAAAAA2A/jcrnAhINo44/s72-c/new-spaceship.png" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://blog.sethladd.com/2011/09/box2d-collision-damage-for-javascript.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEMDSHk-cCp7ImA9WhdVE08.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-7798478767117957833</id><published>2011-09-15T21:49:00.001-07:00</published><updated>2011-09-17T23:14:39.758-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-09-17T23:14:39.758-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="javascript" /><category scheme="http://www.blogger.com/atom/ns#" term="games" /><category scheme="http://www.blogger.com/atom/ns#" term="box2d" /><category scheme="http://www.blogger.com/atom/ns#" term="html5" /><title>Box2D, Impulse, and JavaScript</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/l23rQTWogligsDSdu_jbI3C0u7I/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/l23rQTWogligsDSdu_jbI3C0u7I/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/l23rQTWogligsDSdu_jbI3C0u7I/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/l23rQTWogligsDSdu_jbI3C0u7I/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://newgameconf.com/" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-IOTwoG0gjyE/TnGoz_KcZAI/AAAAAAAAA2A/jcrnAhINo44/s1600/new-spaceship.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;Sponsor:&amp;nbsp;&lt;/b&gt;Register today for&amp;nbsp;&lt;a href="http://newgameconf.com/"&gt;New Game, the conference for HTML5 game developers&lt;/a&gt;. Learn from Mozilla, Opera, Google, Spil, Bocoup, Mandreel, Subsonic, Gamesalad, EA, Zynga, and others at this intimate and technically rich conference. Join us for two days of content from developers building HTML5 games today. Nov 1-2, 2011 in San Francisco.&amp;nbsp;&lt;a href="http://newgameconf.com/"&gt;Register now&lt;/a&gt;!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Intro&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
So far we have been looking at worlds that interact merely from gravity, or &lt;a href="http://blog.sethladd.com/2011/09/box2d-and-joints-for-javascript.html"&gt;motorized joints&lt;/a&gt;. The demos have been fairly calm. Time to add a bit more excitement with the introduction of Impulses. Using an impulse, we can immediately change a body's velocity.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Adding energy&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Box2D has two seemingly similar ways to inject a little life into your bodies.&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;ApplyImpulse&lt;/span&gt; : immediately change momentum. Think being hit by a bat.&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;ApplyForce&lt;/span&gt; : change momentum over time. Think pushing something.&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
There's a lot of confusion over what the difference is between the two methods. If you need a quick obvious change, use &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;ApplyImpulse&lt;/span&gt;. If you need to slowly change over time, use &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;ApplyForce&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-zhA_2Ha8YUE/TnQVl9jQasI/AAAAAAAAA2o/E-WZJkrd5Tg/s1600/Screen+shot+2011-09-15+at+9.21.01+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="302" src="http://1.bp.blogspot.com/-zhA_2Ha8YUE/TnQVl9jQasI/AAAAAAAAA2o/E-WZJkrd5Tg/s400/Screen+shot+2011-09-15+at+9.21.01+PM.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;iframe height="500" src="http://box2d-javascript-fun.appspot.com/11/index.html" width="680"&gt;&lt;/iframe&gt;
&lt;br /&gt;
&lt;br /&gt;
The demo above has a &lt;a href="http://box2d-javascript-fun.appspot.com/11/index.html"&gt;stand alone version&lt;/a&gt; and the &lt;a href="https://github.com/sethladd/box2d-javascript-fun/tree/master/static/11"&gt;source code&lt;/a&gt; is available.&lt;br /&gt;
&lt;br /&gt;
As you can see, we have the beginnings of an Angry Birds clone. An impulse is applied to the circle which propels it through the air, eventually crashing into innocent blocks.&lt;br /&gt;
&lt;br /&gt;
The object configuration is straight forward:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;    var initialState = [
      {id: "ground", x: ctx.canvas.width / 2 / SCALE, y: ctx.canvas.height / SCALE, halfHeight: 0.5, halfWidth: ctx.canvas.width / SCALE, color: 'yellow'},
      {id: "ball", x: 2, y: ctx.canvas.height / SCALE - 2, radius: 0.5},
      {id: "b1", x:17, y: ctx.canvas.height / SCALE - 1, halfHeight: 2, halfWidth: 0.10},
      {id: "b2", x:17, y: ctx.canvas.height / SCALE - 5, halfHeight: 0.25, halfWidth: 2}
    ];
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Control your impulses&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Both force and impulse require a vector, specifically a &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;b2Vec2&lt;/span&gt;, for direction and magnitude. The following code is an easy way to create an impulse:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;bTest.prototype.applyImpulse = function(bodyId, degrees, power) {
    var body = this.bodiesMap[bodyId];
    body.ApplyImpulse(new b2Vec2(Math.cos(degrees * (Math.PI / 180)) * power,
                                 Math.sin(degrees * (Math.PI / 180)) * power),
                                 body.GetWorldCenter());
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
To compute the vector for the impulse, the direction (in terms of degrees) and power (an arbitrary multiplier provided by me to test different powers) are combined.&lt;br /&gt;
&lt;br /&gt;
The impulse is launched from the center of mass of the circle. The impulse can be launched from any point on the object, which creates a torque on the object.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Summary&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Box2D allows you to add force and impulses to bodies. Adding an impulse is like hitting a body with a hard bat. Adding force over time is like pushing on a body.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Next Up&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Please continue to follow along and learn about &lt;a href="http://blog.sethladd.com/2011/09/box2d-collision-damage-for-javascript.html"&gt;Box2D, Collision detection, and Damage&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Thanks for continuing to follow along. Your comments help, let me know what other topics you're looking for.&lt;/div&gt;
&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-7798478767117957833?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/9ieLSkb4G6U" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/7798478767117957833/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=7798478767117957833&amp;isPopup=true" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/7798478767117957833?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/7798478767117957833?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/9ieLSkb4G6U/box2d-impulse-and-javascript.html" title="Box2D, Impulse, and JavaScript" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-IOTwoG0gjyE/TnGoz_KcZAI/AAAAAAAAA2A/jcrnAhINo44/s72-c/new-spaceship.png" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://blog.sethladd.com/2011/09/box2d-impulse-and-javascript.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkACQnkzeip7ImA9WhdVEUk.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-4396819851211849793</id><published>2011-09-15T14:24:00.001-07:00</published><updated>2011-09-15T21:52:43.782-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-09-15T21:52:43.782-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="javascript" /><category scheme="http://www.blogger.com/atom/ns#" term="games" /><category scheme="http://www.blogger.com/atom/ns#" term="box2d" /><category scheme="http://www.blogger.com/atom/ns#" term="html5" /><title>Box2D and Joints for JavaScript</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/x-yoKlsMoh5j-zexBVUFzBfU7RY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/x-yoKlsMoh5j-zexBVUFzBfU7RY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/x-yoKlsMoh5j-zexBVUFzBfU7RY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/x-yoKlsMoh5j-zexBVUFzBfU7RY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;a href="http://newgameconf.com/" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em; text-align: center;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-IOTwoG0gjyE/TnGoz_KcZAI/AAAAAAAAA2A/jcrnAhINo44/s1600/new-spaceship.png" /&gt;&lt;/a&gt;&lt;b&gt;Sponsor:&amp;nbsp;&lt;/b&gt;Register today for&amp;nbsp;&lt;a href="http://newgameconf.com/"&gt;New Game, the conference for HTML5 game developers&lt;/a&gt;. Learn from Mozilla, Opera, Google, Spil, Bocoup, Mandreel, Subsonic, Gamesalad, EA, Zynga, and others at this intimate and technically rich conference. Join us for two days of content from developers building HTML5 games today. Nov 1-2, 2011 in San Francisco.&amp;nbsp;&lt;a href="http://newgameconf.com/"&gt;Register now&lt;/a&gt;!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Intro&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Previously we looked at &lt;a href="http://blog.sethladd.com/2011/09/box2d-with-complex-and-concave-objects.html"&gt;complex bodies with multiple shapes for Box2D&lt;/a&gt;. Using those techniques, we learned how to build concave bodies by associating two or more shapes to a body.&lt;br /&gt;
&lt;br /&gt;
Another way to build more complex objects is to bind bodies together with joints. Joints constrain bodies together, for example by restricting ranges of motion or affecting motion of one body from another. Box2D supports many different types of joints, including pullings, gears, and ropes.&lt;br /&gt;
&lt;br /&gt;
Box2D 2.1a (the version we've been using for all of these tutorials and demos) supports the following joint definitions:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Distance&lt;/li&gt;
&lt;li&gt;Friction&lt;/li&gt;
&lt;li&gt;Gear&lt;/li&gt;
&lt;li&gt;Line&lt;/li&gt;
&lt;li&gt;Mouse&lt;/li&gt;
&lt;li&gt;Prismatic&lt;/li&gt;
&lt;li&gt;Pulley&lt;/li&gt;
&lt;li&gt;Revolute&lt;/li&gt;
&lt;li&gt;Weld&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
Box2D 2.2 (the latest at the time of this writing, yet not yet ported to JavaScript) also supports:&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;all of the above&lt;/li&gt;
&lt;li&gt;Wheel&lt;/li&gt;
&lt;li&gt;Rope&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
Let's start with a "revolute joint" which sounds like a fancy way of saying a joint that allows two bodies to revolve around a shared point. The below screenshot shows a sloped ground with a simple two wheel object rolling down.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-pNNsyOLkhxM/TnJMF7JZJLI/AAAAAAAAA2E/edVM6xfUI8Q/s1600/Screen+shot+2011-09-15+at+12.02.51+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="302" src="http://3.bp.blogspot.com/-pNNsyOLkhxM/TnJMF7JZJLI/AAAAAAAAA2E/edVM6xfUI8Q/s400/Screen+shot+2011-09-15+at+12.02.51+PM.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
See the below live example:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;iframe height="540" src="http://box2d-javascript-fun.appspot.com/09/index.html" width="680"&gt;&lt;/iframe&gt;
&lt;br /&gt;
&lt;br /&gt;
Visit the &lt;a href="http://box2d-javascript-fun.appspot.com/09/index.html"&gt;stand alone example of wheel joints with Box2D&lt;/a&gt; and check out the &lt;a href="https://github.com/sethladd/box2d-javascript-fun/tree/master/static/09"&gt;source code&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Explanation&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Joints bind two bodies. As with many of Box2D's APIs, it all begins with a definition. For example, let's look at the definition of a Revolute joint:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;    var joint = new b2RevoluteJointDef();
    joint.Initialize(body1, body2, body1.GetWorldCenter());
    this.world.CreateJoint(joint);
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
You can see the joint works with Bodies (not shapes or fixtures). The joint is created by the Box2D world.&lt;br /&gt;
&lt;br /&gt;
Really weird stuff starts to happen when the anchor point for the joint is outside the body. And by really weird, I mean doesn't work. I've used the body.GetWorldCenter() to find the center of the body for the joint to bind to.&lt;br /&gt;
&lt;br /&gt;
Notice that I'm only referencing the the center of one body. This is because a revolute joint specifies a single hinge point, and it assumes that the two bodies are already in the right position.&lt;br /&gt;
&lt;br /&gt;
Revolute joints are quite configurable. Some examples of options for a revolute joint are:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;enableLimit&lt;/span&gt; - whether the joint limits will be active&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;lowerAngle&lt;/span&gt; - angle for the lower limit&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;upperAngle&lt;/span&gt; - angle for the upper limit&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;enableMotor&lt;/span&gt; - whether the joint motor will be active, defaults to false&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;motorSpeed&lt;/span&gt; - the target speed of the joint motor. Positive for counter clockwise, negative for clockwise&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;maxMotorTorque&lt;/span&gt; - the maximum allowable torque the motor can use. A torque too weak won't be able to move the bodies.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;Self powered&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Using the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;motorSpeed&lt;/span&gt; and the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;maxMotorTorque&lt;/span&gt; we can build a self powered car.&lt;/div&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;bTest.prototype.addRevoluteJoint = function(body1Id, body2Id, params) {
    var body1 = this.bodiesMap[body1Id];
    var body2 = this.bodiesMap[body2Id];
    var joint = new b2RevoluteJointDef();
    joint.Initialize(body1, body2, body1.GetWorldCenter());
&lt;b&gt;    if (params &amp;amp;&amp;amp; params.motorSpeed) {
      joint.motorSpeed = params.motorSpeed;
      joint.maxMotorTorque = params.maxMotorTorque;
      joint.enableMotor = true;
    }&lt;/b&gt;
    this.world.CreateJoint(joint);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
To show off this awesome hotrod, we'll flatten the ground and let her rip!&lt;br /&gt;
&lt;br /&gt;
&lt;iframe height="540" src="http://box2d-javascript-fun.appspot.com/10/index.html" width="680"&gt;&lt;/iframe&gt;
&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
You can control the direction of the motor with the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;motorSpeed&lt;/span&gt;. A positive &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;motorSpeed&lt;/span&gt; will travel counter-clockwise and a negative speed will travel clockwise (as seen above). The &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;maxMotorTorque&lt;/span&gt; needs to be high enough to actually move your bodies.&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Summary&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Box2D joints bind two bodies together. Box2D has numerous joints like revolute, distance, and pulleys. Many joints have attributes like motor speeds, upper and lower angle bounds, and torque.&lt;br /&gt;
&lt;br /&gt;
For more information on Box2D joints, the&amp;nbsp;&lt;a href="http://www.box2d.org/manual.html#_Toc258082974"&gt;manual&lt;/a&gt;&amp;nbsp;has examples and explanations of all the joint types. For more information on revolute joints specifically, I found this &lt;a href="http://www.iforce2d.net/b2dtut/joints-revolute"&gt;Box2d Revolute Joints&lt;/a&gt; tutorial helpful.&lt;br /&gt;
&lt;br /&gt;
Next Up&lt;br /&gt;
&lt;br /&gt;
Add some excitement to your &lt;a href="http://blog.sethladd.com/2011/09/box2d-impulse-and-javascript.html"&gt;Box2D simulations with Impulse&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-4396819851211849793?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/JA2AVbw5oQQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/4396819851211849793/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=4396819851211849793&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/4396819851211849793?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/4396819851211849793?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/JA2AVbw5oQQ/box2d-and-joints-for-javascript.html" title="Box2D and Joints for JavaScript" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-IOTwoG0gjyE/TnGoz_KcZAI/AAAAAAAAA2A/jcrnAhINo44/s72-c/new-spaceship.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.sethladd.com/2011/09/box2d-and-joints-for-javascript.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEQNQ3c5cCp7ImA9WhdVEEo.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-740284189848488510</id><published>2011-09-14T17:12:00.002-07:00</published><updated>2011-09-15T00:39:52.928-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-09-15T00:39:52.928-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="javascript" /><category scheme="http://www.blogger.com/atom/ns#" term="games" /><category scheme="http://www.blogger.com/atom/ns#" term="box2d" /><category scheme="http://www.blogger.com/atom/ns#" term="html5" /><title>Box2D with Complex and Concave Objects, for JavaScript</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/HYH9kNsQAzQb8MFaUvmsxm-kww0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/HYH9kNsQAzQb8MFaUvmsxm-kww0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/HYH9kNsQAzQb8MFaUvmsxm-kww0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/HYH9kNsQAzQb8MFaUvmsxm-kww0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://newgameconf.com/" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-IOTwoG0gjyE/TnGoz_KcZAI/AAAAAAAAA2A/jcrnAhINo44/s1600/new-spaceship.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;Sponsor: &lt;/b&gt;Register today for &lt;a href="http://newgameconf.com/"&gt;New Game, the conference for HTML5 game developers&lt;/a&gt;. Learn from Mozilla, Opera, Google, Spil, Bocoup, Mandreel, Subsonic, Gamesalad, EA, Zynga, and others at this intimate and technically rich conference. Join us for two days of content from developers building HTML5 games today. Nov 1-2, 2011 in San Francisco.&amp;nbsp;&lt;a href="http://newgameconf.com/"&gt;Register now&lt;/a&gt;!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Intro&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
Previously in this Box2D for the JavaScript developer series, I covered how to&amp;nbsp;&lt;a href="http://blog.sethladd.com/2011/09/box2d-and-polygons-for-javascript.html"&gt;build and simulate arbitrary polygons&lt;/a&gt;. Not entirely arbitrary, as I mentioned that you can only build convex objects. Turns out I was both right and wrong. Read on to learn how to build more complex and even concave bodies.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
The below screenshot has an example of a complex object. The green square + triangle is also concave.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/--OR8Qw1SUCY/TnE3mtyc5NI/AAAAAAAAA10/qYbnn3gfaEc/s1600/Screen+shot+2011-09-14+at+4.22.55+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="484" id=":current_picnik_image" src="http://2.bp.blogspot.com/--OR8Qw1SUCY/TnE3mtyc5NI/AAAAAAAAA10/qYbnn3gfaEc/s640/Screen+shot+2011-09-14+at+4.22.55+PM.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Below is the live version:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;iframe height="600" src="http://box2d-javascript-fun.appspot.com/08/index.html" width="680"&gt;&lt;/iframe&gt;&lt;br /&gt;
&lt;br /&gt;
The &lt;a href="http://box2d-javascript-fun.appspot.com/08/index.html"&gt;stand alone example&lt;/a&gt; for the above demo is available, as well as the &lt;a href="https://github.com/sethladd/box2d-javascript-fun/tree/master/static/08"&gt;source code&lt;/a&gt; for review.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Explanation&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
I'll start using more exact terminology now that we've covered the basics of getting objects onto the screen and interacting with each other. It's important to understand the specifics now that we're getting more complex.&lt;br /&gt;
&lt;br /&gt;
A Body is the rigid chunk of matter.&lt;br /&gt;
&lt;br /&gt;
A Shape is a geometrical object, such as a polygon, circle, square, etc. A Shape must be convex.&lt;br /&gt;
&lt;br /&gt;
A Fixture binds a shape to a body.&lt;br /&gt;
&lt;br /&gt;
Up to this point, we've had a one-to-one relationship between Body, Shape, and Fixture. &lt;i&gt;Turns out that Box2D Bodies can have multiple shapes!&lt;/i&gt;&amp;nbsp; If you extrapolate what that means, you can see that we can build Bodies with complex shapes.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-YgcF5RBmLd4/TnE_8zCYAkI/AAAAAAAAA14/SErt8eWfW5g/s1600/Screen+shot+2011-09-14+at+4.59.29+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-YgcF5RBmLd4/TnE_8zCYAkI/AAAAAAAAA14/SErt8eWfW5g/s1600/Screen+shot+2011-09-14+at+4.59.29+PM.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Code&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
With this more specific understanding and terminology, let's use it to build complex bodies (see, I used the right term there :)&lt;br /&gt;
&lt;br /&gt;
I've extended our JSON shorthand object definitions to support one &lt;i&gt;or more&lt;/i&gt; polygons for a body. (This is non-standard Box2D code, just something I put together to make it easier to play around.)&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;var initialState = {"0": {id: 0, x: 10, y: 5, radius: 2},
                    "1": {id: 1, x: 5, y: 5, polys: [
                      [{x: 0, y: 0}, {x: 1, y: 0}, {x: 0, y:2}] // triangle
                    ]},
                    "2": {id: 2, x: 9, y: 4, halfHeight: 1.5, halfWidth: 0.9},
                    "3": {id: 3, x: 4.5, y: 3, polys: [
                      [{x: 0, y: -2}, {x: 2, y: 0}, {x: 0, y:2}, {x:-0.5, y: 1.5}] // odd shape
                    ]},
&lt;b&gt;                    "4": {id: 4, x: 10, y: 10, polys: [
                        [{x: -1, y: -1}, {x: 1, y: -1}, {x: 1, y: 1}, {x: -1, y: 1}], // box
                        [{x: 1, y: -1.5}, {x: 2, y: 0}, {x: 1, y: 1.5}]  // arrow
                    ], color: "green"}&lt;/b&gt;
};
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Notice the last definition has two polygon definitions. The first has four points for a box, and the second has three points for a triangle. Just like arbitrary shapes, the points are defined relative to the position of the body as specified by the x,y coordinates.&lt;br /&gt;
&lt;br /&gt;
To construct the Box2D objects from the above definition, I refactored the original code a bit to handle more than one Fixture and Shape per Body.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;bTest.prototype.setBodies = function(bodyEntities) {
    this.bodyDef.type = b2Body.b2_dynamicBody;
    
    for(var id in bodyEntities) {
        var entity = bodyEntities[id];
        
        this.bodyDef.position.x = entity.x;
        this.bodyDef.position.y = entity.y;
        this.bodyDef.userData = entity.id;
        var body = this.world.CreateBody(this.bodyDef);
        
        if (entity.radius) {
            this.fixDef.shape = new b2CircleShape(entity.radius);
            body.CreateFixture(this.fixDef);
        } else if (entity.polys) {
&lt;b&gt;            for (var j = 0; j &amp;lt; entity.polys.length; j++) {
                var points = entity.polys[j];
                var vecs = [];
                for (var i = 0; i &amp;lt; points.length; i++) {
                    var vec = new b2Vec2();
                    vec.Set(points[i].x, points[i].y);
                    vecs[i] = vec;
                }
                this.fixDef.shape = new b2PolygonShape;
                this.fixDef.shape.SetAsArray(vecs, vecs.length);
                body.CreateFixture(this.fixDef);
            }&lt;/b&gt;
        } else {
            this.fixDef.shape = new b2PolygonShape;
            this.fixDef.shape.SetAsBox(entity.halfWidth, entity.halfHeight);
            body.CreateFixture(this.fixDef);
        }
    }
    this.ready = true;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The HTML5 Canvas code didn't change too much. As you can see below, the canvas is translated and rotated once, and then all of the shapes are drawn with a series of &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;moveTo()&lt;/span&gt; and &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;lineTo()&lt;/span&gt; calls.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;PolygonEntity.prototype.draw = function(ctx) {
  ctx.save();
  ctx.translate(this.x * SCALE, this.y * SCALE);
  ctx.rotate(this.angle);
  ctx.translate(-(this.x) * SCALE, -(this.y) * SCALE);
  ctx.fillStyle = this.color;

&lt;b&gt;  for (var i = 0; i &amp;lt; this.polys.length; i++) {
    var points = this.polys[i];
    ctx.beginPath();
    ctx.moveTo((this.x + points[0].x) * SCALE, (this.y + points[0].y) * SCALE);
    for (var j = 1; j &amp;lt; points.length; j++) {
       ctx.lineTo((points[j].x + this.x) * SCALE, (points[j].y + this.y) * SCALE);
    }
    ctx.lineTo((this.x + points[0].x) * SCALE, (this.y + points[0].y) * SCALE);
    ctx.closePath();
    ctx.fill();
    ctx.stroke();
  }&lt;/b&gt;

  ctx.restore();
  
  Entity.prototype.draw.call(this, ctx);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Summary&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Box2D supports bodies with complex and even concave shapes by binding more than one Fixture and Shape to a Body.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Next Up&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Did this help clear up shapes and bodies? What would you like to see next? Please leave comments below and let me know.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-740284189848488510?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/DHAHng8Gihs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/740284189848488510/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=740284189848488510&amp;isPopup=true" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/740284189848488510?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/740284189848488510?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/DHAHng8Gihs/box2d-with-complex-and-concave-objects.html" title="Box2D with Complex and Concave Objects, for JavaScript" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-IOTwoG0gjyE/TnGoz_KcZAI/AAAAAAAAA2A/jcrnAhINo44/s72-c/new-spaceship.png" height="72" width="72" /><thr:total>4</thr:total><feedburner:origLink>http://blog.sethladd.com/2011/09/box2d-with-complex-and-concave-objects.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEMEQ3o_eip7ImA9WhdVEEo.&quot;"><id>tag:blogger.com,1999:blog-8601579266012455634.post-221375296757623583</id><published>2011-09-14T00:53:00.000-07:00</published><updated>2011-09-15T00:40:02.442-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-09-15T00:40:02.442-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="javascript" /><category scheme="http://www.blogger.com/atom/ns#" term="games" /><category scheme="http://www.blogger.com/atom/ns#" term="box2d" /><category scheme="http://www.blogger.com/atom/ns#" term="html5" /><title>Box2D and Polygons for JavaScript</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/_om_Y7oDc0gpj-xJ7x3-qVOvvFw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/_om_Y7oDc0gpj-xJ7x3-qVOvvFw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/_om_Y7oDc0gpj-xJ7x3-qVOvvFw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/_om_Y7oDc0gpj-xJ7x3-qVOvvFw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://newgameconf.com/" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-IOTwoG0gjyE/TnGoz_KcZAI/AAAAAAAAA2A/jcrnAhINo44/s1600/new-spaceship.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;Sponsor:&amp;nbsp;&lt;/b&gt;Register today for&amp;nbsp;&lt;a href="http://newgameconf.com/"&gt;New Game, the conference for HTML5 game developers&lt;/a&gt;. Learn from Mozilla, Opera, Google, Spil, Bocoup, Mandreel, Subsonic, Gamesalad, EA, Zynga, and others at this intimate and technically rich conference. Join us for two days of content from developers building HTML5 games today.&lt;/span&gt;&lt;/b&gt;&amp;nbsp;Nov 1-2, 2011 in San Francisco.&lt;b&gt;&lt;span class="Apple-style-span"&gt;&amp;nbsp;&lt;a href="http://newgameconf.com/"&gt;Register now&lt;/a&gt;!&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Intro&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Welcome back to another installment of Box2D for the JavaScript programmer. We will pause our exploration of running &lt;a href="http://blog.sethladd.com/2011/09/box2d-web-workers-better-performance.html"&gt;Box2D in a Web worker&lt;/a&gt; for a while and get back to the fundamentals. Today we look at Polygons, how to specify them, and how to render them.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;The shape of things&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Box2D supports both circles and squares/rectangles, and we've seen examples of both from our original &lt;a href="http://blog.sethladd.com/2011/08/box2d-orientation-for-javascript.html"&gt;Box2D Orientation&lt;/a&gt; article.&lt;br /&gt;
&lt;br /&gt;
For quick review, specifying a circle requires the radius and the position:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;this.fixDef = new b2FixtureDef;
this.fixDef.density = 1.0;
this.fixDef.friction = 0.5;
this.fixDef.restitution = 0.2;

this.bodyDef = new b2BodyDef;
this.bodyDef.type = b2Body.b2_dynamicBody;
&lt;b&gt;this.fixDef.shape = new b2CircleShape(radius);&lt;/b&gt;

this.bodyDef.position.x = x;
this.bodyDef.position.y = y;
this.world.CreateBody(this.bodyDef).CreateFixture(this.fixDef);
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Remember, the x and y coordinates are using Box2D's meters, and should not be pixels on your canvas.&lt;br /&gt;
&lt;br /&gt;
Specifying a rectangle or square is fairly similar:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;this.fixDef = new b2FixtureDef;
this.fixDef.density = 1.0;
this.fixDef.friction = 0.5;
this.fixDef.restitution = 0.2;

this.bodyDef = new b2BodyDef;
this.bodyDef.type = b2Body.b2_dynamicBody;
&lt;b&gt;this.fixDef.shape = new b2PolygonShape;
this.fixDef.shape.SetAsBox(halfWidth, halfHeight);&lt;/b&gt;

this.bodyDef.position.x = x;
this.bodyDef.position.y = y;
this.world.CreateBody(this.bodyDef).CreateFixture(this.fixDef);
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
You can see the reference to &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;bPolygonShape&lt;/span&gt; which is a hint that Box2D can simulate other types of polygons.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Constraints and definitions&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
What does Box2D mean by Polygon? Referring to the &lt;a href="http://www.box2d.org/manual.html"&gt;manual&lt;/a&gt;:&lt;br /&gt;
&lt;blockquote&gt;
Polygon shapes are solid &lt;b&gt;convex&lt;/b&gt; polygons. A polygon is convex when all line segments connecting two points in the interior do not cross any edge of the polygon. Polygons are solid and &lt;b&gt;never hollow&lt;/b&gt;. A polygon must have &lt;b&gt;3 or more vertices&lt;/b&gt;.
&lt;/blockquote&gt;
See the below illustration for the difference between a convex and concave polygons:&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-VJQFum-Cnzw/Tl7_A8oh3hI/AAAAAAAAAx0/WuWhpOr8vcY/s1600/Screen+shot+2011-08-31+at+8.40.15+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-VJQFum-Cnzw/Tl7_A8oh3hI/AAAAAAAAAx0/WuWhpOr8vcY/s1600/Screen+shot+2011-08-31+at+8.40.15+PM.png" /&gt;&lt;/a&gt;&lt;/div&gt;
You can simulate a concave polygon by binding together more than one convex polygons, and we'll look at an example of that in another article.&lt;br /&gt;
&lt;br /&gt;
Out of the box (pun not intended), Box2D supports polygons with up to eight vertices.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Specification&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Specifying a polygon is harder than specifying a square or rectangle, so kudus to Box2D for providing helper methods for those common cases.&lt;br /&gt;
&lt;br /&gt;
The &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;bodyDef&lt;/span&gt; still has a position, however with a polygon that position is not considered the center of the object. (This is different than the circle or square, so do be aware of this difference.) The position for a polygon is the point from which the vertices are relative to. &amp;nbsp;For example, a vertex of &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;0,0&lt;/span&gt; is the same as the &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;x,y&lt;/span&gt; position of the polygon. A vertex of &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;1,1&lt;/span&gt; is one unit to the right and one unit down from the &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;x,y&lt;/span&gt; position of the object.&lt;br /&gt;
&lt;br /&gt;
The manual will tell you that the vertices must be specified in CCW, or counter-clockwise, order for "right handed coordinate systems." However, as far as I can tell, this is the opposite of the HTML5 Canvas. When specifying your polygon coordinates for HTML5 canvas, you should specify in clockwise order. (please correct in the comments if this is wrong)&lt;br /&gt;
&lt;br /&gt;
The code for a triangle polygon is as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;this.fixDef.density = 1.0;
this.fixDef.friction = 0.5;
this.fixDef.restitution = 0.2;

this.bodyDef = new b2BodyDef;
this.bodyDef.type = b2Body.b2_dynamicBody;
this.fixDef.shape = new b2PolygonShape;

// entity.points == [{x: 0, y: 0}, {x: 1, y: 0}, {x: 0, y:2}]
var points = [];

for (var i = 0; i &amp;lt; entity.points.length; i++) {
    var vec = new b2Vec2();
    vec.Set(entity.points[i].x, entity.points[i].y);
    points[i] = vec;
}

this.fixDef.shape.SetAsArray(points, points.length);

this.bodyDef.position.x = x;
this.bodyDef.position.y = y;
this.world.CreateBody(this.bodyDef).CreateFixture(this.fixDef);
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The follow screenshot shows the triangle (amongst other shapes):&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-QPPdDp7QsIs/TnBXfVgqZFI/AAAAAAAAA1w/CkM4GX__y6w/s1600/Screen+shot+2011-09-14+at+12.19.15+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="486" src="http://3.bp.blogspot.com/-QPPdDp7QsIs/TnBXfVgqZFI/AAAAAAAAA1w/CkM4GX__y6w/s640/Screen+shot+2011-09-14+at+12.19.15+AM.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;Drawing the polygon&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Using HTML5 Canvas we can draw any Box2D polygon:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;PolygonEntity.prototype.draw = function(ctx) {
  ctx.save();
  ctx.translate(this.x * SCALE, this.y * SCALE);
  ctx.rotate(this.angle);
  ctx.translate(-(this.x) * SCALE, -(this.y) * SCALE);
  ctx.fillStyle = 'red';

  ctx.beginPath();
  ctx.moveTo((this.x + this.points[0].x) * SCALE, (this.y + this.points[0].y) * SCALE);
  for (var i = 1; i &amp;lt; this.points.length; i++) {
     ctx.lineTo((this.points[i].x + this.x) * SCALE, (this.points[i].y + this.y) * SCALE);
  }
  ctx.lineTo((this.x + this points[0].x) * SCALE, (this.y + this.points[0].y) * SCALE);
  ctx.closePath();
  ctx.fill();
  ctx.stroke();

  ctx.restore();

  Entity.prototype.draw.call(this, ctx);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Notice how in the above code, we first moveTo() to the position of the object, and then draw lineTo() to the vertices of the object, and then connect the last vertex to the original position.&lt;br /&gt;
&lt;br /&gt;
The only properties of the object that change are the position and the angle, the vertices remain static (as they are essentially offsets from the position).&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Center of mass&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Finding the center of mass for a circle or rectangle in Box2D is easy, as it's just the position (via &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;GetPosition()&lt;/span&gt; on the &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;bodyDef.&lt;/span&gt;) The position for a polygon is &lt;b&gt;not&lt;/b&gt; the center of mass, more like a simple reference point for the vertices of the polygon.&lt;br /&gt;
&lt;br /&gt;
Finding the "center" of a polygon requires &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;bodyDef.GetWorldCenter()&lt;/span&gt;. You can see a yellow dot for the center of mass of each of the objects in the example below.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
The embedded example below displays the triangle, with other objects like a circle, rectangle, and a four sides polygon.&lt;br /&gt;
&lt;br /&gt;
&lt;iframe height="580" src="http://box2d-javascript-fun.appspot.com/07/index.html" width="680"&gt;&amp;amp;amp;lt;p&amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;If you can't see the example, you can't see the iframe&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;lt;/p&amp;amp;amp;gt;&lt;/iframe&gt;
&lt;br /&gt;
&lt;br /&gt;
You can check out the &lt;a href="http://box2d-javascript-fun.appspot.com/07/index.html"&gt;stand alone example for Box2D polygons&lt;/a&gt;. The &lt;a href="https://github.com/sethladd/box2d-javascript-fun/tree/master/static/07"&gt;source code&lt;/a&gt; is also available.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Summary&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Box2D supports circles and squares and rectangles, as well as more complex polygons. The polygons must be convex, have no more than eight vertices, and the vertices must be specified in a specific order (seemingly clockwise for HTML5 canvas).&lt;br /&gt;
&lt;br /&gt;
Next up is how to &lt;a href="http://blog.sethladd.com/2011/09/box2d-with-complex-and-concave-objects.html"&gt;build and render complex bodies with Box2D&lt;/a&gt;, even convex bodies!&lt;br /&gt;
&lt;br /&gt;
Please let me know what you think in the comments below. Thanks!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8601579266012455634-221375296757623583?l=blog.sethladd.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SethLaddsBlog/~4/t7G0at-NDHs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.sethladd.com/feeds/221375296757623583/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8601579266012455634&amp;postID=221375296757623583&amp;isPopup=true" title="5 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/221375296757623583?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8601579266012455634/posts/default/221375296757623583?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SethLaddsBlog/~3/t7G0at-NDHs/box2d-and-polygons-for-javascript.html" title="Box2D and Polygons for JavaScript" /><author><name>Seth Ladd</name><uri>https://profiles.google.com/118397406534237711570</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh6.googleusercontent.com/-_3znrz32MsU/AAAAAAAAAAI/AAAAAAAAAxU/DrczVqiZ1Tw/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-IOTwoG0gjyE/TnGoz_KcZAI/AAAAAAAAA2A/jcrnAhINo44/s72-c/new-spaceship.png" height="72" width="72" /><thr:total>5</thr:total><feedburner:origLink>http://blog.sethladd.com/2011/09/box2d-and-polygons-for-javascript.html</feedburner:origLink></entry></feed>

