Friday, March 23, 2012

F# 3.0 CLIMutable Attribute

If you have ever been working with XAML with some designer support, you will be additive to the benefit brought by designer. I'd spend weeks without implementing any feature and work on designer support. One scenario is described here. Default constructor is the key! Unfortunately, the record type in F# does not have a default constructor by default and it makes binding difficult.

Now we have CLIMutable to solve this problem. The screenshot for CLIMutable is below:

If you define the following F# code:

[ < CLIMutable > ]
type R =
  { X: int; Y:int }
type R2 =
  { X: int; Y:int }

then in the C# code you can do

var x = new R();
var x2 = new R(0, 2);
var y = new R2(0, 2);
// var y2 = new R2();   //does not compile

The type R has the default constructor while R2, which is not decorated with CLIMutable, does not have the default constructor.

Hopefully this 3.0 feature can make your UI work a little easier. :-)


1 comment:

Phillip Trelford said...

This looks like a great feature. It would be nice if we could also write something like this:

mutable type R = {X:int, Y:int}