digitalmars.D - A switch/case too far...
- Mike James <deepc fishing.net> May 20 2008
- "Koroskin Denis" <2korden gmail.com> May 20 2008
- Sean Reque <seanthenewt yahoo.com> May 20 2008
- Gregor Richards <Richards codu.org> May 20 2008
- BCS <ao pathlink.com> May 20 2008
- "Janice Caron" <caron800 googlemail.com> May 20 2008
- janderson <askme me.com> May 21 2008
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 :-)
May 20 2008
On Tue, 20 May 2008 15:06:38 +0400, Mike James <deepc 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 :)
May 20 2008
The simple solution to that would be: case is thisObj: case is thatObj: Makes sense and confuses no one, imho... -[Unknown] Koroskin Denis wrote:On Tue, 20 May 2008 15:06:38 +0400, Mike James <deepc 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 :)
May 20 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.
May 20 2008
DISCLAIMER: This suggestion is not serious. Oh please dear god no.
This would be simple if you could switch on a single-value function
returning bool, and if there was some form of sections ala Haskell, e.g.:
switch (a is) {
case b:
(etc)
}
- Gregor Richards
May 20 2008
Reply to Gregor,DISCLAIMER: This suggestion is not serious. Oh please dear god no. This would be simple if you could switch on a single-value function returning bool, and if there was some form of sections ala Haskell, e.g.: switch (a is) { case b: (etc) } - Gregor Richards
I think you might be able to implement that using recursive templates (like I demonstrated in my D '07 talk) it would look something like this Switch!(Object, bool function(Object a, Object b){return a is b;}, A). Case!(B, {action();}). Case!(C, {something();}). End(); OTOH I think you get an issue with not being able to alias local stuff. p.s. No I'm not serious either.
May 20 2008
2008/5/20 Koroskin Denis <2korden gmail.com>:There is a pitfall: should the compiler check for logical equality (opEqual) or instance equality (is)?
I like the following idea: if the case statement specifies a type, check that the object can be implicitly cast to that type; if the case statement specifies a value, check for equality. So: switch(typeof(a)) { case B1: // RTTI dynamic cast test // ... case B2: // ... } But switch(a) { case a1: // equality test // ... case a2: // ... }
May 20 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 :-)
That could be nice, however I think RTTI checks like that should be avoided. It would be much better to use polymorphism then break abstraction using a switch statement. -Joel
May 21 2008









"Unknown W. Brackets" <unknown simplemachines.org> 