tags:

views:

254

answers:

2

I'm trying to return a IDictionary<int,int> (created with dict tuplist) from F# to C#, but it says that I must include a reference to FSharp.Core because of System.Collections.IStructuralEquatable.

I've tried returning a Dictionary<_,_>(dict tuplist), but that doesn't make any difference.

I even tried Dictionary<_,_>(dict tuplist, HashIdentity.Reference), but that says that int is a struct...

UPDATE

OK, ME = STUPID

I just omitted to include in my question the crucial detail: which is that I was returning my dictionary in a F# record and that is the problem. Because I added the record and the Dictionary at the same time, and I saw the IStructuralEquality, I just assumed it was the dictionary which was the problem.

Doh! Sorry...

+3  A: 

I think this should work. If you don't expose any F# specific type in the public signature, than you shouldn't need to reference the FSharp.Core.dll assembly in your C# project (you'll still need to distribute it with the F# library, though).

I tried writing a simple example and I can compile the C# project without referencing FSharp.Core. Could you try if the following project works for you: http://dl.dropbox.com/u/5676796/test.zip ?

It declares a simple F# module:

module Module1
open System.Collections.Generic

let getDictionary() = 
  let d = new Dictionary<_, _>()
  d.[10] <- 1
  d :> IDictionary<int, int>

And the code that references it from C# looks like this:

var d = Module1.getDictionary();
Console.WriteLine("d 10 = " + d[10]);

Do you have any other public F# member that would expose some F# specific type (this may include things like F# records & discriminated unions, F# list, etc.)

Tomas Petricek
Thanks for your answer, see my updated question, sorry for the time wasted... but it was morphing from your sample project to mine that helped me work out where the problem was.
Benjol
Anyway, I'm glad to hear that you solved the puzzle!
Tomas Petricek
By the way, I just this minute finished RWFP. I have difficulty finding words to express just how good it is. Thanks.
Benjol
@Benjol: Thank you very much for the nice words about the book!
Tomas Petricek
+1  A: 

(I have not verified this, but I suspect...)

You are calling dict, which creates an F#-specific implementation of IDictionary that requires FSharp.Core. If instead, you use Dictionary as the concrete implementation, it will work. You can probably even do new Dictionary<_,_>(dict tupleList) and it will be ok, since that will copy the F# structure into a .NET Dictionary.

Brian
It works even if you use the `dict` function. If I write `let getDictionary() = dict [ 10, 1 ]`, it compiles just fine without a reference to FSharp.Core.dll in the C# project. The public signature of the `getDictionary` function doesn't contain anything F# specific...
Tomas Petricek
Ah, hm. The reference to IStructuralEquatable in the OP's question makes me wonder if the behavior is different on .NET 2.0 versus .NET 4.0...
Brian
I tested it on .NET 2.0 (still using some kind of RC version). I don't have .NET 4.0 installed right now to try it... (but I would be quite surprised if the reference was required if the public signature of the library didn't show any F# specific types)
Tomas Petricek
See my updated question :(
Benjol