Thread overview
Switch statement operator overload
Dec 16, 2003
J Anderson
Dec 16, 2003
Ilya Minkov
Dec 16, 2003
J Anderson
Dec 16, 2003
J Anderson
Dec 16, 2003
J Anderson
Dec 18, 2003
Matthew Wilson
December 16, 2003
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.

December 16, 2003
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

December 16, 2003
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
>
Does it. That's a good idea!

December 16, 2003
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
>>
> Does it. That's a good idea!
>
Actually I was just looking through phobos.  Switch.d seems to do something like I originally suggested.

December 16, 2003
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
>>>
>> Does it. That's a good idea!
>>
> Actually I was just looking through phobos.  Switch.d seems to do something like I originally suggested.
>
Does it. That's a good idea!
Should have been (just realised that I could be misinterpreted):
Does it? That's a good idea!

December 18, 2003
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
> >>>
> >> Does it. That's a good idea!
> >>
> > Actually I was just looking through phobos.  Switch.d seems to do something like I originally suggested.
> >
> Does it. That's a good idea!
> Should have been (just realised that I could be misinterpreted):
> Does it? That's a good idea!
>