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 :-)

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 :)

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 :)

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.

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

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.

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