Tuesday, August 25, 2009

The 'classify' statement


I've implemented the 'classify' statement in the Tart compiler, which is similar to a switch statement, except that it switches based on the type, rather than the value of an expression.

Here's an example:

let e:Object = "H";
classify e {
  as s:Exception { /* do something with 's' */ }
  as a:int[] { /* do something with 'a' */ }
  else { /* 'e' is some other type. */ }
}
There is also a 'short form' which handles a single type test:
classify e as s:String { /* do something with 's' */ }
The classify statement works with both reference types and union types.