WeNeedAnswers
WeNeedAnswers

Reputation: 4644

Some F# Features that I would like to see in C#

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.

Upvotes: 4

Views: 461

Answers (4)

kvb
kvb

Reputation: 55184

Discriminated unions and pattern matching can be simulated in C#, although the type definitions are a bit verbose (see How can I duplicate the F# discriminated union type in C#? for some ideas). Here's the approach I advocated in that question: an F# type type T = ACase of A | BCase of B | CCase of C can be represented by a C# abstract class with some static helper methods.

public abstract class T {
    public abstract X Match<X>(Func<A,X> aCase, Func<B,X> bCase, Func<C,X> cCase);

    private class ACase : T {
        private A a;
        public ACase(A a) { this.a = a; }

        public override X Match<X>(Func<A,X> aCase, Func<B,X> bCase, Func<C,X> cCase) {
            return aCase(a);
        }
    }
    private class BCase : T {
        private B b;
        public BCase(B b) { this.b = b; }

        public override X Match<X>(Func<A,X> aCase, Func<B,X> bCase, Func<C,X> cCase) {
            return bCase(b);
        }
    }
    private class CCase : T {
        private C c;
        public CCase(C c) { this.c = c; }

        public override X Match<X>(Func<A,X> aCase, Func<B,X> bCase, Func<C,X> cCase) {
            return cCase(c);
        }
    }

    public static T MakeACase(A a) { return new ACase(a); }
    public static T MakeBCase(B b) { return new BCase(b); }
    public static T MakeCCase(C c) { return new CCase(c); }
}

Matching now looks similar to F#, but without case labels. The equivalent of this F# code:

function
| A a -> 1
| B b -> 2
| C c -> 3

Is this C# code:

public static int MatchDemo(T t) {
    return t.Match(
        a => 1,
        b => 2,
        c => 3);
}

Upvotes: 2

Brian
Brian

Reputation: 118865

Check out

switch / pattern matching idea

for some crazy ways to attempt to do pattern-matching in C#.

Upvotes: 0

Tomas Petricek
Tomas Petricek

Reputation: 243041

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# :-).

Upvotes: 2

Reed Copsey
Reed Copsey

Reputation: 564383

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...

Upvotes: 7

Related Questions