throw inside switch-expression depends on whether return value is used #3624
-
This code works:
However, remove the dummy variable and it no longer compiles:
Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement This with .NET Core 3.1. This seems like a wart. I could understand it if the first example had I think #2632 is related but this issue may be a bit narrower. It doesn't require adding a new kind of switch statement, or supporting general statements like If I might be so bold, I'd like to suggest a general principle: if |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments
-
The switch expression is just that - an expression. What you've written is the same as writing (which also doesn't compile): 5; What does your code do (or would you want it to do if it worked) besides throw an exception if |
Beta Was this translation helpful? Give feedback.
-
True, but C# doesn't normally require you to assign the result of an expression. What about if we've got something like this:
Should they all have to have a common return type that is assigned to a variable? |
Beta Was this translation helpful? Give feedback.
-
You are right that As noted, in this case I want to evaluate the expression only for its side effects. The real-world use is to validate some parameters against a set of patterns. If none of the patterns match, then an exception is thrown. An example would be checking command line parameters ("if you specify --foo-begin then you must also specify --foo-end, but if you give --ignore-baz then neither --foo option is valid, unless --force is also given...."). P.S. and although I could have used the old style switch statement, the new more concise syntax seemed appealing... |
Beta Was this translation helpful? Give feedback.
-
I guess the throw part is a bit of a red herring since I see that switch expressions in general cannot be used throwing away their return value. That seems a bit awkward, but perhaps it was a deliberate choice to allow for adding switch statement expressions later? If so, perhaps a better diagnostic is all that's needed. |
Beta Was this translation helpful? Give feedback.
-
@foxesknow your question about a common return type relates to #2632 I believe. |
Beta Was this translation helpful? Give feedback.
-
You can simply discard the value if you really don't need it: _ = x switch
{
1 => "hi",
2 => "bye",
_ => throw new Exception()
}; |
Beta Was this translation helpful? Give feedback.
-
In C#, as the error message says, only some kinds of expression can be used in a statement. This made sense because originally, these were the only constructs that could have side effects. Other kinds of expression would have no effect (with the possible exception of division by zero). But with the addition of throw expressions to the language, even though they are restricted in where they can appear, we now have many more expressions that have a side effect. I would suggest updating the rule from Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement. to Only assignment, call, increment, decrement, await, and new object expressions, or expressions containing Then in general any side-effecting expression could be used just for its side effects, if the programmer chose to: obviously it won't always be in good taste, but there are cases when it would be handy, pun not intended. |
Beta Was this translation helpful? Give feedback.
-
I'm going to leave this open, but I'll tell you now that this is something we are very unlikely to ever change. C# is very deliberate on what expressions we allow on the top level: you can see the list here. We don't want to have side-effecting expressions or expressions with no value encouraged in the language. |
Beta Was this translation helpful? Give feedback.
-
Now, switch expressions specifically, maybe we'll change. I'm personally not a fan of the top level switch expression proposal, but it is championed and I expect to discuss it for C# 10. |
Beta Was this translation helpful? Give feedback.
Now, switch expressions specifically, maybe we'll change. I'm personally not a fan of the top level switch expression proposal, but it is championed and I expect to discuss it for C# 10.