I’ve had a look at various samples of F#/WPF, and contrasted that with the experience I’ve gained over the last 15 months doing a lot of commercial WPF. I can see a lot of potential for F#, but not combined with WPF. Here’s why:
XAML
XAML is an XML based language for defining and instantiating object trees. It’s possible, indeed easy, to use away from WPF, but the most common usage today is surely with WPF. Here it functions as a kind of markup language for GUIs. Just as its a lot easier to create a web page in HTML than to create one using the DOM, so creating WPF Windows, Pages and User Controls is better done in XAML than masses of imperative code. In addition to layout, its relatively simple to define visual styles, animations, data bindings, and even quite powerful interactivity with triggers, all in XAML. The code behind a WPF form is often rather minimal. I’ll even go so far as to say, if you have a lot of code behind, you should probably review it.
F# isn’t a GUI markup language, just as C# wasn’t, and it makes little sense to replace XAML with F#.
Tool Support
When you a create a new form in WPF, you typically subclass the Window class (there are other candidate base classes). The XAML file is used at run time to instantiate each instance of this class (usually via a pre-compiled BAML data). If you use Visual Studio or Blend to create the form, it creates the subclass as 2 C# partials – one with some necessary plumbing, and the other acting as the code behind file, for you to work in (Visual Studio does a good job of hiding the plumbing partial, but it’s there, with *.g.cs extension.)
There’s nothing mysterious about the the generated partial, and we could re-create it in F#. However, there is no direct advantage to doing this – its more work, and just another source of potential bugs. We could create the tooling to avoid this problem, but F# has nothing like C#’s partial classes and we’d end up with the plumbing and code behind in one ugly file.
MVC
MVC being the Model View Controller Pattern. In WPF it’s more common to use a variations on that theme (M-V-MV DM-V-VM), but the idea remains the same – get a clean separation between the UI, the data, and the application logic (yes, “application logic” is very vague, and in practice we often have several controllers). Essentially, MVC is the means to make good on the idea of minimal code behind files.
No F# On The Client?
No need to throw away the baby with the bath water. I have outlined some good reasons to keep the WPF forms etc in XAML, and use the extant tools which will back that up with C#. That’s just the “V” in MVC though, and if we really have a clean separation, we can certainly explorer building the “M” and the “C” in F#. So, that’s what I’ll be doing next.
Incidentally, if you’re building an application that will generate a lot of WPF objects dynamically, and place much less reliance on XAML, then F# could be useful here. 3D and Data Visualization applications spring to mind.