BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Open-source Functional Language Extensions to C# 6

Open-source Functional Language Extensions to C# 6

This item in japanese

Lang Ext, an open-source library for C# written by London-based Paul Louth, provides a set of helper functions and types that aim to "bring some of the functional world into C#" while trying to look like extensions to the language itself.

Lang Ext tries to extend C# in a few areas where using a functional style can bring some improvements to the language, says Paul:

It is great to see the direction that C# is going in, it's clearly going toward a more centrist position midway between OO and functional. But a few key things won't go away, ever. null, for example, won't ever leave the language. Mutable first won't either. And because of this I'm always trying to think of ways of removing those classes of bugs that hurt the most.

Here is a list of a few of the features of the current release of Lang Ext:

  • Better syntax for tuples: e.g., var ab = tuple("a","b");
  • Fix null references through Option.
  • One-liner lambdas: e.g. var add = fun( (int x, int y) => x + y );
  • Void as a real type: in order to support pure functions that shall always return a value (void isn't a first class value)
  • More convenient syntax for immutable lists and dictionaries, with the aim of fostering their use vs. their mutable counterparts, and list pattern matching.

When asked about the rationale for trying and extending C# instead of, e.g., using F# or Haskell, Paul explains that it is not always possible to switch away from C# in an existing project and that sort of bending C# towards a more functional style can be a reasonable approach.

Over the past few years I've done some Haskell and also we've started creating satellite projects around our core project that are developed in F#. F# has been great to work with, and I love how a few key language decisions can make whole classes of bugs disappear. It's frustrating to run back into those bugs when returning to C#.

One of the drawbacks of Lang Ext is that it is non-idiomatic, although Paul questions the true importance of being idiomatic: "A lot of C#'s idioms are inherited from Java and C# 1.0", all the while "C# as a language is becoming more and more like a functional language on every release."

Rate this Article

Adoption
Style

BT