Proposal: Searched switch statement #8235
Unanswered
wdnijdam
asked this question in
Language Ideas
Replies: 2 comments
-
Beta Was this translation helpful? Give feedback.
0 replies
-
As mentioned in our README, all new proposals need to start as discussions unless a member of the LDM directs you to do otherwise. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Searched switch statement
Summary
Create a more convenient way to write a searched set of conditions resulting in a single result.
Motivation
The current alternatives of writing a searched switch are verbose and do not clearly indicate to the reader that a result is generated from a list of conditions and expressions
Detailed design
A search switch consists of a series conditions with expressions. The expression for first condition met should be returned. I should be clear for the reader that one answer is returned. The conditions are evaluated top to bottom. Conditions after the met condition are not executed.
Proposed syntax:
Drawbacks
Possible confusion with other switch expressions syntaxes. In this case the switch is executed without input object and may not be clear.
Alternatives
Use of current switch with constant input, having additional verbose:
Use of listed if statements. It is not very clear for the reader that al branches contribute to the same result. It is only usable for method results bailing out with if statements. And it is fairly verbose on multiple lines
Using ternary if statements in a cascade. This is hard to read and error prone.
or layout like (may be hard to read for longer conditions and expressions). It is unreadable when longer statements and more conditions are added.
Use of syntax like in TSQL:
Unresolved questions
Design meetings
Beta Was this translation helpful? Give feedback.
All reactions