F#: is there no UI (like WPF) for it?

11,773

Solution 1

F# actually has some very nice constructs for creating event-driven UI applications, such as First Class Events, Object Expressions, calling property setters from a constructor e.g.:

new Form(Text="My Window Title", Width=600, Height=400),

and much else.

However, creating a forms designer in VS reqiures a CodeDom for your language. The current CodeDom architecture works great, as long as your language looks exactly like C# or VB; it does not lend itself well to generation of F# code (this from a webcast or interview that I can't locate right now). It also requires partial classes, which if I recall correctly, are not supported in the language as of Beta 1. Rather than focus on designer support in the first release, the F# team decided to spend their resources on enhancing other parts of the language, such as asynchronous and parallel programming, etc.

What this means is that you have at least 4 choices for creating UI in F#:

  • Write all UI code by hand, which is fine for simple apps;
  • Create your F# code as a library to handle the "hard parts," like asynchronous and parallel code, or computation centric code, and call it from C#/VB;
  • Create your UI code as a C#/VB library, and both drive it from F# and delegate event handling to F#; or
  • Use a DSL or Computation Expression (monad) to simplify building the UI by hand (just discovered this while looking for other links in this answer).

Of these, calling a C# UI library from F# may be the most flexible while still retaining a familiar paradigm. But, using computation expressions for quickly building UI by hand is certainly worth looking at.

Solution 2

With F# 3.0 and the XAML type provider it's possible to create a WPF designer for F# in Visual Studio 11. See http://www.navision-blog.de/2012/03/22/wpf-designer-for-f/

Solution 3

You can certainly create GUIs in F# - it's just another .NET language, after all.

Tomas Petricek's book, Functional Programming for the Real World (which I've helped out with a little bit) has various GUI examples. The source code is available to download if you want to see examples.

Admittedly some aspects of GUI programming don't map terribly well to a functional style, as there's a lot of mutation involved, but there are ways and means around that :)

Solution 4

We sell a commercial library called F# for Visualization that is written in 100% F# code and uses WPF to provide interactive graphics with typeset mathematics from your F# code:

alt text
(source: ffconsultancy.com)

So it is certainly possible to write GUI apps in F# using WPF.

Solution 5

It is a .NET language, so it can use the .NET class library. Which means Winforms, WPF or anything else you might use in C#.

Share:
11,773

Related videos on Youtube

iceangel89
Author by

iceangel89

a web/software developer who always seem to want to make things difficult for myself by picking up new things as i see it. i currently am more familiar with PHP/Zend Framework but is also exploring ASP.NET MVC and Ruby on Rails ... is Python good? maybe next time for the software development department, i did VB 2005 b4 but am exploring C# 3.0 and 4.0 in near future, WPF for fancy looks, LINQ ... whats the diff bet LINQ and Entity Framework ??? havent figured that out ... LINQ seems better with auto complete and compile time checking

Updated on April 15, 2022

Comments

  • iceangel89
    iceangel89 about 2 years

    i recently saw some videos on F#. it seems it used mainly for Service or Classes only? i see no "F# WPF" app in VS2010 Beta?

  • fishlips
    fishlips almost 15 years
    Have a look at wxHaskell or Gtk2Hs :) AFAIK, F# can be integrated with WPF.
  • iceangel89
    iceangel89 almost 15 years
    oh, so i can say that F# (or functional programming) is used mainly for developing libraries for something else (eg. C#) to use?
  • Jon Skeet
    Jon Skeet almost 15 years
    No, I don't think that sort of generalisation is really appropriate either.
  • James Hugard
    James Hugard almost 15 years
    Also, there is an excellent series of articles on creating WPF apps in F# here: blogs.msdn.com/dsyme/archive/2008/01/05/…
  • weiqure
    weiqure almost 15 years
    Sure, you can do OOP and state quite well. Still, the focus is on the functional side of the language. Otherwise, what would be the reason why there is no GUI project type?
  • James Hugard
    James Hugard almost 15 years
    Downvoted because while the emphasis of the 1.0 release is utility and core code, the intention is to eventually bring F# to full integration, including forms designer, etc.
  • Botz3000
    Botz3000 almost 15 years
    Oh, sorry, didn't know that. But full integration of F# sounds very interesting and like it's gonna be fun using it. Looking forward to it.
  • James Hugard
    James Hugard almost 15 years
    @weiqure - That statement might be more true for other ML derived languages, but only slightly so. IMHO, the F# designers have done a bang-up job making OOP in general, and .NET integration in particular, very natural and, in many cases, substantially easier than in, say, C#; e.g. object expressions, implicit constructors, property accessors, property setters in constructors, automatic conversion of lambdas to delegates, first-class events, not to mention type inference by virtue of being an ML-derived language. Most of these are in direct support of stateful, mutable OOP-style programming.
  • weiqure
    weiqure almost 15 years
    Sorry if I caused misunderstandings. F# supports these features nicely. It is not worse for GUI programming than C# or VB. But I still think that this is not the main use case for the language which I think is the reason why there is 'no "F# WPF"' project in WPF which the original question was about. Though, if there is another reason I'll be happy to learn about it.
  • James Hugard
    James Hugard almost 15 years
    Just to back this up, see the VS2010 integration announcement at blogs.msdn.com/dsyme/archive/2008/12/10/…. Specifically, "In this first supported release, our aim has to be to focus on the core strengths of F# for exploratory programming with F# Interactive, programming with data and implementing parallel and asynchronous components." In a later comment, "Don says: presentation-oriented designer tools that generate F# code are definitely feasible in the longer term."
  • weiqure
    weiqure almost 15 years
    I just read your comment on Botz3000 answer. So the answer to the original question would be that for now only the "core strengths" should be focused. I'll update the answer according to that.
  • James Hugard
    James Hugard almost 15 years
    DannyAsher over on hubFS.com took the Computation Expression (monad) linked in the answer above and applied it directly to WPF. There were a few issues, but Brian on the F# dev team helped us get it debugged (scroll to the end of the thread). See the thread here: cs.hubfs.net/forums/thread/11127.aspx
  • Brennan Vincent
    Brennan Vincent almost 13 years
    Note that you can call property setters in a C# constructor: var f = new Form { Text="My Window Title", Width=600, Height=400 };
  • Totti
    Totti about 11 years
    You're quoting Don Syme out of context. His statements were specifically about the use of UI designer tools like Expression Blend. He was not talking about GUI programming in general.
  • MasterMastic
    MasterMastic about 10 years
    Could you expand on the ways and means around that?
  • Jon Skeet
    Jon Skeet about 10 years
    @Ken: I wouldn't claim to be enough of an expert to give good advice on that front, I'm afraid.