Best approach for designing F# libraries for use from both F# and C#

前端 未结 4 559
后悔当初
后悔当初 2020-12-12 08:49

I am trying to design a library in F#. The library should be friendly for use from both F# and C#.

And this is where I\'m stuck a little bit. I can make it

4条回答
  •  萌比男神i
    2020-12-12 09:11

    Although it probably would be an overkill, you could consider writing an application using Mono.Cecil (it has awesome support on the mailing list) that would automate the conversion on the IL level. For example, you implement your assembly in F#, using the F#-style public API, then the tool would generate a C#-friendly wrapper over it.

    For instance, in F# you would obviously use option<'T> (None, specifically) instead of using null like in C#. Writing a wrapper generator for this scenario should be fairly easy: the wrapper method would invoke the original method: if it's return value was Some x, then return x, otherwise return null.

    You would need to handle the case when T is a value type, i.e. non-nullable; you would have to wrap the return value of the wrapper method into Nullable, which makes it a bit painful.

    Again, I'm quite certain that it would pay off to write such a tool in your scenario, maybe except if you'll be working on this such library (usable seamlessly from F# and C# both) regularly. In any case, I think it would be an interesting experiment, one that I might even explore sometime.

提交回复
热议问题