Calling F # functions from C # and getting null reference exceptions
I am using Visual Studio 2008 with Oct 2009 FTP CTP installed.
I am trying to call some F # code from my C # program. Most of the F # function types seem to work, but some are not initialized in F # and throw NullReferenceExceptions. The ones that do this are closures and partially usable functions, i.e. things that appear in C # as FastFunc <> types.
Is there something I am doing wrong or am I forgetting, or is this possibly a bug with F # or .NET?
the code below is meant to demonstrate the problem. I am not really 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 up"
let Closure =
let o = new System.Object()
fun (i:int) -> o.ToString() + i.ToString()
let ClosureWrapper (i:int) =
Closure i
C # (F # project and FSharp.Core links)
//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
source to share