2009-12-10

F# on Mac using Cocoa

Now that Windows 7 is out, I bought a new PC with Windows 7 preloaded.  I got a Gateway SX2802-03 from Newegg.com for $525 shipped.  Of course, it only comes with Windows 7 Home and I needed Professional for Remote Desktop Connection to work.  Upgrading to Professional cost $98.50, but the process was really smooth using the built in program “Windows Activation Upgrade”.  Pro also lets you get and use the program “Windows XP Mode”.  It is essentially a Windows XP virtual machine that runs on Windows Virtual PC.

Now that I own both a Mac and a PC, I thought it would be fun to see if I can build programs for my Mac using .NET.  I loaded Visual Studio 2010 Beta 2 on my PC.  The final version of Visual Studio 2010 is due out on March 22nd and it will be shipping with a new functional .NET language called F# that I like very much.

Laurent Etiemble created a Monobjc project/library that allows access to Objective-C frameworks and libraries like Cocoa.  I just ported the first tutorial to F# in HelloFoundation.fs.  I’m able to build it on my PC and run it on my Mac.  The VS 2010 may be checked out:

svn checkout http://ctaggart.googlecode.com/svn/tags/blog-2009/2009-12-10/HelloFoundation/

The output on my Mac looks like this:

iMac:Debug ctaggart$ pwd
/Volumes/fmac/HelloFoundation/bin/Debug
iMac:Debug ctaggart$ mono HelloFoundation.exe
Hello from F#
Username: Cameron
Home Dir: /Users/ctaggart
AppKit Version: 949.54
iMac:Debug ctaggart$ mono --version
Mono JIT compiler version 2.4.3 (tarball Tue Dec  1 15:56:16 MST 2009)

In order to run it on your Mac, you will need OS X 10.5+, Mono, and F#.  I’m running OS X 10.5.8, Mono 2.4.3, and I installed the F# assemblies in Mono’s GAC (Global Assembly Cache).  Here is a script to download F# and install its main assemblies in Mono’s GAC.

What’s next?  I would like to be able to build a Cocoa UI that doesn’t use a NIB, but I’m not sure how to do this directly in Objective-C yet.  I would like to be able to access iTunes.  Is there any Mono support for the Scripting Bridge so we can do stuff like the other language examples here?