Live data from Hacker News

C# 8: Switch expressions

alexatnet.com

111–120 of 124 posts

Re: C# 8: Switch expressions

#111
post #102

This is perhaps the most obvious example yet of what PG described as "taking features from Lisp and gluing them to C". That's not necessarily a bad thing -- this is a great and useful feature, and one I frequently miss when using C-family languages. I wonder, though, at what point a programming language collapses on itself, from too much syntax. I haven't used C# since the 4.x days, and I recall back then it was alre…

This is my feeling as well. I suggested that a c# lite or a rethinking of c# would be awesome. I got downvoted to oblivion though. But I do think that there's too much mental capacity used just reading c# if the code really uses all the features of it. I like go lang cause of this, but go is a bit too far on the other side of the spectrum for me.

> I like go lang cause of this

I'm a huge fan of C#, but the lack of features makes Go so damned pleasant to read. As an extreme example, the new C# record type really breaks the neurons I have dedicated to the language:

    public struct Pair(object First, object Second);
As soon as I see parenthesis, it's a method. I want record types, but that syntax is foreign. I wish Anders would spend a few months back on the C# team, he has a great knack for keeping things as consistent and minimal as possible.

Re: C# 8: Switch expressions

#112
post #65
post #9

Java is also getting switch expressions next month (as a preview feature in JDK 12): https://openjdk.java.net/jeps/325

It is exactly the same isn't it?

It's not. C#'s switch can pattern match on value and type: https://blogs.msdn.microsoft.com/dotnet/2019/01/24/do-more-w...

Java's switch still operates only on String, int, short, byte, char, and their wrapper types: https://blog.codefx.org/java/switch-expressions/

Re: C# 8: Switch expressions

#113
post #31

Earlier quoted context omitted.

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.

C# doesn't allow fallthrough. Every branch in a case clause must exit the case clause. Fallthrough can only be aachieved with a goto at the end of the case clause. The `break;` requirement is completely superficial; there's no reason why it couldn't be a classical block/statement choice:

    case "foo" { /* ... */ }
    case "bar" /* ... */;

Re: C# 8: Switch expressions

#114
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…

Do you know what "expression trees" are and how they relate to LINQ?

Re: C# 8: Switch expressions

#115

Earlier quoted context omitted.

This is my feeling as well. I suggested that a c# lite or a rethinking of c# would be awesome. I got downvoted to oblivion though. But I do think that there's too much mental capacity used just reading c# if the code really uses all the features of it. I like go lang cause of this, but go is a bit too far on the other side of the spectrum for me.

> I like go lang cause of this I'm a huge fan of C#, but the lack of features makes Go so damned pleasant to read. As an extreme example, the new C# record type really breaks the neurons I have dedicated to the language: public struct Pair(object First, object Second); As soon as I see parenthesis, it's a method. I want record types, but that syntax is foreign. I wish Anders would spend a few months back on the C# te…

    public struct Pair(object First, object Second);

Scala, Kotlin, Rust among others use the same or similar syntax.

Re: C# 8: Switch expressions

#116
post #5

To me, all these IIFE’s make a good case for adding a block-as-expression construct. Either take from Scala, where every block evaluates to the last expression executed in that block (and every statement is an expression); or maybe introduce some new syntax (‘do { .... }’ maybe?) if the prior would be a breaking change.

There's a bytecode representation already - you can use it when compiling to IL: https://docs.microsoft.com/en-us/dotnet/api/system.linq.expr...

What's missing is a way to express an anonymous block expression in C#. I'm not sure that's a gap worth filling, though - if the logic is worth a block, isn't it worth a name?

Re: C# 8: Switch expressions

#117
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…

IIRC, LINQ can be translated to C# 3.0 without LINQ, so I guess it is syntactic suger but for C# 3.0. To implement it in C# 2.0 one would need to replace expression trees with “home made” query specification classes. It would be a lot of work, it would not be standardized, and it would not be integrated in the language.

So the real new feature in C# 3.0 are the expression trees. LINQ is only one place where expression trees are used.

Re: C# 8: Switch expressions

#118
post #116
post #5

To me, all these IIFE’s make a good case for adding a block-as-expression construct. Either take from Scala, where every block evaluates to the last expression executed in that block (and every statement is an expression); or maybe introduce some new syntax (‘do { .... }’ maybe?) if the prior would be a breaking change.

There's a bytecode representation already - you can use it when compiling to IL: https://docs.microsoft.com/en-us/dotnet/api/system.linq.expr... What's missing is a way to express an anonymous block expression in C#. I'm not sure that's a gap worth filling, though - if the logic is worth a block, isn't it worth a name?

“If” statements and loops already make use of anonymous blocks, and I think serve as ample demonstration that, no, not all blocks are better off named. What’s missing is the ability to use those blocks wherever an expression is expected.

Re: C# 8: Switch expressions

#119
post #62

Earlier quoted context omitted.

Rather than adding syntax for it, the sane approach would just be to generalize the functionality. T Log (string msg, Func func) { Log(msg); return func(); } var result = operation switch { "+" => Log("addition", () => a + b), "-" => Log("subtraction", () => a - b), "/" => Log("division", () => a / b), _ => throw new NotSupportedException() }; This is shorter, cleaner, and how most folks would actually write somethin…

I don't know a great deal of C#, but couldn't you just do: T Log (string msg, T result) { Log(msg); return result; } var result = operation switch { "+" => Log("addition", a + b), "-" => Log("subtraction", a - b), "/" => Log("division", a / b), _ => throw new NotSupportedException() }; You already have a lambda for calling Log(), no need for a second to call the inner function. This does change the order of evaluatio…

>To be honest, I find the idea of passing a parameter to Log() (whether it's the value or the function) that your don't actually want logged, so that you can shoehorn two statements into an expression, abhorrent.

You've basically reimplemented Haskell's Debug.Trace[0] :)

The difference is of course that Haskell doesn't really have statements in the same way that C# does, so in Haskell it's nescessary to turn the log/trace-function into an identity function after you've applied the message-string.

I agree that it is ugly, and in any serious project I would use a proper logging setup, but I almost feel like the ugliness is a feature not a bug. It's like an extra cost when doing debug-by-print, which at least seem to keep me more mindful about cleaning up after myself.

[0] http://hackage.haskell.org/package/base-4.12.0.0/docs/Debug-...

Re: C# 8: Switch expressions

#120
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…

There is no way you can implement Linq in C# 2.0. C# 2 does not have lambdas and expression trees, so you cannot write an expression which can either be compiled into regular code or into an expression tree depending on context.
Post reply on HN