12

I just want to point out that this is question is not the reverse of

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

Here I'm not asking how to design a functional library written C# to be used in both worlds.

I'd like to know good practices on what design choices embrace or avoid to get a reasonable compromise for make this library usable from F#.

Practices like (for example):

  • keep object hierarchy as simple as possible

  • avoid mutating state of objects, but return new ones

  • etc...

Anyone that already done it, can share it's experience?

Side note

It's interesting note this OSS project, IronJS. Yes, it's written in F#, but the author expose two specialized host IronJS.Hosting.FSharp and IronJS.Hosting.CSharp.

Community
  • 1
  • 1
jay
  • 1,510
  • 2
  • 11
  • 19
  • I use c# library from fsharp, and never gave a deep thought at this. The few mismatch I encountered were the difference between Func and FSharpFunc, and the lack of option types. – nicolas Feb 27 '13 at 14:14

2 Answers2

4

Interop with existing .NET libraries was a major design goal of F#, so there aren't any constraints on the libraries to be consumed.

That said, because of F#'s stricter typing, there are some patterns that result in slightly clunkier code. The builder pattern is one.

var bldr = new StringBuilder();
bldr.Append("abc"); //ignoring return value

vs.

bldr.Append("abc") |> ignore //must be explicitly ignored

But this is easily worked around using an extension method or let-bound function. Bottom line: interop is one of F#'s strengths and greatest achievements.

Daniel
  • 47,404
  • 11
  • 101
  • 179
  • +1 for example, @Daniel; when you talk about extension method, you mean something that returns ``void/Unit``, a way to avoid pipelining to ``ignore``? – jay Feb 27 '13 at 19:15
4

Imagine one day you would like to rewrite your C# library in F# for better usability. Here are the paths you are likely to take:

enter image description here

I focus on the path "Imperative C# --> Functional C# --> Functional F# --> Idiomatic F#". The more functional your C# library is, the more usable your library is in F#. Functional style helps increase composability and is closer to idiomatic F# code. Along these lines, you can:

  • Embrace immutability by default principle. If you don't know whether you need to update a field/property later, just mark it readonly first.
  • Follow expression-based and declarative programming style. LINQ operations are good examples.
  • Use immutable collections or mutable collections in an immutable way. With the introduction of C# immutable collections, it should be easier than ever before.

The picture above is taken from F# for fun and profit's Porting from C# to F# series. They are very helpful; knowing how C# concepts are expressed in F# will improve usability of your library.

It's hard to avoid C#'s object-oriented features. Remember that F# type inference doesn't work very well with these features. Along the line of keeping object hierarchy simple, you should reduce number of member overloads. A big number of member overloads will easily confuse F# type checker. Moreover, it doesn't hurt to distribute a thin F# wrapper with your C# library. Certain things you need to do are turning some methods into module functions and creatingActive Patterns to decompose object hierarchy.

pad
  • 41,040
  • 7
  • 92
  • 166
  • +1 @pad; optimal confirmations and excellent further explanations; this answers exactly my question, that could be also expressed as: 'how to model C# code in a F# friendly manner?' articles supplied completes the picture. concluding, we can add that _immutability_ is a central topic because it 'keeps away' side effects. – jay Feb 27 '13 at 19:12
  • The whole http://fsharpforfunandprofit.com site is an excellent resource, I only discovered it yesterday. – Benjol Feb 28 '13 at 06:35
  • @pad, really excellent (I didn't know it)! I've found here also [this question](http://stackoverflow.com/questions/1817248/patterns-to-mix-f-and-c-sharp-in-the-same-solution) in same way connected to this topic (posted for reference). – jay Feb 28 '13 at 08:44
  • @Benjol: Yes, it is. I'm often surprised how well-written the site is. Many SO questions can be answered by citing their articles only. – pad Feb 28 '13 at 12:25