Pattern Matching (or Type-like switch statement) implementation

From http://stackoverflow.com/questions/156467/switch-pattern-matching-idea/7301884#7301884