Showing posts from March, 2012

4 new changes to the Dart language spec

The Dart team published version 0.08 of the Dart language spec , including 15 changes. I've detailed four of the most exciting changes below, some of which you might have seen as early proposals. Not all of these changes are implemented yet, but they show what direction the language and team is moving. Lazily Initialization for static variables This change was proposed  in February 2012. Previously, static class variables and top level variables had to be compile time constants. This kept initialization costs at startup low, but otherwise was constraining to the developer. With this change, static class variables and top level variables will be initialized at first access (lazily) and no longer need to constant. This is a developer friendly change that keeps initialization costs low. Version 0.08 of the spec now reads "Static variable declarations are initialized lazily. The first time a static variable v is read, it is set to the result of evaluating its initial

Dart templates now allow nesting

UPDATE: Work on this library has stopped. You probably want to see Web UI , the fully supported modern client-side library for dynamic, data-driven web apps. Just a few days after we see the first hints at a Dart template library, new features and fixes have landed to make Dart templates even more useful. This is Part 2 of our exploration of Dart templates, read Part One for an introduction to Dart templates . Included in this new commit are: fixed bug with whitespace being removed from text nodes added local names for #with and #each added ability to call another template from within a template Let's take these new features for a spin. Local names Using local names, we can loop through a simple List of Strings.  For example, given the following simple script: #import('dart:html'); #source('hello.dart'); main() {   List fruits = ['apples', 'oranges', 'bananas'];   Hello hello = new Hello("Bob",

JSONP with Dart

A few people have asked how to handle JSONP in Dart. Turns out, this is basically possible, and I'd appreciate feedback on this technique. JSONP is a trick to get around the lack of CORS headers in your favorite API. CORS is the modern way to get around the single origin policy, however even Google still doesn't support CORS on many of their APIs. The web developer community has come up with JSONP as a hack until all browsers and all APIs support CORS. Option 1: If you always deploy to JavaScript This method only works if you are always deploying to JavaScript and are not deploying or testing on Dartium (Chromium with an embedded Dart VM). Also, this feels hacky. Add the JSONP callback to your main page as a small JavaScript method. <script type="text/javascript"> function callbackForJsonpApi(data) {   dartCallback(JSON.stringify(data)); } </script> This simple method converts the data from the server into a big JSON string

First look at Dart's HTML template library

UPDATE: Work on this library has stopped. You probably want to see Polymer  or Angular . Note: this is very very early access stuff. Feedback most welcome! Dart is built to help developers build modern web apps. With today's commit of an HTML template library, that job just got a little bit easier. I take a first look at this new (and still evolving) template library to see what it does and how it works. This week has been full of great stuff in the Dart community. Just a sample: Lars Bak and Kasper Lund join the JavaScript Jabber podcast, support for adjacent string literals lands in the Dart Editor , and the article I co-authored titled What is Dart? was published by O'Reilly. With this new template library, it looks like there is no slowing down. Intro Modern web apps process data and generate display content on the client . The days of full page refreshes and expensive server round trips are gone. Modern web app frameworks usually ship with some sort of

Concatenating string literals in Dart

UPDATE: Dart now supports the + concatenator for string. Dart supports multiple ways to concatenate strings: string interpolation, the + operator, and adjacent string literals. It turns out that  puzzles arise from the using + to concatenate strings, so the Dart team felt compelled to take a fresh approach. Dart already had string interpolation, which allows you to embed a string within a string: // String interpolation in Dart String to = 'Bob'; String msg = "Hello $to";  // Hello Bob Dart also has multi-line Strings, using triple quotes: String htmlTemplate = """ <div>   <p>     Hello $to.   </p> </div>"""; Sometimes, however, you need to deal with very long strings. You can use the + operator to concat long strings, or use adjacent string literals. // This works String longMessage = 'This is what you used to do in Dart. ' +                      'You would use the + operator &#

Using Futures in Dart for Better Async Code

(Updated on March 9th, 2015.) Dart bundles lots of functionality into its standard libraries, helping developers avoid reinventing the wheel from project to project. One of those wheels is a better way to handle asynchronous callback driven programs. Thanks to the Future class, potentially hard to follow callback code can be replaced with a more structured design. (This is part 13 of an ongoing series about Dart.) Intro Let's pretend I have a set of potentially expensive methods that should be executed in order. Ideally, I should be able to write code like: // Yikes! This will lock the page by running too // many long processes in the main UI thread. {   costlyQuery();   expensiveWork();   lengthyComputation();   print("done!"); }); Unfortunately, code like the above locks the main thread, freezing the application. Bummer. Using callbacks is a typical way to make my UI responsive and move expensive processes

Dart's warnings, errors, and checked and production modes

The Dart team talks about "checked" and "production" mode. Dart's systems can generate warnings and exceptions. I wanted to wrap my head about the two different modes and the two different kinds of feedback, so I wrote this post to gather my thoughts. Dart uses both warnings and errors to signal detected problems. Dart detects problems both during static compilation (or analysis) and during dynamic execution. Warnings and Errors A warning does not halt execution. It seems obvious, but look for the word "warning". That tells you a potential problem has been detected. The program will still be compiled, and, if you're running the Dart VM, will be executed. An error , on the other hand, throws an execution. If you do not catch the exception, it will halt your program. Dart detects some type assignment problems during compilation time, if given enough information. For example, the following code uses static types: num add(num x, num y) =&