[Proposal]: add "when" variants for return, continue, break and throw statements #8081
Replies: 2 comments 4 replies
-
This isn't any different to the linked discussion. As stated there, you can already write those examples as a single line by omitting the block: if (updateResult != 0) return updateResult;
if (!File.Exists(nupkgPath)) throw new Exception($"Could not download package {id} @ {version} using any input feed");
if (!group.Packages.Any()) continue; |
Beta Was this translation helpful? Give feedback.
-
I think, it would be more interesting and distinct from existing C# features when it would use a pattern (like the is-expression) instead of a generic boolean expressions because it would provide the benefit of avoiding redundant variables and also is not artifically constrained to certain language keywords.
(Skips the statement when acceleration is null.)
It reads more like a mathematical formula where the condition also is put behind the cases. There is a good argument for putting conditions after the behaviour because the behaviour of a function is of central interest when flying over foreign code. There are sufficiently many cases where if-conditions are trivial implementation details and impede the reading flow. But C# has already adopted some of C's syntax fragments and therefore, there is few or no reason to replace it with just another syntax. It should rather add a new way of coding. I could imagine, if C# is ever willing to integrate theorem solving capabilities into it (which I think is part of the future in programming), then the proposed syntax could be useful:
Returns some fixpoint of Foo(x) that is > 3.
Returns the maximum fixpoint of Foo. The keyword does not actually matter, it could also be It could also be useful as unification operator, returning the unification result. Generalized, it works on arbitrary expressions and patterns, even on subexpressions of bigger expressions, and could be concatenated like the ternary operator.
Example, rotate a box if it makes sense:
The benefit is, it doesn't require the reader to read one complex if-condition at once. One can directly see that a Box Rotation is the code behaviour and could choose to skip the nested expressions or read them. I would like it if we could use patterns as alternative type annotations for declarations (inspired from constraint programming).
All of this is likely too radical anyways and does not really fit to the "C" in C#. So, I would see this just as inspiration of what could be possible in language design. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Add "when" variants for return, continue, break and throw statements
I have doubts about the usefulness of these extensions, but I think it makes sense to consider it
#176
#6345
Motivation
Lines like the ones below are very common in the code
Example 1
Example 2
Example 3
The code blocks above can be rewritten with "when" variants without using "if" statement as
Beta Was this translation helpful? Give feedback.
All reactions