Note: here I talk about dotnet core and not full framework! For full framework there are plenty of docs on this, but this is dotnet core.
I have an F# library created by
dotnet new --lang F#
And an ASP.NET Core RC2 C# application (created by File->New Project in VS).
Now I use my F# library from C# (I guess a very common scenario…). The F# project is referenced in the project.json as any other .net library (by listing it under dependencies)
Now all this compiles and works fine, which is great!
The only problem is that VS does not seem to support it. There is no intellisense and lines, where I use the F# functions from C# are marked as error. Debugging from C# to F# does not work either. I tried it on a Mac with VS Code, same there…
When I hit compile, the compilers figure this out and everything is fine.
This screenshot summarizes it (this is where I call an F# function from C#):
So basically I ended up with VS solution with a bunch of errors in it, which still compiles.
Here is a simple reproducer.
Questions:
Here is the code from the reproducer:
F# library - project.json (created by cli - dotnet new --lang F#
)
"version": "1.0.0-*",
"buildOptions": {
"compilerName": "fsc",
"compile": {
"includeFiles": [
"FSharpLibrary.fs"
]
}
},
"dependencies": {
"Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160316",
"NETStandard.Library": "1.5.0-rc2-24027"
},
"tools": {
"dotnet-compile-fsc": {
"version": "1.0.0-*",
"imports": [
"dnxcore50",
"portable-net45+win81",
"netstandard1.3"
]
}
},
"frameworks": {
"netstandard1.5": {
"imports": [
"portable-net45+win8",
"dnxcore50"
]
}
}
F# library, the code:
namespace FSharpLibrary
module Sample =
let public FSharpFunction number =
printfn "Hello From F#, %d" number
C# application (here I use a console app, but same things as with asp.net core)
using System;
namespace ConsoleAppCSharp
{
public class Program
{
public static void Main(string[] args)
{
FSharpLibrary.Sample.FSharpFunction(42); //<- This is red. and marked as error.
Console.ReadKey();
}
}
}
C# console application project.json
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"LibraryFSharp": "1.0.0-*",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002702"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
Thanks
So currently what I described above is all the support you have in Visual Studio 2015 with the preview tooling. Visual Studio 15 will be hopefully better, but the current Preview (Preview 5) does not have .NET Core support at all.
What is worth trying out is Visual Studio Code. In F# you get intellisense for type which are defined in C#. So for F#-C# interopability currently VS Code has much better support than the full VS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With