A switch/case too far...

Koroskin Denis 2korden at gmail.com
Tue May 20 05:09:57 PDT 2008


On Tue, 20 May 2008 15:06:38 +0400, Mike James <deepc at fishing.net> 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 :-)

There is a pitfall:
should the compiler check for logical equality (opEqual) or instance  
equality (is)?
"myObj is thisObj" is definately not the same as "myObj == thisObj", but  
how do you choose?

And what about possible side effects? a.opEquals(b) can give different  
result each time being called, and therefor cannot be used in a switch  
statement unless it's marked as pure, which is to be implemented, I hope :)



More information about the Digitalmars-d mailing list