Live data from Hacker News

C# 8: Switch expressions

alexatnet.com

31–40 of 124 posts

Re: C# 8: Switch expressions

#31

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

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?)

Re: C# 8: Switch expressions

#32

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

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

#33

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

> fairly obvious decrease in clarity

You feel that "{0} {1} {2}" provides better clarity than $"{FirstName} {MiddleInitial} {LastName}"?

Re: C# 8: Switch expressions

#34
post #6

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.

Actually, I think it is rather intuitive and readable - but then again I programmed in lisp for years.

For some time, I've thought that the direction of C#'s evolution makes a lot of sense if you look at as the designers' effort to cope with Haskell envy (pattern matching, type inference (which is really just class inference), and monad comprehensions).

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

#35

Earlier 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").

Ironically that’s actually also one of the key advantages of it. If you have a long string with many placeholders in it and need to add a new one in the middle... you just add it and don’t have to figure out where in the list of params to insert the new arg.

Re: C# 8: Switch expressions

#36
post #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.

If you are curious about where it came from look into Caml Light, ML and Miranda.

Re: C# 8: Switch expressions

#37
post #28

This makes sense together with algebraic data types, but I'm not sure if that's implemented.

They’re saving ADTs for 9.0, at which point they’ll beat their chests and loudly proclaim them to be the Best Thing Ever of All Time.

It's OK, it's good direction, so I won't complain.

Re: C# 8: Switch expressions

#38
post #31

Earlier 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?)

the break keyword in switch statements also allows fallthrough, allowing for a terser way to write state machines, at the cost of making the whole construct less intuitive and readable.

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

#39

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

[deleted]

Re: C# 8: Switch expressions

#40
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.

Maybe this type of switch works differently, because there's no good default default case.
Post reply on HN