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.
Using switch as an expression makes sense, just like using if as an expression (often written as a?b:c) does. It makes it obvious that all you are doing is changing one variable, and it makes initialising constants a lot cleaner. Once you start making switch expressions, the break keyword makes no sense. And with pattern matching the case keyword technically wouldn't be nessesary for normal switch statements already,…
C# 8: Switch expressions
31–40 of 124 posts
Re: C# 8: Switch expressions
#32Earlier quoted context omitted.
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
#33Earlier quoted context omitted.
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.
I wasn’t necessarily saying it doesn’t offer any benefit, more that it’s not really a benefit that seems to benefit a lot of people. Between code intelligence tools and actually running the code you write (not even in an actual QA process, but simply executing each line) that just doesn’t seem like a big deal to me. There are also instances where, such as with Console.Write you are now formatting a string in order to…
You feel that "{0} {1} {2}" provides better clarity than $"{FirstName} {MiddleInitial} {LastName}"?
Re: C# 8: Switch expressions
#34Earlier 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.
Actually, I think it is rather intuitive and readable - but then again I programmed in lisp for years.
But, it makes almost complete sense if you instead look at it as coping with Lisp envy. I'm still amazed that they managed to sneak in function literals and FEXPRs (Linq), a metaobject system (dynamic + DLR), a limited form of call/cc (aysnc/await), first-class metaprogramming (Roslyn -- albeit more in the style of Smalltalk rather than Lisp), and a REPL.
Re: C# 8: Switch expressions
#35Earlier quoted context omitted.
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.Format("Is {0} to read {2} {1} of the {3} out of place", "harder", "half", "because", "string is").
Re: C# 8: Switch expressions
#36Though 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
#37Re: C# 8: Switch expressions
#38Earlier quoted context omitted.
Using switch as an expression makes sense, just like using if as an expression (often written as a?b:c) does. It makes it obvious that all you are doing is changing one variable, and it makes initialising constants a lot cleaner. Once you start making switch expressions, the break keyword makes no sense. And with pattern matching the case keyword technically wouldn't be nessesary for normal switch statements already,…
The break keyword in switch statements makes sense only if the programmer considers switch as a syntax sugar for goto, which are the semantics in C. Once you have a language that doesn't use goto but that still requires breaking each switch case, the only rationale is inertia for people familiar with C. (That said, C# does have goto, right?)
In a switch expression of course that is moot since break is a statement, and statements can't appear in expressions.
Re: C# 8: Switch expressions
#39Earlier quoted context omitted.
String interpolation is more intuitive than trying to match a placeholder in the string with a list outside of the string.
I suppose that depends upon where you’re coming from. Many, many languages have an sprintf-style construct of one form or another, so I think it’s actually much more familiar to most people to have a placeholder with an optional formatter. To each their own, though.
Re: C# 8: Switch expressions
#40// 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.