Tuesday, February 21, 2012

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:

Recursive list: [1, 2, 3, [...]]

You'll need the very latest source from the bleed_edge branch to see this in action. It won't be long until this lands in trunk, though.

Enjoy!

Post a Comment

Disclaimer

I'm probably required to say that the views expressed in this blog are my own, and do not necessarily reflect those of my employer. Also, except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the BSD License.