views:

75

answers:

1

While debugging an F# application, I would like to be able to invoke an F# method from the VS2010 immediate window but it doesn't seem to work. The problem appears to be that F# methods are actually FSharpFunc objects. I tried using the "Invoke" method but the interactive window doesn't recognize it.

+3  A: 

The F# integration for Visual Studio doesn't support F# expressions in immediate window (or watches), so the only option is to write C# code corresponding to the compiled representation that F# uses. I tried doing that and I'm having the same issue as you described - the Invoke method appears to be there (in Reflector), but Visual Studio doesn't want to call it directly. I tried it using the following example:

let foo f = 
  let n = f 1 2 // Breakpoint here
  n + 1

However, there are other ways to call the function. In this case, the actual code generated by the F# compiler is a call to InvokeFast method. If you type the following to the immediate window, it works:

Microsoft.FSharp.Core.FSharpFunc<int, int>.InvokeFast<int>(f, 1, 2)  |

It also appears that you can call the usual Invoke method using dynamic from C# 4.0 (proving that the method is actually there!):

((dynamic)f).Invoke(1, 2)

This works only if you add reference to Microsoft.CSharp.dll (and use some type defined in the assembly somewhere in your code - e.g. as an annotation - so that it gets loaded).

Tomas Petricek
Ok, this worked!I tried the static InvokeFast but I didn't realize I needed to force the load of Microsoft.CSharp. Using dynamic is pretty clever ;)
Mo Flanagan
Just to clarify - the `InvokeFast` approach should work without `Microsoft.CSharp.dll` (though maybe something needs to be done so that the immediate window loads `FSharp.Core.dll`). The C# library is needed only for `dynamic`.
Tomas Petricek