Monday, August 08, 2011

F# and the immediate window

(This post is inspired from this Stack Overflow question)

F# debugger integration is quite good, except for the immediate window. The immediate window requires C# style syntax, so calling F# functions is not intuitive. Below are recipes for the immediate window integrated into a console application. As always code can be found here:
// Learn how to use the immediate window for F#.
module Program=

    // To build, add  a reference to Microsoft.CSharp. This is required for dynamic support in
    // the debugger.
    let allowDynamicToWorkFromImmediateWindowByLoadingCSharpBinders = Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags.BinaryOperationLogical

    let plusOne = (+)1
    let xPlusOnes = [1;2;3] |> Seq.map plusOne
    
    []
    let main (args) = 
       printfn  "Try the following in the immediate window:
// plusOne 4
((dynamic)plusOne).Invoke(4)

// Seq.Head xPlusOnes
Microsoft.FSharp.Collections.SeqModule.Head(xPlusOnes)

// let xPlusTwos= xPlusOnes |> Seq.map plusOne 
// Seq.Head xPlusTwos
var xPlusTwos = Microsoft.FSharp.Collections.SeqModule.Map(plusOne,xPlusOnes);
Microsoft.FSharp.Collections.SeqModule.Head(xPlusTwos)"

       System.Diagnostics.Debugger.Break()
       let returnValue = 1 
       returnValue

No comments: