Saturday, April 03, 2010

F#ing Around

In my last post, I wrote a tiny standalone F# program that would print out a reversed string. It wasn't enough, I wanted to call the function from C#. Difficult? Not at all, but there are a couple of tricks that might catch you at first.
1) Add a new C# project to the solution
2) Add a new assembly reference to the F# project
3) One of two (or is that three?) things:
a) did you name your F# module (a declaration at the top of the .fs file like module MyFirstGreeting? If yes, you will reference the function from C# by it's fully qualified name: MyFirstGreeting.reverse(...)
b) do you have an unnamed F# module? If yes, it's assumed the name of the .fs file, most likely Program, so you'll need to invoke the function by calling Program.reverse(...)
c) if you answered yes to b) and the class in your C# project was also called Program, you are waiting for this: use the global namespace qualifier! global::Program.reverse(...). Remember your C# Program's fully qualified class name is actually something like ConsoleApplication2.Program.

Compile and run! Happy days! And marvel in the wonder of Visual Studio 2010 as you step through the code from C# to F# (the call stack window even shows what language your current stack frame is written in!). This is going to be a fun journey...

No comments: