Type deduction using switch case

Nick Sabalausky a at a.a
Sat Feb 18 17:47:16 PST 2012

"Xinok" <xinok at live.com> wrote in message 
news:yeuigezlevjbhgufwrti at forum.dlang.org...
> 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: 

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.

More information about the Digitalmars-d mailing list