Live data from Hacker News

What's new in C# 12: overview

pvs-studio.com

61–70 of 100 posts

Re: What's new in C# 12: overview

#61
post #44
post #35

Earlier quoted context omitted.

> int result = some_variable switch { a => callAFunctionReturningVoid(); 1, b => 2, }; What does it even mean? Multi-statement lambdas without return statement, I get it, but what is the return value? And returning a void to int? How is it all supposed to work? What the trailing comma means? Inconsistent way of dealing with things that may confuse developers just results in that proposal resisted.

It is not a lambda but a switch expression. The tricky bits here are separating a call and the returned expression with ;. The trailing comma is just sugar to allow for another case

Understood. You want to escape curly braces and let the last expression be the returned one. F# does implicitly return the last expression.

> The expression is the body of the function, the last expression of which generates a return value. Examples of valid lambda expressions include the following:

https://learn.microsoft.com/en-us/dotnet/fsharp/language-ref...

Re: What's new in C# 12: overview

#62
post #14

I love that Java and C# (not sure about VB.NET and F# in the .NET ecosystem) are continuing to get handy language features instead of collecting dust. The array syntax stuff is a nice win. Having other languages be the guinea pigs for language features is a good way to go.

C# has always been pretty good about evolving the language. Java has recently left the freezer and seems to be catching up as well. I don't use Java but I see people excited about new releases since v11 or so.

[dead]

Re: What's new in C# 12: overview

#63
post #41

Collection expressions are somewhere between spooky and very cool. If you don't specify a concrete type for the collection, the compiler is free to choose the "best" option given your usage of the collection: https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...

Yep, just like withHM type inference, it infers the most concrete type from the usage.

Re: What's new in C# 12: overview

#64
post #47

Earlier quoted context omitted.

Well, this features is designed to be used by source generators and not humans. So as it's intended for being used inside the compilation process, i don't think this is too valid of a concern.

Source code generators are used because the language itself is not expressive enough. Maybe fix that instead.

One example would be a tool that you compile in and records some state after every LOC in a method which you are interested in.

If you wanted to use a normal debugger at the same time you would usually be shown your code with the tool's generated code mixed in.

With this you could step through just your code.

Re: What's new in C# 12: overview

#65
post #30
post #25

Earlier quoted context omitted.

> I feel that recent "features" are more in the "things I can do with a preprocessor or a code generator" than real language enhancements. As someone that has spent far too much time in JS world any time I’m able to ditch a preprocessor or code generator is a a great relief. I want my build processes to be as simple and straightforward as they can possibly be.

C# preprocessor and code generator have nothing to do with JS build chains. The .NET build chain is relatively painless.

People very rarely rely on extra source code generation stages or preprocessors in C#. The Javascript ecosystem is dependent on them because Javascript is the only language that's a first class citizen in the browser. You can also do some pretty expressive things in C# with LINQ, expression trees, reflection, and so on. You can even emit IL at runtime if you really need to.

https://learn.microsoft.com/en-us/dotnet/csharp/advanced-top...

The only C# generator I'm familiar with is for protobuf. I suppose the P/Invoke generators count too.

Re: What's new in C# 12: overview

#66

I welcome these but for each of 8, 9, 10, 11 and 12 I still feel that the elphant (missing) in the room is a proper sum type. How come I have to write some 100 line monstrosity of a utility class each time i want "Either a result or an error" or similar.

Discriminated Unions will make working with Result easier as would provide exhaustive checking. DUs are being designed: https://github.com/dotnet/csharplang/issues/113

Re: What's new in C# 12: overview

#67

Earlier quoted context omitted.

What is the alternative specifically for unit tests? You can create integration and e2e tests that aren't as sensitive but I don't really understand what people are suggesting when they say mocking is bad in unit tests.

First: drop e2e, unit, etc distinction It is irrelevant and driven by some testing evangelists Tests are either quick or slow and may touch external stuff, thats mostly it. Whats wrong with mocks? If they lead to scenarios where your tests are green, but app doesnt work then it sucks. Ive witnessed projects with all green tests but app wasnt even waking.

I think there is an evolution developers go through. Right around the "design pattern" stage where everything is decoupled, hinges for all parts. Devs seem to focus on the test coverage value. Both these attributes stem from the same failure to understand the underlying business. The model rarely reflects the actual business needs (usually because the dev spends more time reading about, and thinking about tech as opposed to understanding the business) they use flexibility in the codebase as a fallback because gee the business can be anything. Code coverage equally fails because they only think about their code. Sometimes the most useful thing is a business case that was never thought about. It's code that doesn't exist. Perfect code coverage won't tell you anything about code you haven't written.

I'd rather spend time trying to learn the business, and where it's going, and why my product is useful, than mocking out some dumb part I can test a few times manually. Unit tests are super valuable tool, but not everything needs a unit test.

Re: What's new in C# 12: overview

#68
post #57
post #47

Earlier quoted context omitted.

Source code generators are used because the language itself is not expressive enough. Maybe fix that instead.

You can't make a language fully general without it turning into a mess; things like the protobuf compiler are reasonable use cases for source generation.

[deleted]

Re: What's new in C# 12: overview

#69

Earlier quoted context omitted.

Well, this features is designed to be used by source generators and not humans. So as it's intended for being used inside the compilation process, i don't think this is too valid of a concern.

That's good to know. I wonder if VS and VS Code are set to at least produce warnings when the feature is used in human generated code. Any feature can be abused, will be abused. At least with warnings by default the programmer can know this isn't a desired use case and then decide if their particular need is a worthwhile exception.

On the other hand if this oopsies its way into a codebase it could highlight a serious problem with the code review process..

Re: What's new in C# 12: overview

#70

Earlier quoted context omitted.

>I feel that recent "features" are more in the "things I can do with a preprocessor or a code generator" than real language enhancements. Well, you can hack a lot with code gen, but thats not the way, imo. But using your logic: are generics a feature? Since you could achieve almost identical results with code gen hacks

I didn't write that all additions to C# since 1.0 are useless. We have generics since .NET 2 (2005 IIRC) and at the time, the lang improvements had a reason to exist (framework support in some cases). At the time it was a nice thing to add, if you ask me. For me, the next big thing, if you ask, was LINQ. A few years later, I feel it's not the case with recent additions. But my comment is very subjective, of course. A…

Well, is LINQ really a lang. feature?

Extension methods are lang feature, LINQ is implemented with ext. Methods.

You can argue that there is LINQ Query syntax instead of method chaining, but almost nobody is using this except for some specific cases (let keyword)

Post reply on HN