DEV Community

Cover image for The Dream of C# 9.0

The Dream of C# 9.0

Matt Eland on December 29, 2019

As part of the final entry in the C# Advent series of 2019, Calvin Allen touched on some proposals in the C# 9 language that he was most excited ab...
Collapse
 
cappe987 profile image
Casper

I really wish they would bring in discriminated unions to C#. Ever since I started using them in F# I've missed them in every other language I've used.

Collapse
 
integerman profile image
Matt Eland

Same here. Typescript comes close though.

Collapse
 
haaxor1689 profile image
Maroš Beťko

Typescript doesn't come close. It goes way above and beyon with it's intersection, union, mapped and conditional types. I worked on a project where I was in charge of frontend (ts) and my coworker was doing backend (.net) and we wanted to move some logic from client to server. And it just wasn't possible to write without using dynamic which throws all compile time checks out of the window.

Thread Thread
 
nthcommit profile image
Michael Fry

While I agree that the TS type system is awesome, discriminated unions in TS are a bit clunky, compared to Haskell/F#. Would be good to see them become a first class concept in TS, and not a side-effect of the super flow analysis.

Collapse
 
brugner profile image
Nery Brugnoni

When you think the language is done, the guys from the C# team keep adding things. It never ceases to amaze me how they come up with new ideas and implementations. I love it!

Collapse
 
saint4eva profile image
saint4eva

The C# team is quite amazing. They keep introducing features to make the language better; the amazing aspect of it is that they always design new features to be idiomatic and feel "C#" in nature. What an elegant and multipurpose language C# is.

Collapse
 
dudeinthemoon42 profile image
Bobby Barjasteh

Can the declarations inside those discriminated unions theoretically be inline functions? I know in your example you're providing the arguments and doing a little logic in the switch, i'm wondering if you could also do that kind of thing in the constructor. Maybe it wouldn't be helpful though. Cool article!

Collapse
 
integerman profile image
Matt Eland

Possibly. I couldn't find as many examples of working with Discriminated Unions in the feature proposal thread. I went with the switch expressions to try to get the syntax looking as close as F#'s Match expressions as possible while still working with existing C# syntax.

Collapse
 
eljayadobe profile image
Eljay-Adobe

Some of the features I'd like to see in C# are borrowed from Midori's programming language (which evolved from Sing#, which evolved from Spec#), F# functional programming language, and D programming language.
C# Code Contracts that were backported from Spec# are broken. Besides being dog slow, they don't compose polymorphically. That kind of facility needs to be in the core language, not a bolt-on terribly slow afterthought. Here C# could take a page out of D's playbook
Unit testing should be part of the core language. I've used NUnit, and I've used xUnit.net, and there are others. The problem with them is that they all solve that problem space their own way, so there's lock-in for something that ought to be a core language construct. (As long as I have the magic of NCrunch, I'm only complaining about the lock-in. The test runners can do their own thing their own way with their own reporting models.) Another page out of D's playbook.
The key concept I've picked up on from Midori is bugs are not recoverable errors. That notion is not something that C# has learned.
The other neat thing in Midori is that calling a method that could throw an exception requires notation at the callsite: var y = try blah(); The required try there makes the "invisible goto" no longer invisible.
F# has too many awesome things to list. I wouldn't mind seeing more F#-isms in C#, with due consideration. But C# will never be F#. They serve different programming purposes, and are suitable programming language tools for different domains. And that's a good thing.
C# not having non-nullable references from the get-go was a mistake, and I'm happy to see that this trillion dollar mistake is being addressed. (Sir Tony Hoare's mistake in ALGOL W was merely a billion dollar mistake. C# is much more widespread.)

Collapse
 
melun profile image
help me aaaaaaa

Hey! What color scheme is that?

Collapse
 
integerman profile image
Matt Eland

For some of my smaller examples - especially those featuring future language features, I use Carbon to create code example visuals.

This is the A11y Dark scheme. You can find out more about Carbon at carbon.now.sh/

I believe most of their color schemes are based on IDE configs, so you might be able to find it for your editor of choice by searching for "A11y Dark"

Collapse
 
melun profile image
help me aaaaaaa

Okay! Thanks!

Collapse
 
saint4eva profile image
saint4eva

I love C#. It is improving and getting better every release. C# is a true multipurpose programming language. You can use it to develop the web, IoT, desktop solutions, server application, machine learning and big data manipulation, games as well as mobile app.
Thank you for your writeup- I really enjoyed reading it.

Collapse
 
markbroadhurst profile image
Mark Broadhurst

Some of these features have been promised since C#6.

While I appreciate the F# love if you want the features why not just use F#.

Collapse
 
integerman profile image
Matt Eland

If you follow me, you'll see that I do.

Collapse
 
amphioxys profile image
Amphioxys

Awesome! Why the Egyptian brackets, though?

Collapse
 
integerman profile image
Matt Eland

Good catch. I do TypeScript and C# examples frequently and use different bracket styles for each. Here I used my TypeScript brackets on accident.

Collapse
 
ants profile image
ants

As pretty as the discriminated unions are, I was taught that if I am switching based on an object type, it is a code smell that I probably should be using polymorphism and the strategy pattern.

Collapse
 
integerman profile image
Matt Eland

And that largely holds true for object-oriented programming. DUs are more for supporting the functional flows of application logic.

Collapse
 
ants profile image
ants

Are enum class declarations automatically sealed ?

Collapse
 
oswaldo profile image
Oswaldo Dantas

The dream of c# 9 sounds almost like scala 2 to me 😜

Collapse
 
saint4eva profile image
saint4eva • Edited

Scala 3 is copying a lot from C# and Java 😜

Collapse
 
jenseckervogt profile image
Jens Eckervogt

Bad dream! PLEASE USE CONVERSATION OF TYPEDEF VOID (CALLBACK)(VOID) Like this same from C/C++