Tomas Petricek

Searching for new ways of thinking in programming & working with data

I believe that the most interesting work is not the one solving hard problems, but the one changing how we think about the world. I follow this belief in my work on data science tools, functional programming and F# teaching, in my programming languages research and I try to understand it through philosophy of science.

The Gamma

I'm working on making data-driven storytelling easier, more open and reproducible at the Alan Turing Institute.

Consulting

I'm author of definitive F# books and open-source libraries. I offer my F# training and consulting services as part of fsharpWorks.

Academic

I published papers about theory of context-aware programming languages, type providers, but also philosophy of science.

Tomas Petricek
  • Tomas Petricek
  • Home
  • F# Trainings
  • Talks and books
  • The Gamma
  • Academic

BREAKING Open-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 a simple Point class and ended up with the following:

1: 
2: 
3: 
4: 
5: 
public class Point(double x, double y) {
  public double X => x;
  public double Y => y;
  public double Distance => Math.Sqrt(x*x + y*y);
}

Compared to C# 5, there are a few nice things. First, you can write so called main constructor that makes parameters x and 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 var keyword and type-inference. So, why do we have to write double five times? Also, there is a lot of noise when you have to mark everything as public explicitly. 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: 
type Point(x:float, y:float) =
  member this.X = x
  member this.Y = y
  member this.Distance = sqrt(x*x + y*y)

The arguments x and 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: 
type Point = 
  { X:float; Y:float }
  member this.Distance = sqrt(x*x + y*y)

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 Rectangle and Circle. Instead of writing abstract class with two inherited classes (in 3 separate files), you can just write:

1: 
2: 
3: 
type Shape = 
  | Rectangle of x1:float*y1:float * x2:float*y2:float
  | Circle of x:float*y:float * diameter:float

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.

Conclusions

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 language 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!

Multiple items
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 x : float
Multiple items
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<_>
val y : float
val this : Point
member Point.X : float

Full name: csharpreleased.Point.X
member Point.Y : float

Full name: csharpreleased.Point.Y
member Point.Distance : float

Full name: csharpreleased.Point.Distance
val sqrt : value:'T -> 'U (requires member Sqrt)

Full name: Microsoft.FSharp.Core.Operators.sqrt
property Point.Distance: float
type Shape =
  | Rectangle of x1: float * y1: float * x2: float * y2: float
  | Circle of x: float * y: float * diameter: float

Full name: csharpreleased.Shape
union case Shape.Rectangle: x1: float * y1: float * x2: float * y2: float -> Shape
union case Shape.Circle: x: float * y: float * diameter: float -> Shape
argument x : float
argument y : float

Published: Tuesday, 1 April 2014, 2:24 PM
Author: Tomas Petricek
Typos: Send me pull request!
Tags: c#, fun, functional programming

Contact & about

This site is hosted on GitHub and is generated using F# Formatting and DotLiquid. For more info, see the website source on GitHub.

Please submit issues & corrections on GitHub. Use pull requests for minor corrections only.

  • Twitter: @tomaspetricek
  • GitHub: @tpetricek
  • Email me: tomas@tomasp.net

Blog archives

November 2018 (1),  October 2018 (1),  May 2018 (1),  September 2017 (1),  June 2017 (1),  April 2017 (1),  March 2017 (2),  January 2017 (1),  October 2016 (1),  September 2016 (2),  August 2016 (1),  July 2016 (1),  May 2016 (2),  April 2016 (1),  December 2015 (2),  November 2015 (1),  September 2015 (3),  July 2015 (1),  June 2015 (1),  May 2015 (2),  April 2015 (3),  March 2015 (2),  February 2015 (1),  January 2015 (2),  December 2014 (1),  May 2014 (3),  April 2014 (2),  March 2014 (1),  January 2014 (2),  December 2013 (1),  November 2013 (1),  October 2013 (1),  September 2013 (1),  August 2013 (2),  May 2013 (1),  April 2013 (1),  March 2013 (1),  February 2013 (1),  January 2013 (1),  December 2012 (2),  October 2012 (1),  August 2012 (3),  June 2012 (2),  April 2012 (1),  March 2012 (4),  February 2012 (5),  January 2012 (2),  November 2011 (5),  August 2011 (3),  July 2011 (2),  June 2011 (2),  May 2011 (2),  March 2011 (4),  December 2010 (1),  November 2010 (6),  October 2010 (6),  September 2010 (4),  July 2010 (3),  June 2010 (2),  May 2010 (1),  February 2010 (2),  January 2010 (3),  December 2009 (3),  July 2009 (1),  June 2009 (3),  May 2009 (2),  April 2009 (1),  March 2009 (2),  February 2009 (1),  December 2008 (1),  November 2008 (5),  October 2008 (1),  September 2008 (1),  June 2008 (1),  March 2008 (3),  February 2008 (1),  December 2007 (2),  November 2007 (6),  October 2007 (1),  September 2007 (1),  August 2007 (1),  July 2007 (2),  April 2007 (2),  March 2007 (2),  February 2007 (3),  January 2007 (2),  November 2006 (1),  October 2006 (3),  August 2006 (2),  July 2006 (1),  June 2006 (3),  May 2006 (2),  April 2006 (2),  December 2005 (1),  July 2005 (4),  June 2005 (5),  May 2005 (1),  April 2005 (3),  March 2005 (3),  January 2005 (1),  December 2004 (3),  November 2004 (2), 

License

Unless explicitly mentioned, all articles on this site are licensed under Creative Commons Attribution Share Alike. All source code samples are licensed under the MIT License.

CC License logo