Posts

Showing posts from February, 2012

Overriding core functions in Dart

An interesting question came up on the Dart mailing list today, asking how to essentially overload a core function, avoiding a name collision. For example, how to handle this? class Printer {                        // print() comes from dart:core   print(String msg) => print(msg); } The dart:core top level function print() is available and defined, however what do you do if you want to name a method print() ? Thanks to Sam McCall , there are a few options: Function consolePrint = print;  class Printer {    print(m) => consolePrint(m);  }  and consolePrint(m) => print(m);  class Printer {    print(m) => consolePrint(m);  }  Thanks for the question and answer about Dart !

IndexedDB in Dart

WARNING : As of 2012-02-27, and after further hacking, there are two blocking issues preventing IndexedDB from working properly. You can't delete an object by a key , and retrieved objects aren't actually objects . IndexedDB is the new way to store key and indexed objects for offline retrieval in web apps. Learn how to use IndexedDB in your Dart application. (Warning: this code isn't pretty, and many bugs were filed. This should get cleaned up soon. Regardless, it works! Also, you'll need the bleeding_edge branch of code, as of 2012-02-23.) Why offline data storage? Use client side local data storage to reduce bandwidth bills, decrease latency, increase UI performance, and allow your application to run offline. Local data storage options There are three options for storing local data for your web apps. Local Storage (aka Web storage) is a very simple key/value store, however it can only store strings and is a blocking/synchronous API. Local Storage is bett...

Pretty print Dart collections

Thanks to a recent commit from Josh Bloch, you can now pretty print your Dart collections. This makes debugging and scripts much easier to write. Example main() {   var list = [1,2,3];   print("List: $list");   var map = {"hello": "world", "1": 1};   print("Map: $map");   var set = new Set();   set.add("foo");   set.add("bar");   print("Set: $set"); } The VM's old 'n busted results: List: [1, 2, 3, ] Map: Instance of 'LinkedHashMapImplementation' Set: Instance of 'HashSetImplementation<Dynamic>' The VM's new hotness results: List: [1, 2, 3] Map: {hello: world, 1: 1} Set: {bar, foo} Recursion That's pretty, but what about something tough? How does it handle recursive references? list.add(list); print("Recursive list: $list"); Old 'n busted: [I never let it stop running, totally busted] New hotness: ...

Enabling type checks for Dart

Intro Dart is an optionally typed language, which means it helps you if you add static types, but doesn't get in the way if you don't. Dart is designed to allow you to scale from simple scripts to large, complex applications, and optional types are a key feature for this vision. Because types are optional, Dart programs must have the same runtime semantics with or without type statements. If the type statements are ignored during runtime, what good are they? Think of type statements more like documentation or annotations for your program. Once added, these types are a terse way for you to express your intent to both your fellow humans and your fellow machines. Using the type annotations to express "x is a number" is a great way to clearly indicate what x is so other developers know how to use it. Compilers can use this annotation to warn you if you do something unnatural with x. Example Consider the following interface: interface Util {   add(a, b); } ...

Classes in Dart, Part One

(This is part 12 in an ongoing series on Dart. See part 11, Booleans in Dart .) Intro Dart is a familiar object oriented programming language, with classes, single inheritance, and interfaces. You can build classes and instantiate objects pretty much like how you would expect, with a few caveats and pleasant surprises. This post is the first in a series of posts that will explore classes in Dart. If you think you need to use classes when programming in Dart, think again. Dart is a scripting language, which implies you can write "short and sweet" scripts and programs with ease. Dart functions very well with only, well, er,  functions, alleviating the requirement to build classes. In fact, I've made it through eleven posts here in this series without talking about classes. You can get far with just functions in Dart. Dart is built to be scalable, helping you as your program grows in scope. You may start with a collection of functions, but as complexity increases, e...

Booleans in Dart

(This is part 11 in an ongoing series about Dart. Read part 10, Numbers in Dart .) Intro 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. Dart has a formal boolean type, named bool . There are only two objects of type bool: true and false . Examples: bool registered = false; var verified    = true; True 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. False 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. Boolean conversion What happens when you write the following code? var name = 'Bob'; if (name) {   print("you have a name!");...

Numbers in Dart

Image
(This is part 10 in an ongoing series about Dart. Check out part 9, a Dart SDK quick tour .) Intro 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. The Basics Like everything else in Dart, numbers are objects. In fact, numbers are specified as interfaces. Numbers come in two flavors in Dart: integers of arbitrary size and decimal 64 bit doubles as specified by the IEEE 754 standard. Both int and double are subinterfaces of num . num is the supertype, int and double extend num. The num interface defines the basics like +, -, /, and *. Num is where you'll also find abs() , ceil() , and floor() , among other methods. Int Integers are numbers without a decimal...