views:

334

answers:

1

I'm using Visual Studio 2008 with the October 2009 F# CTP installed.

I'm trying to call some F# code from my C# program. Most types of F# functions seem to work, but some are not getting initialized in F# and are throwing NullReferenceExceptions. The ones doing this are closures and partially applied functions, i.e. things that appear in C# as FastFunc<> types.

Is there something I'm doing wrong or forgetting, or is this possibly a bug with F# or .NET?

the code below is to demo the problem. I'm not actually trying to use this code in a real application. also, within F#, everything works correctly. this is an F#-to-C# problem

F#:

namespace FS      
module FunctionTypes =

    //these all work in c# as expected
    let Value = "value"

    let OneParam (str:string) = str

    let TwoParam (str:string) (str2:string) = str + " " + str2

    let Lambda =
        fun () -> "lambda"  


    //these functions are null references in C#
    // they do work as expected in F# interactive mode
    let PartialApplication = TwoParam "what's up"

    let Closure = 
        let o = new System.Object()
        fun (i:int) -> o.ToString() + i.ToString()

    let ClosureWrapper (i:int) =
        Closure i

C# (references F# project and FSharp.Core)

 //these work as expected:
        var oneParam = FS.FunctionTypes.OneParam("hey");
        var twoParam = FS.FunctionTypes.TwoParam("yeah", "you");
        var lambdaFunction = FS.FunctionTypes.Lambda();
        var value = FS.FunctionTypes.Value;
        //  in the May09 CTP, Value returned null, 
        //      so it must have been fixed in Oct09 CTP



 //these do not work--each throws a NullReferenceException.
        var partial = FS.FunctionTypes.PartialApplication.Invoke("hello");
        var closure = FS.FunctionTypes.Closure.Invoke(1);
        var closureWrapper = FS.FunctionTypes.ClosureWrapper(1);

 //  FS.FunctionTypes.Closure itself is null, 
 //  so is FS.FunctionTypes.PartialAppliction.
 //  FS.FunctionTypes.ClosureWrapper is a regular function, 
 //    but it calls Closure, which is null
+2  A: 

It works for me, i get "what's up hello", "System.Object1", "System.Object1" for partial, closure and closureWrapper vars. Are you referencing the good FSharp.Core assembly?

Stringer Bell
@Stringer Bell yeah I was referencing FSharp.Core. The problem was my FSharp project was compiling to an exe... Everything is working for me now.
dan