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...
For further actions, you may consider blocking this person and/or reporting abuse
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.
Same here. Typescript comes close though.
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.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.
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!
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.
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!
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.
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.)
Hey! What color scheme is that?
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"
Okay! Thanks!
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.
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#.
If you follow me, you'll see that I do.
Awesome! Why the Egyptian brackets, though?
Good catch. I do TypeScript and C# examples frequently and use different bracket styles for each. Here I used my TypeScript brackets on accident.
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.
And that largely holds true for object-oriented programming. DUs are more for supporting the functional flows of application logic.
Are
enum class
declarations automaticallysealed
?The dream of c# 9 sounds almost like scala 2 to me 😜
Scala 3 is copying a lot from C# and Java 😜
Bad dream! PLEASE USE CONVERSATION OF TYPEDEF VOID (CALLBACK)(VOID) Like this same from C/C++