Live data from Hacker News

What’s New in C# 7.0

blogs.msdn.microsoft.com

51–60 of 278 posts

Re: What’s New in C# 7.0

#51
post #31

All I want is easy syntax for immutable records, and C#-like a fluent syntax for .WithPhoneNumber(phoneNumber) etc. Seems like it'd be a cinch to implement, and for me the single handiest feature from F#. In F# these are "guaranteed" to be non-null, even though null instances come up during deserialization all the time. In C# use cases, I don't think the non-null "guarantee" should be made or implied, just a POCO.

Hi there! C# language designer here. You can see (and participate in) the discussion on records here if you'd like: https://github.com/dotnet/roslyn/issues/10154 > Seems like it'd be a cinch to implement Ah... how i wish that were so :)

Indeed. While records are my favorite feature from F#, they could be improved.

I'd love it if record definitions were extensible (even abstractly--sometimes you want a record with just the data, and sometimes including db-centric "id, createdTime, etc"), and there should be a way of defining / converting between them without a ton of boilerplate. That would allow something like:

record DbStuff = { id: int; created: DateTime }

record UserRecord: DbStuff { name: string; ... }

var userRecord = new UserRecord(...)

var userData = userRecord.WithoutDbStuff() // but what would be the reflected name of this type?

var newRecord = userData.WithDbStuff(3, DateTime.Now)

----

Sometimes you want to be able to define records

PhotoData{source, metadata, yada, etc, url} and

PhotoDisp{source, metadata, yada, etc, bytes}

without the repetition, and again with an easy way to convert between the two. (And yes you could simplify the above by using containership but oftentimes there are cross-cutting things that containership doesn't solve. You really want a flattening solution.)

Easy integration with C# anonymous classes should be considered too.

C# has always been the more real-world-centric language so I'd hope these common use cases would be considered.

Re: What’s New in C# 7.0

#52
post #45

Earlier quoted context omitted.

Adding features for the the subset of developers that need optional performance enhancements is great. However, it does seem ripe for misuse. As in the developer who makes everything a ref reference because they read it makes it "faster." Would there be a way to get Visual Studio to warn about this?

Not .NET developer here but the usual way is to use some sort of linting tool to "disable" the esoteric features.

These tend to become cargo cult practices, "thou shalt never" that get in the way of those few places where they are useful.

Re: What’s New in C# 7.0

#53

Earlier quoted context omitted.

Feels kinda crazy to see tuples and pattern matching "just arrive" when other languages have had them for years.

A big advantage of this design over most of the other languages with tuples that I've seen is that their members can have names - that is, types can be anonymous but have named members. This I think hits a sweet spot as it's rare to not want fields to have names, but fairly common to want intermediate types that just bundle a few fields together where there's no good name that clarifies much.

Anonymous items with named fields are always only just one name away from being structs. That seems to me to be the least-sweet spot in the possibilities of (named/anonymous item) x (named/anonymous fields).

Re: What’s New in C# 7.0

#54

In the binary literal proposal there is also some unresolved discussion about octal support, chip in if you like: https://github.com/dotnet/roslyn/issues/215

I wish more languages would take a radix-agnostic approach (at least to a reasonable limit). What's wrong with: 2#1010101 8#75342 1341235 10#13451 16#feb1300 radix#value, from erlang. Or: #b10101 #xfefe #o7777 #36rSSSS From common lisp. Customize in some fashion for C# and its current notations for hex and (now) binary literals. 036rSSSS 02r10101 0b10101 // this and above are equal

The reason people don't do this is:

1. Our current base notation is already familiar and widely used. Nobody is confused about 0xA vs 0b10.

2. Nobody will really use those other bases, so you're adding obscure features that muddle your syntax.

Re: What’s New in C# 7.0

#55
Does the pattern matching also statically check that you don't try to use a failed match.

What if instead of

     if (o is null) return;     // constant pattern "null"
     if (!(o is int i)) return; // type pattern "int i"
     WriteLine(new string('*', i));
somone forgot to return, as in

     if (!(o is int i)) {
         // do something
     }
     // incorrectly try to use `o` as an int
     WriteLine(new string('*', i));
Rust uses the special `match` syntax to avoid this. But I suppose C# 7.0 also analyse the structure of the `if`s and `return`s to acheive the same effect more flexibily.

And if it can do that, could that they also start phasing in statically-checked null avoidance?

Re: What’s New in C# 7.0

#56
post #45

Earlier quoted context omitted.

Hey there, C# language designer here. > Out variables seem like a mis-feature, especially when they are adding a better alternative (Tuples) in the same release. Out variables are really great when working with existing code that predates Tuples (for example, the entire BCL). We don't just introduce language features that will only work well for new code. We also want to make the experience of coding against existing…

Adding features for the the subset of developers that need optional performance enhancements is great. However, it does seem ripe for misuse. As in the developer who makes everything a ref reference because they read it makes it "faster." Would there be a way to get Visual Studio to warn about this?

Sure. Write a Roslyn analyzer to help out here.

Note: it's not like you can just sprinkle 'ref' on the return type of your methods willy nilly. Like 'ref'/'out' parameters, it requires you to do very specific things to keep your code safe/legal. As such, it's unlikely to just be added by people because, by and large, most code won't be equipped to actually handle it properly.

The codebases that will want to use this are already ones that have done a lot of work that makes them amenable to ref. i.e. game engines where you have large arrays of structs that you want to operate on in an efficient manner and whatnot.

Re: What’s New in C# 7.0

#57
post #43

Unfortunately, C# can flail about adopting F# features yet the best feature of F# is something C# can never get: Dependency Order Compilation

Except it does. And F# doesn't. F# just has in-order compilation. C# has an extra step to determine dependencies and compile them in dependency order. The only thing for F# is that F# allows the user to determine the compilation order and show it in Visual Studio.

Sorry I wasn't clear, I meant in the following sense:

"One of the most common complaints about F# is that it requires code to be in dependency order."

In light of your statement I understand the way I put it made little sense.

Re: What’s New in C# 7.0

#58
post #53

Earlier quoted context omitted.

A big advantage of this design over most of the other languages with tuples that I've seen is that their members can have names - that is, types can be anonymous but have named members. This I think hits a sweet spot as it's rare to not want fields to have names, but fairly common to want intermediate types that just bundle a few fields together where there's no good name that clarifies much.

Anonymous items with named fields are always only just one name away from being structs. That seems to me to be the least -sweet spot in the possibilities of (named/anonymous item) x (named/anonymous fields).

> Anonymous items with named fields are always only just one name away from being structs.

That's a huge line to cross though! Once you give it a name then you generally have to give it a home somewhere.

Re: What’s New in C# 7.0

#60

Pattern matching! Tuples! Awesome, C# 7.0 is scratching two itches I've felt every time I've worked with it. If only .NET had a suitable cross-platform UI toolkit, then I'd be using it everywhere. Eto.Forms is a good attempt but I found rather buggy and limited at this point in development. Avalonia, while further along in terms of stability and features, doesn't even try to fit in with any platform's look and feel.

This is the feature I've been waiting for!

Correct me if I'm too excited, but won't these be an excellent alternative to maintaining lots of DTOs for SQL calls?

Post reply on HN