BREAKINGOpen-source C# 6.0 released
At last, the long wait is finally over! After 4 years of waiting, the fully managed implementation of the C# compiler codenamed "Roslyn" has been finally released. In the recent months, "Roslyn" has been slowly turning into vaporware, but thanks to the recent breakthrough, the team made an enormous progress over the last two months and even implemented a number of new C# 6.0 features.
The C# 6.0 compiler, together with the full source code has been released today!
UPDATE: In case you are reading this article later than on the day when it was published, let me just point out that this was released on 1 April 2014. Just to avoid any disappointments. Have fun ;-).
The new "Roslyn" implementation
Roslyn team started by re-implementing the compiler in C#. A team member who prefers to remain anonymous comments: "Writing a language X in X is kind of cool, right? We just had to try! We also have a secret project to implement Excel fully as an Excel spreadsheet." This turned out not to be a good idea. While C# is a fine language for writing line-of-business applications, it lacks many features that are practically a must-have when writing a compiler such as algebraic data types and pattern matching.
As is often the case, the breakthrough was due to a lucky accident: "One of our colleagues came back to work after a long party and accidentally clicked a wrong button! His vision was a bit fuzzy and so he just mistook F for C." It turns out that the button was labeled "New F# Project" and it saved the project. Mads Torgersen from the C# team comments:
You know, F# has all you need to implement Roslyn - algebraic data types, pattern matching and even active patterns! Once we discovered that it is already in Visual Studio, we started from scratch and finished the new managed C# compiler in just two months!
To those familiar with F#, this is not a surprise. An F# community member, Neil Danson recently implemented a C# sub-set in F#. Indeed, the C# team decided to stand on the shoulders of giants: "It was easy. We just took Neil's code, added a couple of missing features and it was ready to ship!" As the "new Microsoft" is a big proponent of open-source, the C# team follows and released the complete source code:
- C# 6.0 compiler source code - available on CodePlex, cleverly hidden under the "F# PowerPack with F# Compiler Source Drops" project.
Why is the release done so secretly? The "new Microsoft" not only cares about open-source, but is also sensitive to social issues. After all, the creator of C# 6.0, Anders Hejlsberg is Danish. He explains:
We added a few new language features and it made the language quite powerful. Maybe a bit too much. If everyone starts using this, it can badly affect the unemployment rate, because solving problems will be just too easy! So we wanted to hide it a little bit...
What do the new features look like? The C# team started a nice web site where you can try it yourself, but let's have a quick look.
Introducing new features in C# 6
At the NDC 2013 talk in London,
Mads Torgersen discussed some ideas for C# 6.0. You can find a nice write-up here.
The final version of C# 6.0 goes even further. In the talk, Mads discussed the example of implementing
Point class and ended up with the following:
1: 2: 3: 4: 5:
Compared to C# 5, there are a few nice things. First, you can write so called main constructor
that makes parameters
y automatically visible to the body. Creating properties and
methods is also simplified using the
=> syntax. Yet, the code was still not completely
satisfactory. Anders Hejlsberg provides an explanation:
In C# 3.0, we introduced the
varkeyword and type-inference. So, why do we have to write
doublefive times? Also, there is a lot of noise when you have to mark everything as
publicexplicitly. We just want to make developer's life easier!
In the final version of C# 6.0, you can write the class as follows:
1: 2: 3: 4:
y are still annotated with types, but the rest of the types are
inferred automatically. The compiler is smart enough to know that multiplication and addition
of floating point numbers is also a floating point.
You can also see that the
class keyword has been replaced with
type. Mads Torgersen
explains this design decision:
Well, classes are not the only useful type. There are interfaces, delegates and so on. So, we just made the language more uniform and added a few more useful types!
The addition of "a few more useful types" means that we can make the
Point even simpler
using the record type:
1: 2: 3:
Another new type that has been added is called discriminated union. This basically lets you
implement an entire class hierarchy in just 3 lines of code. For example, say you want to model
2D shapes including
Circle. Instead of writing abstract class with two
inherited classes (in 3 separate files), you can just write:
1: 2: 3:
The documentation is still somewhat sparse, but the new C# 6.0 features are heavily inspired by Scala and you can find great documentation on the Scala for Fun and Profit web site.
This release presents an important milestone in the development of C#. Not only is the compiler now implemented in a fully managed and extensible way, but it has also been open-sourced. The community has already used it to develop useful addins for the Visual Studio IDE as well as other tools.
The new langauge features in C# 6 make the language significantly more powerful and expressive. The only worry expressed by some is: "Doesn't C# 6.0 look a little like F#?" Anders Hejlsberg dismisses this worry:
The C# design obviously takes inspiration from other languages. So, yes, you can definitely see a lot of inspiration from F# in the C# 6.0 release. We are happy to adopt good ideas developed elsewhere and F# simply has a lot of good ideas!
type Point =
new : x:float * y:float -> Point
member Distance : float
member X : float
member Y : float
Full name: csharpreleased.Point
new : x:float * y:float -> Point
val float : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float
type float = System.Double
Full name: Microsoft.FSharp.Core.float
type float<'Measure> = float
Full name: Microsoft.FSharp.Core.float<_>
Full name: csharpreleased.Point.X
Full name: csharpreleased.Point.Y
Full name: csharpreleased.Point.Distance
Full name: Microsoft.FSharp.Core.Operators.sqrt
| Rectangle of x1: float * y1: float * x2: float * y2: float
| Circle of x: float * y: float * diameter: float
Full name: csharpreleased.Shape