Thread overview | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 03, 2013 request switch statement with common block | ||||
---|---|---|---|---|
| ||||
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 Re: request switch statement with common block | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | 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 Re: request switch statement with common block | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | 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 Re: request switch statement with common block | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | 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 Re: request switch statement with common block | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | 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 Re: request switch statement with common block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | 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 Re: request switch statement with common block | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | 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 Re: request switch statement with common block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | I can see you saving a little bit of typing with this, but it's not worth it. |
August 03, 2013 Re: request switch statement with common block | ||||
---|---|---|---|---|
| ||||
Posted in reply to w0rp | 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 Re: request switch statement with common block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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.
|
Copyright © 1999-2021 by the D Language Foundation