Daniel already explained how to define a C#-friendly version of the F# function that you wrote, so I’ll add some higher-level comments. First of all, you should read the F# Component Design Guidelines (referenced already by gradbot). This is a document that explains how to design F# and .NET libraries using F# and it should answer many of your questions.
When using F#, there are basically two kinds of libraries you can write:
-
F# library is designed to be used only from F#, so it’s public interface is written in a functional style (using F# function types, tuples, discriminated unions etc.)
-
.NET library is designed to be used from any .NET language (including C# and F#) and it typically follows .NET object-oriented style. This means that you’ll expose most of the functionality as classes with method (and sometimes extension methods or static methods, but mostly the code should be written in the OO design).
In your question, you’re asking how to expose function composition as a .NET library, but I think that functions like your compose
are too low level concepts from the .NET library point of view. You can expose them as methods working with Func
and Action
, but that probably isn’t how you would design a normal .NET library in the first place (perhaps you’d use the Builder pattern instead or something like that).
In some cases (i.e. when designing numerical libraries that do not really fit well with the .NET library style), it makes a good sense to design a library that mixes both F# and .NET styles in a single library. The best way to do this is to have normal F# (or normal .NET) API and then provide wrappers for natural use in the other style. The wrappers can be in a separate namespace (like MyLibrary.FSharp
and MyLibrary
).
In your example, you could leave the F# implementation in MyLibrary.FSharp
and then add .NET (C#-friendly) wrappers (similar to code that Daniel posted) in the MyLibrary
namespace as static method of some class. But again, .NET library would probably have more specific API than function composition.