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;
}

Reply via email to