"Xinok" <[email protected]> wrote in message news:[email protected]... > While reading "The Right Approach to Exceptions", it gave me this idea. > The user would declare a list of variables of different types as case > statements, and the variable with matches the type of the switch > expression is used. > > I don't know how the syntax should look, this is merely an example: > > void foo(T)(T arg){ > switch(arg){ > case int i: > writeln("Type is int ", i); break; > case float f: > writeln("Type is float ", f); break; > case string s: > writeln("Type is string ", s); break; > default: > writeln("Type is unknown"); break; } > } > > While static if would be more practical for templates, my idea could be > extended to dynamic upcasting of classes as well, which could be applied > to exception handling: > > try{ ... } > catch(Throwable err) switch(err){ > case IOError a: > break; > case OutOfMemoryError b: > break; > default: > throw err; > }
Yea, pattern matching, basically. Great as D is, pattern matching is one thing where Nemerle blows D out of the water: http://nemerle.org/Grok_Variants_and_matching With exceptions in particular, I've often felt that we need some sort of templated catch: try {...} catch(TypeTuple!(ExceptionA, ExceptionB) e) { // Common implementation, but NO need // for that re-throwing abomination you'd // inevitably need if you just caught // a common base type. }
