C# 8: Switch expressions
11–20 of 124 posts
Re: C# 8: Switch expressions
#12Earlier quoted context omitted.
This might be better titled "changing things for the sake of changing things and appealing to the programming language geeks". The new syntax is not intuitive, not more readable, just a bit more terse.
This actually seems to be somewhat of a trend in C# as of late. Ever since they introduced lambdas, really. While I use them every day, my favorite is still “replacing” String.Format with string literals. Shorter, sure, but less intuitive particularly for people coming from another language. You can do roughly the same thing in PHP with backticks, but most people avoid them for the same reasons. I’m not sure how I fe…
Re: C# 8: Switch expressions
#13Re: C# 8: Switch expressions
#14* I acknowledge my ignorance in thinking that scala was the first one to the table with this.
Re: C# 8: Switch expressions
#15"+" => ((Func )(() => { Log("addition"); return a + b; }))(), This is incredibly ugly. Why couldn’t it be “+” => {stuff}
What he's doing is creating an anonymous function which takes no parameters and returns an int. That's what a Func is, and then he is executing that function, that's the () bit after }))
He could write it nicer as:
"+" => Add(),
Where he defines add as int Add() => a + b;
He's just doing too much inline, 99% of C# developers would not write code as he has done there.Its' the bloggers code style that is the problem here, not the new switch syntax. Although he probably did it to make the example more self contained and terse.
Re: C# 8: Switch expressions
#16"+" => ((Func )(() => { Log("addition"); return a + b; }))(), This is incredibly ugly. Why couldn’t it be “+” => {stuff}
Re: C# 8: Switch expressions
#17Earlier quoted context omitted.
This might be better titled "changing things for the sake of changing things and appealing to the programming language geeks". The new syntax is not intuitive, not more readable, just a bit more terse.
This actually seems to be somewhat of a trend in C# as of late. Ever since they introduced lambdas, really. While I use them every day, my favorite is still “replacing” String.Format with string literals. Shorter, sure, but less intuitive particularly for people coming from another language. You can do roughly the same thing in PHP with backticks, but most people avoid them for the same reasons. I’m not sure how I fe…
Re: C# 8: Switch expressions
#18I'm not sure what this accomplishes over a normal ugly switch.
Re: C# 8: Switch expressions
#19// now you kind of enforced to // handle all values, otherwise // it will not compile I fail to see how it will not compile if all values are not handled. C# switch is not traditional pattern matching, because you don't get compile time feedback. Claiming that is does is disingenuous.
Re: C# 8: Switch expressions
#20Earlier quoted context omitted.
This might be better titled "changing things for the sake of changing things and appealing to the programming language geeks". The new syntax is not intuitive, not more readable, just a bit more terse.
This actually seems to be somewhat of a trend in C# as of late. Ever since they introduced lambdas, really. While I use them every day, my favorite is still “replacing” String.Format with string literals. Shorter, sure, but less intuitive particularly for people coming from another language. You can do roughly the same thing in PHP with backticks, but most people avoid them for the same reasons. I’m not sure how I fe…
For example:
var h = "Hello ";
var w = "world";
var output = String.Format("{1} {2}", h, w);
output = $"{h} {w}";
Would compile fine but throw on the String.Format() line due to the typo.