F# Now Available

F# is a research project from Microsoft Research. It is not a Microsoft product. All opinions are my own. All content is provided "AS IS" with no warranties, and confers no rights.

F# is now available at http://research.microsoft.com/downloads.  This release was made to coincide with Microsoft Research "TechFest 2005", which I mentioned in a previous post.

I've just posted the detailed release notes (this includes a summary of known issues).  Besides the stablizing bug fixes, this release has two interesting new features: a modicum of operator-overloading (in a fashion suitable for .NET, but not going overboard!!), and subtype constrained polymorphism.  The latter lets you do two things: firstly, F# functions can now have type signatures that feel much more like the signatures that you expect of .NET functions, and secondly F# now has the mechanisms to support .NET's constrained generic type parameters. 

As an example of the former, consider the function Idioms.using.  This is F#'s current way of simulating the C# "using" statement.

let using resource action =
  try action()
  finally (resource :> System.IDisposable).Dispose()

This now has the following signature:

val Idioms.using: (_ :> System.IDisposable) -> (unit -> 'a) -> 'a

Note that in F# ":>" means "statically coerce" and cannot fail, i.e. is an upcast and not a downcast, and so F# has inferred the constraint on the first parameter.   Here is an example of the function in use (taking an example from a piece of code that draws part of a pie chart using the System.Windows.Forms library).

      let brush = new SolidBrush(color) in
      Idioms.using brush (fun () ->
        let sweepAngle = ... in

Basically the code in blue is executed under the scope of the "using" statement, meaning that at the end of the statement the given brush object is disposed.    In previous versions of F# the "Idioms.using" function had the F# type signature

   val Idioms.using: System.IDisposable -> (unit -> 'a) -> 'a

F# takesthe first type quite literally, and interprets it as saying "you must pass something of exactly type System.IDisposable", and so callers had to insert spurious coercions callsites.  The new signature is

   val Idioms.using: (_ :> System.IDisposable) -> (unit -> 'a) -> 'a

which is effectively the same as

   val Idioms.using: 'resource -> (unit -> 'a) -> 'a
when 'resource :> System.IDisposable

and this means exactly what we want to say: "you can call this on any type that is coercable to IDisposable". 

In other words, subtype constrained polymorphism  is used to simulate subsumption at callsites.  There are some limitations of this technique in the context of .NET (currently discussed in the CHANGE notice for this release) though these are not more restrictive than those applied previously by F#.    Some refinements to these mechanisms may be made for future releases, e.g. the way in which these functions appear to C# may be changed.  For the moment subtype-constrained functions are mostly for use within F# code. 

Note the above feature can be used with or without .NET generics: if you're not using .NET generics then F# implements its type variables by erasure, which means 'resource, 'a and friends become the type System.Object, the constraint is removed and a cast is inserted at the point where the constraint is invoked (this cast should not fail if you stick to calling F# code from F# code).


Comments (2)

  1. chaitnaya says:

    this is good

Skip to main content