Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I invoke an F# function from the VS2010 immediate window

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.

like image 499
Maurice Flanagan Avatar asked Jul 23 '10 20:07

Maurice Flanagan


1 Answers

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).

like image 172
Tomas Petricek Avatar answered Sep 28 '22 11:09

Tomas Petricek