[Proposal] Allow goto case to jump to any case label not just static constants #1859
Replies: 5 comments
-
I don't understand, this code compiles fine for me: switch (state) {
case SignalState.Strong : {
// run the operations only to be applied to Strong signals
goto case SignalState.Weak;
}
case SignalState.Weak : {
// run operation even applicable to Weak signals
break;
}
case SignalState.FlatLine : {
// report flatline
break;
}
default : {
// handle any unknown state and programmers error eg. by throwing an exception
break;
}
} Can you explain what exactly is the code that didn't work for you and what is the error you got? |
Beta Was this translation helpful? Give feedback.
-
Hi, ...
switch(expression)
{
case string value:
// do something and then jump to another case
goto case int;
case int _:
// do something
break;
case CustomType myValue:
// do something and then jump to another case
goto case CustomBaseType;
break;
case CustomBaseType myBaseValue:
// do something
break;
}
|
Beta Was this translation helpful? Give feedback.
-
How exactly would that work? Consider this code: switch (expression)
{
case string value:
goto case int;
case int i:
Console.WriteLine(i);
break;
} If
You're reading an old version of the spec (it's an unofficial spec for C# 6.0; there is currently no official spec for anything newer than C# 5.0) and attempting to directly apply that to a new version of the language. That's not how it works; when a new version of the language affects an existing construct (like the |
Beta Was this translation helpful? Give feedback.
-
Compiler should determine, because it can, that the current case pattern label type is derived from the type (or have implicit conversion) where you try to jump with
I think about it, but I didn't find anything new in the proposal about |
Beta Was this translation helpful? Give feedback.
-
Sorry for the late response: Hm Strange, could have bet that i read somewhere in the docs (maybe old Which is exactly what i thought I'm the first to propose ;-) Thank you |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi
had the rare case that i needed the case goto in conjunction with enumeration values encoding the strength of a signal. IF the signal only those operation should be done which safely can be applied to the weak signal and when when the signal state is good all operations should be run
Enum SignalState {
FlatLine,
Weak,
Strong
};
As goto case SignalState.Weak;
Is not a valid statement my solution was the following so far
switch ((long)state ) {
case (long)SignalState.Strong : {
// run the operations only to be applied to Strong signals
goto case -1111111111;
}
case (long)SignalState.Weak:
case -1111111111: {
// run operation even applicable to Weak signals
break
}
case (long)SignalState.FlatLine : {
// report flatline
}
default : {
// handle any unknown state and programmers error eg. by throwing an exception
}
}
Therefore i would suggest to introduce the following type of case label of alike
case goto <goto_only_label>:
where the <goto_only_label> can be any static constant accepted by goto case independent of the type of values the switch statement is switching upon and any non whitespace string which would be accepted for normal goto labels.
Its only purpose would be to act as valid target of goto case statement in case the switch statement does not use static constants but Enum, patterns etc. No code besides the one for the mere goto case semantic should be emitted by the frontend and backend compiler.
It must immediately precede or follow another valid case label like
case SignalState.Strong: {
// process strong
case goto CommonWeakAndStrong;
}
case SignalState.Weak:
case goto CommonWeakAndStrong: {
// process
break;
}
or
case SignalState.Strong: {
// process strong
case goto CommonWeakAndStrong;
}
case goto CommonWeakAndStrong:
case SignalState.Weak: {
// process
break;
}
the following would be for example invalid
case SignalState.Strong: {
// process strong
case goto CommonWeakAndStrong;
}
case SignalState.Weak: {
// process weak only
case goto CommonWeakAndStrong;
}
case goto CommonWeakAndStrong: {
// process
break;
}
The difference to the casting solution shown initially would be the value -111111111 is a valid value for (long) even though it is highly unlikely that the SignalState enum will contain a Label representing it, while the case got < goto_only_label> would not be valid outside its switch statement.
Beta Was this translation helpful? Give feedback.
All reactions