Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing an F# list from inside C# code

Tags:

c#

types

list

f#

I have written an F# module that has a list inside:

module MyModule
type X = 
    {
        valuex : float32
    }
let l = [ for i in 1 .. 10 -> {valuex =  3.3f}]

Now from a C# class I'm trying to access the previously defined list, but I don't know how converting it:

... list = MyModule.l ; //here's my problem

I'd need something like:

IList<X> list = MyModule.l;

How can I achieve that?

like image 256
Heisenbug Avatar asked Nov 23 '11 16:11

Heisenbug


People also ask

Does downloading Facebook data include deleted messages?

You won't find information or content that you deleted because we delete that content from our servers. Keep in mind: The categories of data we receive, collect, and save may change over time. Learn more about your Facebook data in our Privacy Policy.


1 Answers

As simple as:

IList<MyModule.X> list = MyModule.l.ToList();

The reason you need the conversion method rather than a cast / implicit conversion is because an FSharpList<T> implements IEnumerable<T> but not IList<T> since it represents an immutable linked-list.

Note that you'll have to include FSharp.Core as a reference in your C# project.

like image 124
Ani Avatar answered Sep 30 '22 16:09

Ani