A switch/case too far...

Sean Reque seanthenewt at yahoo.com
Tue May 20 12:21:31 PDT 2008


Mike James Wrote:

> I was looking thru some DWT examples and there is plenty of code like this...
> 
> if (myObj is thisObj)
>     // do something
> else if (myObj is thatObj)
>     // do something else
> else ...
> 
> It occured to me how much neater it would be if the there was a switch/case construct that could deal with it..
> 
> switch (myObj) {
>     case thisObj:
>         // do something
>         break;
>     case thatObj:
>         // do something else
>         break;
>     default:
>         // do nothing
> }
> 
> I'm not sure how much compiler 'magic' would be required :-)

This could be done, but as far as I understand  it would in effect do the exact same thing as the if/else statements and would therefore serve only as syntactic sugar.  

There are also other ways to avoid the kind of code above. For instance, you could create a map of objects to function pointers/delegates at runtime and perform a single lookup instead.




More information about the Digitalmars-d mailing list