souserge
souserge

Reputation: 31

F#: Converting FSharpRef to C# object reference

I use my F# Class Library in another C# project. The function I am calling returns an object like this:

type Item =
{ Title: string
  Items: List<Item ref> }

The problem is that I when using it in C#, the Items property is of type List<FSharpRef<Item>>. So the question is:

How can I convert it to List<Item> (in F#) without loosing the references to objects?

i.e. in cases when two Item objects have a reference to a third one inside their lists, they should refer to the same object in C#.

Upvotes: 3

Views: 203

Answers (2)

souserge
souserge

Reputation: 31

Thanks to everyone who participated in the discussion! The following solutions have emerged:

  1. Dereference the values in C# (by creating some kind of a flatMap);
  2. Just not use ref at all - works fine if the values should be immutable;
  3. Use ref inside F#, but return an object of another type which doesn't require ref.

I've used the third strategy, since I needed to transform the objects to the other type anyways.

Upvotes: 0

TheQuickBrownFox
TheQuickBrownFox

Reputation: 10624

If you want the items to still be inside ref cells in C# then you can keep the F# as it is and dereference each FSharpRef in C# just before using it with its .Value property.

Upvotes: 3

Related Questions