Jump to page: 1 27  
Page
Thread overview
request switch statement with common block
Aug 03, 2013
JS
Aug 03, 2013
Andrej Mitrovic
Aug 03, 2013
MattCodr
Aug 03, 2013
dennis luehring
Aug 03, 2013
Ary Borenszweig
Aug 03, 2013
JS
Aug 03, 2013
w0rp
Aug 03, 2013
Walter Bright
Aug 03, 2013
MattCoder
Aug 03, 2013
JS
Aug 03, 2013
Walter Bright
Aug 03, 2013
JS
Aug 03, 2013
Walter Bright
Aug 03, 2013
bearophile
Aug 04, 2013
monarch_dodra
Aug 04, 2013
Dicebot
Aug 04, 2013
monarch_dodra
Aug 04, 2013
bearophile
Aug 04, 2013
Walter Bright
Aug 04, 2013
Ali Çehreli
Aug 04, 2013
bearophile
Aug 06, 2013
Andre Artus
Aug 10, 2013
H. S. Teoh
Aug 10, 2013
Jonathan M Davis
Aug 10, 2013
H. S. Teoh
Aug 03, 2013
JS
Aug 03, 2013
Andre Artus
Aug 03, 2013
Meta
Aug 03, 2013
JS
Aug 03, 2013
Dicebot
Aug 03, 2013
Walter Bright
Aug 04, 2013
JS
Aug 04, 2013
John Colvin
Aug 04, 2013
Andre Artus
Aug 04, 2013
Kagamin
Aug 04, 2013
Andre Artus
Aug 04, 2013
Andre Artus
Aug 04, 2013
MattCoder
Aug 05, 2013
Andre Artus
Aug 05, 2013
Andre Artus
Aug 05, 2013
luminousone
Aug 05, 2013
Andre Artus
Aug 05, 2013
ron
Aug 05, 2013
Borislav Kosharov
Aug 05, 2013
luminousone
Aug 05, 2013
Andre Artus
Aug 05, 2013
Andre Artus
Aug 05, 2013
luminousone
Aug 05, 2013
Andre Artus
Aug 05, 2013
dennis luehring
Aug 05, 2013
QAston
Aug 05, 2013
Andre Artus
Aug 06, 2013
QAston
Aug 06, 2013
Andre Artus
Aug 05, 2013
Andre Artus
Aug 05, 2013
MattCodr
Aug 05, 2013
Andre Artus
Aug 10, 2013
Jonathan M Davis
Aug 10, 2013
Jonathan M Davis
Aug 10, 2013
H. S. Teoh
Aug 10, 2013
H. S. Teoh
August 03, 2013
switch (cond)
    common: always executed code here
    case A : etc...
    ....
}

instead of

if (cond) { always executed code here  }
switch (cond)
    case A : etc...
    ....
}

which requires modification of the condition twice when necessary
August 03, 2013
On Saturday, 3 August 2013 at 14:38:48 UTC, JS wrote:
>
> switch (cond)
>     common: always executed code here
>     case A : etc...
>     ....
> }
>
> instead of
>
> if (cond) { always executed code here  }
> switch (cond)
>     case A : etc...
>     ....
> }
>
> which requires modification of the condition twice when necessary

Can you give a real-world example, I'm not sure what's requested here?
August 03, 2013
On Saturday, 3 August 2013 at 14:38:48 UTC, JS wrote:
>
> switch (cond)
>     common: always executed code here
>     case A : etc...
>     ....
> }
>
> instead of
>
> if (cond) { always executed code here  }
> switch (cond)
>     case A : etc...
>     ....
> }
>
> which requires modification of the condition twice when necessary

Why don't you just:

switch(cond)
{
  default:
  // YOUR COMMON HERE

  case A:
    ...
    break;

  ...
}
August 03, 2013
Am 03.08.2013 16:38, schrieb JS:
>
> switch (cond)
>       common: always executed code here
>       case A : etc...
>       ....
> }
>
> instead of
>
> if (cond) { always executed code here  }
> switch (cond)
>       case A : etc...
>       ....
> }
>
> which requires modification of the condition twice when necessary
>

switch does get an value not an condition in its scope (the cases are the evaluators)

what is the sense of common in this switch example?

switch(my_enum)
{
  common: printf("common");
  case A: printf("A"); break;
  case B: printf("B"); break;
}

why not write it like...

printf("common");
switch(my_enum)
{
  case A: printf("A"); break;
  case B: printf("B"); break;
}

cases are equal-to-value evaluators - so what is the evaluation of "common"?

i don't get it, and speaking about fall-through principle is common always fired on start, on end or what?

August 03, 2013
On 8/3/13 11:38 AM, JS wrote:
>
> switch (cond)
>      common: always executed code here
>      case A : etc...
>      ....
> }
>
> instead of
>
> if (cond) { always executed code here  }
> switch (cond)
>      case A : etc...
>      ....
> }
>
> which requires modification of the condition twice when necessary

Do you mean this?

switch(cond) {
  case A:
    common_code();
    // something
  case B:
    common_code();
    // something else
}

(common_code() must not be executed if it doesn't hit any switch case)
August 03, 2013
On Saturday, 3 August 2013 at 16:16:24 UTC, Ary Borenszweig wrote:
> On 8/3/13 11:38 AM, JS wrote:
>>
>> switch (cond)
>>     common: always executed code here
>>     case A : etc...
>>     ....
>> }
>>
>> instead of
>>
>> if (cond) { always executed code here  }
>> switch (cond)
>>     case A : etc...
>>     ....
>> }
>>
>> which requires modification of the condition twice when necessary
>
> Do you mean this?
>
> switch(cond) {
>   case A:
>     common_code();
>     // something
>   case B:
>     common_code();
>     // something else
> }
>
> (common_code() must not be executed if it doesn't hit any switch case)

exactly
August 03, 2013
On 8/3/13 10:21 AM, JS wrote:
> On Saturday, 3 August 2013 at 16:16:24 UTC, Ary Borenszweig wrote:
>> On 8/3/13 11:38 AM, JS wrote:
>>>
>>> switch (cond)
>>>     common: always executed code here
>>>     case A : etc...
>>>     ....
>>> }
>>>
>>> instead of
>>>
>>> if (cond) { always executed code here  }
>>> switch (cond)
>>>     case A : etc...
>>>     ....
>>> }
>>>
>>> which requires modification of the condition twice when necessary
>>
>> Do you mean this?
>>
>> switch(cond) {
>>   case A:
>>     common_code();
>>     // something
>>   case B:
>>     common_code();
>>     // something else
>> }
>>
>> (common_code() must not be executed if it doesn't hit any switch case)
>
> exactly

No because your initial rewrite suggested zero is special. But zero has no special meaning to the switch statement. Consider:

switch (cond)
{
common: ...
case 0: ...
...
}


Andrei

August 03, 2013
I can see you saving a little bit of typing with this, but it's
not worth it.
August 03, 2013
On 8/3/2013 10:45 AM, w0rp wrote:
> I can see you saving a little bit of typing with this, but it's
> not worth it.

It would be a very unconventional syntactic form, and my experience with such things is it'll see very, very little use.
August 03, 2013
On Saturday, 3 August 2013 at 18:04:03 UTC, Walter Bright wrote:
> On 8/3/2013 10:45 AM, w0rp wrote:
>> I can see you saving a little bit of typing with this, but it's
>> not worth it.
>
> It would be a very unconventional syntactic form, and my experience with such things is it'll see very, very little use.

I would like to use this topic and ask if it would be possible to extend a feature of D language like in this case related by the author.

Imagine that a user want to put a trigger when "switch statement" match some option, wouldn't be nice if D could do something like this:

switch(x){
   onBeforeMatch = callFoo();

   case n1:
   ...
   case n2:
   ...
}

Where onBeforeMatch could be onAfterMatch.

Matheus.
« First   ‹ Prev
1 2 3 4 5 6 7