Symbol Literal - I'm probably abusing it

Friday, October 25, 2013

Yesterday suddenly I felt I wanted to implement ancient famous programming stuff, Game of Life's_Game_of_Life.
I had sought something suitable for polymer.dart experience with having fun. Firstly I decided not to use polymer then later on tried to use it in order to compare them. I've enjoyed the implementing (except that I got confused from mysterious error output repeatedly on console about build.dart, still not resolved but works).
You can clone it, then run on Dartium.

Through writing the code, as you can see in index.dart, I use symbol literals like #live, #dead.
Symbol literal is just short version of const Symbol(‘id’) which you may have seen in mirror based codes. Recently the symbol literal have been added to Dartlang, so I felt I wanted to use the new expression instead of String.
It workd as I expected. But when I felt I wanted to use switch statements instead of if-else statements, I stumbled on a error.

From the if-else

    if (_currentState == #dying) {
      _currentState = #dead;
    } else if (_currentState == #aboring) {
      _currentState = #live;
    } else {
      // do nothing

To the switch statements

    switch (_currentState) {
      case #dying:
        _currentState = #dead;
      case #aboring:
        _currentState = #live;

     -> // Error: type class of case expression must not implement operator == case 

I confirmed the error was reproduced in a simple example as below.

  pass() {
    var a = 'a';
    var b = 'b';
    switch (a) {
      case 'a':
      case 'b':
    // -> 'a'
  error() {
    var a = #a;
    var b = #b;
    switch (a) {
      case #a:
      case #b:
    // Error: type class of case expression must not implement operator == case #a:
  main() {

I was curious about it, so I referred dart language specification.
It explains concisely about the use case of symbol -- reflection and minification, also explains the handy syntax becomes addictive. Yes, I did. Should I use String instead of Symbol literal in the case? Probably yes, I think.

Read more ...

How to retrieve metadata using dart:mirror in Dartlang

Wednesday, October 16, 2013

Dartlang provides Metadata feature, which is similar to Annotations in Java(

Dartlang tutorial introduces package:meta and its two members, @deprecated and @overrides (package:meta also provides @proxy).

DartEditor recognise the metadata as shown at above tutorial. The tutorial also explains how to create custom metadata and retrieve it. But there is no code sample on how to retrieve it, so I'm curious about the way.

Through reading some implementations such as @published metadata in Polymer.dart, I find the way. Actually It's easy once I understand the use of dart:mirrors.

Here is the sample code. Pretty simple and easy to read, isn't it?

import 'dart:mirrors';

class todo {
  final String who;
  final String what;

  const todo(this.who, this.what);

@todo('akira', 'add something')
class Foo {
  @todo('naoto', 'do something')
  String fooVariable = 'a';

  @todo('kitano', 'change the name')
  void fooMethod() {


void main() {
  InstanceMirror im = reflect(new Foo());
  ClassMirror classMirror = im.type;
  // ClassMirror
  classMirror.metadata.forEach((metadata) {
    print(metadata.reflectee.who); // -> akira
    print(metadata.reflectee.what); // -> add something

  // VariableMirror   
  for (VariableMirror variable in classMirror.variables.values) {
    print(variable.metadata.first.reflectee.who); // -> naoto
    print(variable.metadata.first.reflectee.what); // -> do something

  // MethodMirror
  classMirror.methods.values.forEach((MethodMirror method) {
    print(method.metadata.first.reflectee.who); // -> kitano
    print(method.metadata.first.reflectee.what); // -> change the name

Read more ...

HTML5 classList API and Dartlang CssClassSet

Sunday, October 13, 2013
JavaScript's classList API is useful. It can manipulate css classes like jQuery's addClass, removeClass, toggleClass.

For instance, you can write as follows (caution, IE version >= 10 required).


Dartlang has similar API named CssClassSet for that manipulation. Element object has classes property, and returns CssClassSet, which also provides add, remove, toggle, etc...


Thinking css class property, Set is proper name. Plus, since it implements Set, it provides more comprehensive functions than classList in JavaScript.
Read more ...