Addition of Case Statement (similar to switch) #2247
brianoh
started this conversation in
Language design
Replies: 2 comments 1 reply
-
Hello, @brianoh. I am sure you can do it with more people on Discord. Many people are not so active in Github Discussion because they are on Discord. The first example could be like this in Carbon: match(iDay) {
case 1 => { Print("Mon"); }
case 2 => { Print("Tue"); }
case 3 => { Print("Wed"); }
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
@jon Thanks, I'll look at that.
Brian
…On Mon, Oct 3, 2022 at 11:09 PM Jon Ross-Perkins ***@***.***> wrote:
For reference, see pattern matching
<https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/pattern_matching.md>
—
Reply to this email directly, view it on GitHub
<#2247 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAWYANFCCR5UJCKNQQLQGTWBLZLXANCNFSM6AAAAAAQ2IFXPY>
.
You are receiving this because you were mentioned.Message ID:
***@***.***
.com>
|
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
-
Switch and “case” statements
In my opinion, in the interest of brevity and elegance, there should be an alternative to the “switch” statement which works in a similar way, but does not require a “break” statement to stop the evaluation. Obviously, the need for such a construct needs to be evaluated, however, there is also the hidden numbers of people who just get on with what is available and don't push for an "improvement" because of immediate time constraints.
Currently the switch statement requires a “break” to exit the switch construct. The “break” I presume was necessary for some tasks, and I presume that single subject comparison variable was also for performance or was most-needed. I presume also that there are situations where multiple evaluation against the single subject variable is needed in some situations. In any case, it exists.
Personally, I very rarely use the switch statement because IMO it is too verbose. Some others (possibly large in number) also feel that it is too verbose. Obviously the “switch” can be avoided using “if then else” statements which are probably not as elegant in one respect, but are more concise.
Some other languages have a “case” statement that can replace “if then else” in many situations and provide an alternative to the “switch” statement by not requiring a “break”. The case statement is a little more elegant than using “if then else” depending on how it is designed imo. I think that brevity is extremely important but not at the expense of readability. Some implementations of the “case” statement that I’ve encountered do not have a single comparison subject variable and work in a similar way to “if then else”, but are more verbose, but some would say more elegant than "if then else". Consequently the case statements that require “begin case” and do not use a single subject comparison variable are more verbose, less restrictive, but arguably “if then else” statements are better because they are less verbose. I only include that information because it is an alternative design that I don’t think serves a very useful purpose, and I don’t recommend that design. I’ve outlined my possible alternatives below (examples 2 & 4).
For example:
Example 1: One example of “switch” that I encountered on the net:
Example 2: (See also examples in 4) A possible alternative that the compiler could interpret using a “case” construct could be :
Example 3 An example using “if then else”:
Example 4: Another possible alternative that the compiler could interpret using a “case” construct (possibly more compiler-friendly) could be :
In conclusion, I’m firmly of the opinion that there should be an alternative to the “switch” statement that works in a similar way to that outlined in my example (2 or 4) above. The reasons for this are:
Possibly, because of the proposed design of Carbon, an introductory key-word is required for the case evaluations in order to simplify the logic of the compiler and other tools. Example 4 possibly meets that criteria.
Beta Was this translation helpful? Give feedback.
All reactions