Asynchronous C# and F#


ThomasP offers a view on C# from an F# perspective. I would also agree with TomasP’s comment: “I was quite surprised that F# wasn’t more visibly mentioned in the PDC talk”, and hints at more to come “there are interesting new things in F# comming out at PDC 2010”

C# vs F#: Differences (copied from TomasP’s blog)
Even though the programming model (C#) looks almost the same (as F#), there are some notable differences. I’ll write a more detailed article about some of them, but here is an assorted list of those that I found quite important:

  • Availability. This is not a techincal aspect, but it is perhaps the most important one. Asynchronous workflows in F# shipped in Visual Studio 2010 and you can use them now to write production code for a wide range of runtimes including .NET 2.0, Mono, Silverlight (see my talk!) and even Windows Phone 7. The bits released for C# are very early version and will (probably) ship with the next version of Visual Studio. My guess is that they will also require a new version of .NET framework when finally released.
  • Running or Delayed? The F# programming model creates Async which is just a specification of what should be done. We can start it in various different ways (or we may not start it) and it can be quite nicely composed. The C# programming model works with Task type, which means that async method creates a computation that is already running in background. The F# programming model definitely feels more suitable for functional (declarative) programming languages. I also think that it makes it easier to reason about what is going on.
  • Cancellation. When you want to implement a computation that can be cancelled in C#, you need to explicitly pass CancellationToken object around and check whether a cancellation was requested. In F#, this is done automatically (and checks are made each time some asynchronous operation is executed (or when a language construct with special meaning is executed). Implementing the cancellation explicitly isn’t as bad as writing everything using BeginFoo, but doing it properly is not as easy as it may sound (more about that later)
  • Waiting inside expressions. In C#, we can use await anywhere inside an expression, while in F#, let! cannot be nested. When you want to rewrite something like await Foo(await Bar()) in F#, you first have to assign the result of Bar to some local variable. This is a good thing for C#, because it makes writing of certain things easier. A slight disadvantage may be that it is not as easy to see when an asynchronous operation occurs in the expression (though it would be interesting to see this feature in F# too).

~ by mdavey on October 29, 2010.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: