Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run F# code in C# - (Like C# in C# using Roslyn)

Tags:

c#

.net

roslyn

f#

Is there a way to run F# code in C#? I have written an app in C# - I'd like to provide it the ability to execute F#, read Record objects, enumerate lists from F#. What is the current solution for this? I know in the future there will be probably be a way to do this via an update to Roslyn. Also, curious how to run F# code in F#, currently. Is there a way to do that easily?

like image 967
BuddyJoe Avatar asked Mar 04 '13 20:03

BuddyJoe


2 Answers

Currently, you have to make the F# a library, and then call it from C#. Since F# is just .NET, using the F# library is (mostly) just like using any other C# library. You might want to look into FSharpX for this portion, however, as it provides some utilities to ease calling F# from C# and vice versa.

If you want to compile the F# dynamically from C#, you'll need to use the F# CodeDom implementation from the F# Power Pack. This will let you use CodeDom to compile F# on the fly, and then execute it from C#.

Note that Roslyn will not help here, even when it's released, as it won't support F# as a code model, only C# and VB.Net. It would potentially be possible to use Roslyn to manipulate and compile C# from within F#, but not the other way around.

like image 95
Reed Copsey Avatar answered Oct 07 '22 15:10

Reed Copsey


Since the question has been asked, there has been a lot of exciting development :-)

You can now do this really easily using the F# Compiler Services project:

The F# compiler services package is a component derived from the F# compiler source code that exposes additional functionality for implementing F# language bindings, additional tools based on the compiler or refactoring tools. The package also includes F# interactive service that can be used for embedding F# scripting into your applications.

like image 43
Tomas Petricek Avatar answered Oct 07 '22 16:10

Tomas Petricek