Posts

Lazy Load Libraries in Dart

Image
UPDATE: This article is now DEPRECATED, but the feature lives on! Dart now formally supports lazy loading (deferred) libraries. Learn more in the Deferred Loading in Dart article . Dart is a statically analyzable language, and its tree-shaking compiler does a good job of eliminating dead code and producing a single, optimized application file. However, sometimes developers need to control when certain libraries are loaded and thus need to control which libraries are included in the main application file. To help, the dart2js compiler, which converts Dart code into JavaScript code, now supports lazy-loaded libraries. Lazy Load As an example, consider an application that has many different screens. Some screens are more obscure than others, and aren't required for the application to start. A developer should be able to say "I don't need these screens now, but pull them in when I do need them." This lazy-loading is a common deployment strategy for web apps,...

Dynamically Load Code with Dart

Image
Rejoice! Dartium, a build of Chromium with the Dart VM, can now spawn a new isolate from a URI. This means your Dart apps have a new option for more modular code. Isolates In Dart, an isolate is an abstraction for an "isolated memory heap". Isolates do not share memory (variables, statics, etc), they are essentially self-contained applications. Isolates communicate by sending and receiving messages over ports . Why Isolates Matter Dart programs are static, in that their shape and structure do not change after they are compiled. A static program is great for optimizations like tree shaking (aka dead code elimination), type-inferencing compilers, and more. However, modular apps often require a way to dynamically load code. Configurable and modular apps need a way to, at run time, pull in new functionality. If Dart apps have a static structure, how can they dynamically alter their behavior? Isolates to the rescue! The structure inside of an isolate is static, b...

6 Dart FAQs - Answered!

Calvin Prewitt wrote some good questions about Dart via G+. It might be hard to find his questions and my answers, so I hoisted them here. Enjoy! Q) Could you also detail compatibility and which JavaScript APIs are fully or partially supported for Dart2JS production code? A) All JavaScript APIs supported by Chrome should be available to Dart. See http://www.dartlang.org/articles/improving-the-dom/ Q) It appears Dart is aiming to eventually replace it? The FAQ says it only targets modern browser versions. A) Dart's aim is to give developers like you a very productive experience, and give your users a very fast experience. Dart targets IE9+, plus the modern versions of Firefox, Chrome, Safari, mobile safari, and Chrome for Android. Basically, modern browsers. Q) Is there a way to have fine grained control over the JS code like in Closure? A) Dart2js can emit a single file, plus up to one other lazily loaded library. In the future, we want to give you more control. Here's the bu...

First Look at Dart Mixins

Image
You can use mixins to help inject behavior into your classes without using inheritance. Use a mixin when you have shared functionality that doesn't fit well in a is-a  relationship. Dart supports a basic form of mixins as of the M3 release in early 2013. The language designers expect to expand on mixins' abilities in future versions of the language. Surprisingly, you've been using the concept of a mixin all along. Dart considers a mixin as the delta between a subclass and its superclass . That is, every time you define an is-a  relationship with the extends  keyword, you are really defining a delta between the new class and its parent class. In other words, a subclass definition is like a mixin definition. Given that short description, it should come as no surprise that an abstract class (with a few restrictions) is itself a mixin. Here is an example of a Persistance mixin: abstract class Persistence { void save(String filename) { print(...

Dart on FLOSS Weekly from TWiT Network

Image
Today I had the pleasure of chatting about Dart with Randal Schwartz and Aaron Newcomb on FLOSS Weekly  (part of the TWiT network ). We talked about the Dart language, its developer ecosystem, and the future of the project. You can watch the entire video of the interview, embedded below for easy viewing: Thanks to Randal and Aaron for a lively discussion. They are awesome hosts and I highly recommend you tune in. Have Dart questions? Join us on the mailing list or the Dartisans G+ Community . We love feedback!

Solving Boggle with Dart

Image
My friends at work and I have been playing a lot of Scramble with Friends. It's a "tile based word searching game" where you find words on a 4x4 grid of letters. Being a good Dart hacker, I wanted to try writing a small library to find all possible words on the board. Here's how I did it! Dart, not trad. :) First off, props to danvk.org and his helpful posts (such as this one ) on solving Boggle. Old 'n busted My first attend was the naive version. The following code simply tried every possible combination, in a depth-first search. It works, but it's slow. So very slow. class Solver { final Map _words; final List<List<String>> _grid; final List<List> _visited = new List.generate(4, (_) => new List.filled(4, false)); final List _found = new List(); Solver(this._words, this._grid); _solve(int x, int y, [String word = '']) { _visited[x][y] = true; final newWord = '${w...

I Shrunk my Dart-to-JS code by 11X and So Can You

Image
Dart , the structured and scalable programming language for web apps, compiles to JavaScript thanks to dart2js . This means Dart apps run across the modern web. When you compile to JavaScript, you want to ensure dart2js generates the smallest amount of bytes. Less bytes means smaller bandwidth bills, and faster load times, and longer battery life. All good things! Read on to learn how to minify your generated JavaScript. For this example, I will use the offline-enabled Todo app that I built to highlight Lawndart , my easy library for browser storage. The Todo app uses quite a few libraries: dart:html Lawndart WebSQL IndexedDB Local storage Web UI The original generated JavaScript output size was 612,249 bytes. Minified, it became 289,840 bytes. Minified and gzipped it became 56,496 bytes. That's about 11X smaller than the original output! (Measuring the original size of the code is tricky. Do I count the lines that I wrote? Do I count the lines generated by the Web UI...