Live data from Hacker News

C# 8: Switch expressions

alexatnet.com

11–20 of 124 posts

Re: C# 8: Switch expressions

#12

Earlier 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…

I think string interpolation is one of the best things they did to C#. String.Format is pretty error prone.

Re: C# 8: Switch expressions

#14
Though I'm sad that a lot of developers dislike scala, I'm certainly glad to see some of it's* good parts being incorporated into current and new languages: Pattern matching to various degrees in Kotlin, now C#, and soon Java 12.

* I acknowledge my ignorance in thinking that scala was the first one to the table with this.

Re: C# 8: Switch expressions

#15
post #8

"+" => ((Func )(() => { Log("addition"); return a + b; }))(), This is incredibly ugly. Why couldn’t it be “+” => {stuff}

That is ugly.

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
post #8

"+" => ((Func )(() => { Log("addition"); return a + b; }))(), This is incredibly ugly. Why couldn’t it be “+” => {stuff}

I couldn't agree more, {} already denotes a block of code in C#. This is more than ugly and will soon become a nightmare for maintaining code.

Re: C# 8: Switch expressions

#17

Earlier 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…

String interpolation is more intuitive than trying to match a placeholder in the string with a list outside of the string.

Re: C# 8: Switch expressions

#19
post #7

// 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.

In TypeScript, this can be achieved using an assertion to the never type. I don't think it exists in C#, though.

Re: C# 8: Switch expressions

#20

Earlier 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…

string interpolation isn't simply a wrapper around String.Format(); it also offers compile time checking which solved a real limitation of String.Format() while offering better performance than concatenation.

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.
Post reply on HN