Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling an F# Function from C#

I've looked at other instructions and still have no idea how to do this. I have two projects (Calculator in C# and Logic in F#). I added a reference to Logic in Calculator, as well as a reference to FSharp.Core

However when I add the line

float result = Logic.start(formula);

In my C# project, I get an error saying:

"The name Logic does not exist in the current context."

There is a module Logic in the logic project, so it should show up right? What am I still missing?

EDIT: Here's the function definition...

let start formula =
core (List.ofSeq formula) [] []
like image 892
SwiftCore Avatar asked Dec 08 '13 05:12

SwiftCore


1 Answers

You cannot refer from C# just an F# module without any namespace. Do something like this:

// F# project
namespace Some
module Logic =
    let start formula =
.....

or equivalent

// F# project
module Some.Logic
let start formula =
.....

and

// C# project
.....
Some.Logic.start(formula)

and reference F# project from C# project.

UPDATE: As JackP pointed out, the other alternative exists, allowing to avoid using of explicit namespace on F# side altogether.

When in C# you create a class outside of any namespace this class may be referred by prepending its name with global contextual keyword followed by :: operator, which is the way of referencing default top-level .NET namespace. F# module with a simple name outside of any namespace from reference standpoint is equivalent to one having namespace global in the very first line of code. Applying this consideration to your case you may alternatively do:

// F# definition
// namespace global is assumed
module Logic
let start formula =
.....

// C# reference
...global::Logic.start(formula)...

Gory details about global:: available on MSDN.

like image 152
Gene Belitski Avatar answered Sep 29 '22 14:09

Gene Belitski