www.digitalmars.com         C & C++   DMDScript  

D - Switch statement operator overload

reply J Anderson <REMOVEanderson badmama.com.au> writes:
This is probably a silly idea (I can't think of a practical 
application), but I'll post it anyway for comment.

What about the ability to override the switch statement for an object, 
kinda like opApply for foreach.

class Obj
{

    int opSwitch(Obj cmpVal, Obj [] cases)
    {
        ...
    }

}

...

Obj A = new Obj;
Obj B = new Obj;
Obj C = new Obj;
switch (A)
{
case B:
//1
break;
case C:
//2
break;
default:
//3
}


would be like typing:

switch (opSwitch(A, {A, B}))
{
case 0:
//1
break;
case 1:
//2
break;
case -1: //default
//3
break;
}


Alternative, more powerful syntax for opSwitch could be:

void opSwitch(Obj cmpVal, Obj [] cases, Obj delegate() [] solution)
{

}

Where solution is a delegate that points to the cases statement that would run
when it is called.
It might be useful, if there user wishes to define how a particular case
determines an answer 
(ie what search algorithm), or for fuzzy cases (ie going for the most-likely
best choice).

It's probably a dumb idea, but I'm just putting it up for discussion anyways.
Dec 16 2003
parent reply Ilya Minkov <minkov cs.tum.edu> writes:
J Anderson wrote:
 This is probably a silly idea (I can't think of a practical 
 application), but I'll post it anyway for comment.

Object has a predefined method to covert itself to hash. I think this should work with switch then. -eye
Dec 16 2003
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Ilya Minkov wrote:

 J Anderson wrote:

 This is probably a silly idea (I can't think of a practical 
 application), but I'll post it anyway for comment.

Object has a predefined method to covert itself to hash. I think this should work with switch then. -eye

Dec 16 2003
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
J Anderson wrote:

 Ilya Minkov wrote:

 J Anderson wrote:

 This is probably a silly idea (I can't think of a practical 
 application), but I'll post it anyway for comment.

Object has a predefined method to covert itself to hash. I think this should work with switch then. -eye


something like I originally suggested.
Dec 16 2003
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
J Anderson wrote:

 J Anderson wrote:

 Ilya Minkov wrote:

 J Anderson wrote:

 This is probably a silly idea (I can't think of a practical 
 application), but I'll post it anyway for comment.

Object has a predefined method to covert itself to hash. I think this should work with switch then. -eye


something like I originally suggested.

Should have been (just realised that I could be misinterpreted): Does it? That's a good idea!
Dec 16 2003
parent "Matthew Wilson" <matthew.hat stlsoft.dot.org> writes:
A man after my own heart. Obsessed with the minutiae. <G>

Matthew

P.S. btw, I think your idea is appealing, if only because it shows how other
"built-in" operations might be given some under-the-covers tailorability

"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:brnjq9$1mja$1 digitaldaemon.com...
 J Anderson wrote:

 J Anderson wrote:

 Ilya Minkov wrote:

 J Anderson wrote:

 This is probably a silly idea (I can't think of a practical
 application), but I'll post it anyway for comment.

Object has a predefined method to covert itself to hash. I think this should work with switch then. -eye


something like I originally suggested.

Should have been (just realised that I could be misinterpreted): Does it? That's a good idea!

Dec 17 2003