After messing about with F# there are some really nice features that I think I am going to miss when I HAVE to go back to C#, any clues on how I can ween myself off the following, or better still duplicate their functionality:
And last but not least the Erlang inspired Message Processing.
Use F# to make reusable libraries you can call from C#.
One very nice thing about F# is that it is still a .NET language. You can mix and match languages in the CLR as much as you'd like...
I'm not sure to what extent is this really a question. However, here are some typical patterns that I use to encode these functional constructs in C# (some of them come from my book, which has a source code available).
Discriminated unions - there is really no good way to implement discriminated unions in C# - the only thing you can do is to implement them as a class hierarchy (with a base class representing the DU type and a derived class for each of the DU case). You can also add Tag
property(of some enum
type) to the base class to make it easer to check which case the class represents. As far as I know, this is used in LINQ expression trees (which really should be discriminated union).
Pattern matching - you'll probably never get this in a fully general way (e.g. with nested patterns), but you can simulate pattern matching on discriminated unions like this (using the Option<int>
type which is either Some of int
or None
):
Option<int> value = GetOption();
int val;
if (value.TryMatchSome(out val))
Console.WriteLine("Some {0}", val);
else if (value.TryMatchNone())
Console.WriteLine("None");
Not perfect, but at least you get a relatively nice way to extract values from the cases.
Message passing - there is Concurrency and Coordination Runtime, which is in some ways also based on message passing and can be used in C#. I bet you could also use F# mailbox processor from C# using a technique based on iterators, which I described in this article and is also used in Wintellect PowerThreading library. However, I don't think anybody implemented a solid message passing library based on this idea.
In summary, you can simulate many functional features in C#, at least to some extend and use some other without any problems (lambda functions and higher-order functions). However, if you need the full power of F#, then you just need to convince your company to start using F# :-).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With