Eval2D F# Interop

Working toward releasing a version of Eval2D, I chose for some inexplicable reason to port core parts of it to an F# library (actually, it’s because I already had a dependency on F# code, and figured I’d roll more functionality into it). My C# library is written in a somewhat functional way already, so it should be easy, right?

Of course it wasn’t. Not because of changes in method bodies, but because I wanted to maintain the same fluent function chaining library design of the C# version, available to C# code. As a result, most difficulties came from trying to make F# work it ways it really wasn’t designed for:

  • Type extension limitations: In F#, extension methods (type extensions) cannot be defined on generic types with concrete type parameters specified. For example, I can extend IEnumerable<T>, but not IEnumerable<int>. In my case, I use extension methods to allow dot notation for chaining functions together in the same way that the F# operator |> operates. Without the ability to specify concrete type parameters, many of my extension methods are unavailable.
  • FSharpFunc: Functions defined in F# (of type FSharpFunc<>) are not implicitly convertible to C# delegates, so I had to define a cast function.
  • Less concise default parameter values: It’s an additional line per parameter. Not so bad, and I’m not sure that every feature like this needs to be part of the language.

Some good points:

  • Indentation-sensitive: No curly brackets needed to identify scope. In some cases, I haven’t been sure how to indent, but I can figure it out through Intellisense.
  • Type inference: This cuts down on a lot of type specifications, but it’s occasionally tricky to trace through the code to determine why the compiler inferred a certain type.
  • Type abbreviations: I’m working with Func<> a lot, and was able to abbreviate certain Func<> types that appear often.

In the end, the core code is F# and extension methods are in a separate C# assembly. I don’t like having two assemblies like this, but I don’t see alternatives at the moment. Overall, I’m happy with the results and hope to release a version soon.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>