Posts

Simple Dart + WebSocket demo

[EDIT: Updated on 2013-03-05] I just posted a simple demo of Dart with WebSockets . Not only does it show how to connect to a WebSocket server and send messages, it also shows off a few neat features of the Dart language . (The code follows below, or check out the full  open source project .) Look mom, no classes! Dart is a class-based object oriented language, but that doesn't mean you are required to wrap everything in a class. No sir, this is Dart, not Java. In Dart, you can use top level functions and top level variables. Libraries for modularity Dart organizes code into libraries for modularity. For example, this code uses the dart:html library for the WebSocket code. Concerned about bloat here? Don't be! Thanks to tree-shaking, the code that you don't use is eliminated during a (optional) compile step. The Dart to JavaScript compiler uses tree shaking to help generate minimal JavaScript code (still more work to do here). An upcoming Dart to Dart script wi...

How we built the new Dart homepage

Image
I work with the Dart team, and we needed to update our dartlang.org site. It was a fun project to rebuild dartlang.org , so I wanted to give a shout-out to some of the libraries and tools that we used. The new dartlang.org Requirements for the new dartlang.org included: work on multiple screen sizes display more obvious calls to action load faster clean up CSS display more info on the home page To achieve these goals, we used the following tools: Bootstrap  CSS and widgets Jekyll  (and plugins) site generator Markdown with kramdown  text file formatting Pygments syntax highlighter Compass CSS framework Bootstrap + Compass for SCSS Font Awesome for sprite icons Make build tool optipng image shrinker App Engine hosting Bootstrap , from Twitter, is a slick set of CSS styles and simple JavaScript plugins. Bootstrap has a grid system, pre-defined styles, and most importantly a sane set of defaults for adaptive layouts. Jekyll is a st...

Slides from Dart talk at O'Reilly's FluentConf JavaScript conference

The fine folks at FluentConf included a Dart session in their first JavaScript conference. I had the pleasure of presenting "Structured web programming with Dart", an introduction to Dart's motivations, language, libraries, tools, and ecosystem. The room was packed, which was quite encouraging! I learned that Dart's optional static type annotations are very interesting to JavaScript developers. They especially appreciated the optional part. That is, in Dart you can experiment with simple functions and scripts as you start your idea, and grow over time to full complex modern web apps that use more structure like classes, interfaces, and libraries. Once your design emerges and you are comfortable with the structure, or once you enter maintenance mode, you can add the type annotations. Once those type annotations are in your code, the magic turns on. Type annotations provide inline documentation for your fellow developers. The toolchain can also read the type annot...

My interview with O'Reilly on Dart and doing better.

Image
I had the pleasure of being interviewed by Simon St. Laurent on the topics of Dart and how we can all strive to do better. Thanks Simon! The orignal post has the chapter markers and topics if you want to skip around.

Misleading strong type systems

This post is a record of a Dart Misc mailing list post  titled "Types are useless (?)". The original author was curious why Darts "allows running code with invalid arguments".  My reply follows: --- For reference, let's look at Java. Many would think that Java has a "strong type system" and that its types prevent, as you say, invalid arguments. However, it's easy to write a Java program that will compile just fine, yet fail at runtime. In your words, it will allow running code with invalid arguments. Here's an example: import java.util.*; public class hacked {   public static void test(int i) {     System.out.println(i);   }   public static void main(String[] args) {     String msg = "hacked";     Map cache = new HashMap();     cache.put("hacked", msg);     // later     int hacked = (Integer) cache.get("hacked");     test(hacked);   } } This code will comp...

When Dart types aren't optional

Dart, the structured web programming language, has a feature typically called "optional types". Dart claims that "type annotations don't affect the runtime semantics of the code". This post helps to explain when a type is and isn't optional, and what "type annotation" means as different than "type". The Dart discussion mailing list kicked off a good thread about generics , which evolved into when types can be optional. The Dart community chimed in with great answers (thanks to Ladislav, Sam, John, Dirk, Ross, Bob, and Eli!). Bob Nystrom summed it up well, with some examples that I wanted to specifically highlight: Bob writes: --- Types aren't always annotations. There are type annotations, but there are also places where types appear in code that are not annotations. For example: // type annotations:  int i; foo( int i) => ... class Foo {   int bar;   int baz() => ... }  // types that are not annotation...

Dart Crypto early access

The Dart project just saw its first crypto libraries land , specifically for SHA1 and SHA256. Also appearing is HMAC support. Learn how to use these very new libraries in this post. This new functionality is very new. You'll need to pull the latest from the bleeding_edge branch, as of 2012-04-30. It's so new, it's not even wired into the dart: library scheme, nor is it in the SDK yet. I expect crypto libs to get the full SDK treatment very soon. Here's an example of how to create a SHA256 hash and convert to a hex string: #import('../dart/lib/crypto/crypto.dart'); // Want this in the crypto lib? Star this bug: http://code.google.com/p/dart/issues/detail?id=2839 String digestToString(List<int> digest) {   var buf = new StringBuffer();   for (var part in digest) {     buf.add("${(part < 16) ? "0" : ""}${part.toRadixString(16).toLowerCase()}");   }   return buf.toString(); } main() {   var sha = new SHA256()...