Live data from Hacker News

C# 8: Switch expressions

alexatnet.com

81–90 of 124 posts

Re: C# 8: Switch expressions

#81
post #44

Earlier quoted context omitted.

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.

Well, let's take the TIOBE index as a rough indicator of popularity: https://www.tiobe.com/tiobe-index/ Java, C/C++/Objective-C/Go (I'm lumping them up because they're basically C + C NextGens), Matlab, SQL, ASM don't have them. Everything else, has them. On the list of languages that don't have it, Java is planning to add it, I don't see the C family to change because it's very conservative, and the others are domai…

C++ has string streams which actually work more like string interpolation than like sprintf just without adding custom syntax.

Re: C# 8: Switch expressions

#82
post #45

Earlier quoted context omitted.

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 (Li…

I don't think it's Lisp envy. They're porting stuff from F#, which as far as I know is from the ML family.

LINQ actually came from Haskell.

"Confessions of a Used Programming Language Salesman, Getting the Masses Hooked on Haskell"

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.72....

Re: C# 8: Switch expressions

#83

I am not sure I like the path c# is going down. For years the language has been trying to evolve from being aesthetic and easy to understand and read. With the functional implementations and patterns I believe the language is trying to embrace too much. This is not specific to the swith expression in this context but in general. You can write C# in too many forms and I don't believe that's really good.

I think I agree. I used to write a lot of C# v2 and loved it. There didn't seem to be anything 'missing'. Each version since then has added nice things but are they really necessary? Now the language looks huge and complicated and because of that, I'm not sure I'd ever want to use it again.

C# 2 did not have Linq. Linq is the best feature ever.

Re: C# 8: Switch expressions

#84
post #2

This might be better titled "C# 8: switch expressions" (edit: it was previously "statement", as the author's post is titled). Or actually, to bait a few more hn clicks (and provide a fuller description): "Pattern matching in C# 8 with switch expressions"

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.

It's not simply a matter of "uncomfortable" syntactic sugar. It's a fundamental change from a statement to an expression.

Also, there is an elegance to the functional style of pattern matching. And C# has been incorporating a lot of functional paradigms for more than a decade now which may initially feel "out of place" but ultimately benefited the language greatly.

Re: C# 8: Switch expressions

#85
post #67

Earlier quoted context omitted.

This change was definitely inspired by F# (or ML ultimately). It's far from the first feature to be brought over. Look at async/await, usable tuples, lambda functions, range syntax, etc.

AFAICT, this is kind of true for a lot of F#: F# is where they do the experimentation, C# is where they do the more heavily engineered "fast" version.

That seems to be true at least to some extent. However, there are some noteworthy parts of the two languages that seem to be intentionally different. For example, F# optimizes tail recursion while C# doesn't.

Re: C# 8: Switch expressions

#86
post #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…

> He's just doing too much inline

It's two short lines.

The problem is that he's writing a small but more than one line bit of code, so the overhead of the anonymous function is just as big as the code itself, and arguably more complex.

Re: C# 8: Switch expressions

#87
post #30
post #19

Earlier quoted context omitted.

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

They aren't real guarantees in TypeScript, because the type system doesn't guarantee anything about the actual values that are flowing through the system. i.e. you have to code everything in TypeScript and make sure everyone follows the best practices.

Most statically-typed languages don't verify types at runtime either. As long as you enforce types at the boundaries between typed and untyped code, you should be fine. And enforcing invariants at the boundaries of your code is pretty important for all languages.

Re: C# 8: Switch expressions

#88

I am not sure I like the path c# is going down. For years the language has been trying to evolve from being aesthetic and easy to understand and read. With the functional implementations and patterns I believe the language is trying to embrace too much. This is not specific to the swith expression in this context but in general. You can write C# in too many forms and I don't believe that's really good.

I would not take pattern matching as the right example. I learned pattern matching at the university and always missed it in C#. It is easy and straightforward. When you love lambdas and linq, so C# 3.0, pattern matching is easy.

However, when it comes to modern C# and memory management I also think that readability suffers a lot. Memory management on that level is well known to the professional C/C++ folks but most of us are just overloaded with it.

Re: C# 8: Switch expressions

#89
post #83

Earlier quoted context omitted.

I think I agree. I used to write a lot of C# v2 and loved it. There didn't seem to be anything 'missing'. Each version since then has added nice things but are they really necessary? Now the language looks huge and complicated and because of that, I'm not sure I'd ever want to use it again.

C# 2 did not have Linq. Linq is the best feature ever.

Actually, adding LINQ to C# did not introduce any new functionality at all. Everything you can do in LINQ you could already accomplish in C# without LINQ. Yes, I suppose someone could say that the older code was uglier, but it already had the same capabilities as LINQ.

That's been the case for the majority of supposed "new features" in C# for at least the past 8 years or so -- LINQ, inline variables, anonymous functions, string literals etc. It's all just syntactic sugar on top of existing features.

Re: C# 8: Switch expressions

#90
post #83

Earlier quoted context omitted.

C# 2 did not have Linq. Linq is the best feature ever.

Actually, adding LINQ to C# did not introduce any new functionality at all. Everything you can do in LINQ you could already accomplish in C# without LINQ. Yes, I suppose someone could say that the older code was uglier, but it already had the same capabilities as LINQ. That's been the case for the majority of supposed "new features" in C# for at least the past 8 years or so -- LINQ, inline variables, anonymous functi…

That's the general nature of a turing complete language. The purpose of every feature is making things less ugly.
Post reply on HN