-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Block Bodied Switch Expression Branches #2636
Comments
I agree, but I suggested it as the default anyways because lambda expressions have optional block bodies and the use of the |
I provided |
I personally, strongly dislike this proposal and I think your example code highlights why I don't like it. What ought to be an expression is suddenly turned into reams of procedural code. Using sequence expressions, as @HaloFour highlights, we could write it as: string description = Console.ReadLine() switch {
"" => "Empty",
"/stop" => (stop = true, "A Process Stop Command"),
"/reset" => (Descriptions = new List<(string, string)> { }, "Interpreter Reset Request"),
var input => ProcessInput(input),
_ => "Errored"
);
AddToLogFile($"Input was successfully described as {description}.");
Console.WriteLine($"Input Description: {description}");
string ProcessInput(string input)
{
var result = "";
foreach ((string describedInput, string description) in Descriptions)
{
if (input.Equals(describedInput)) return description;
}
Console.WriteLine("Text Unrecognized; Please Input a Description: ");
var newDescription = Console.ReadLine();
Descriptions.Add((input, newDescription));
return newDescription;
} but even that looks ugly in my view, with those mutating variables spread throughout the expression, but that then becomes a concern over sequence expressions, which is a different topic. |
While I agree that having some sort of way to chain expressions in the way you have demoed would be nice, what I am suggesting is that certain syntax be implemented where the definition of a new local method, as you have done, would not be necessary to simply execute a body of statements in a |
I have also thought of a new syntax that could be used where an expression body is attached to the end of the block. string description = Console.ReadLine() switch
{
"" => "Empty",
"/stop" => stop = true => "A Process Stop Command",
"/reset" => Descriptions = new List<(string, string)> { } => "Interpreter Reset Request",
var input =>
{
var result = "";
foreach ((string describedInput, string description) in Descriptions)
{
if (input.Equals(describedInput))
return description;
}
Console.WriteLine("Text Unrecognized; Please Input a Description: ");
var newDescription = Console.ReadLine();
Descriptions.Add((input, newDescription));
} => newDescription,
_ => "Errored"
} |
Closing as championed at #3038 |
Actually championed at #3037, not 3038 |
This proposal asks for the ability to define a block body for individual
switch expression
branches. I believe this should be a fairly straightforward addition, considering that there is no other obvious way to execute multiple statements in a branch, minus a few roundabout ways such as defining a local function and calling it or instantiating aFunc<T>
that is invoked immediately.Syntax Proposal
Note that example code is trivial.
Instead of
return
, in order to clarify that the statement does not terminate control flow for the member, a new keyword could be introduced to represent the submission of a result from a branch such assubmit
.Implementation
A PoC could probably simply implement this by instantiating a
Func<T>
via a rewrite, but I am unsure as to how feasible it would be to do. Eventually, I don't see why this couldn't just be evaluated like any other block body such as one denoted via{ }
in a member body, with the return simply submitting the result for the branch as it is currently done. This may be easier said than done.What are your thoughts?
The text was updated successfully, but these errors were encountered: