F# – inline functions and member constraints

Yan Cui

I help clients go faster for less using serverless technologies.

Generic type parameters were introduced in C# 2.0, and they gave us the ability to write code that works against any type that matches a set of constraints and remove the need to create type-specific overloads, e.g.:

image

A few years passed, and dynamic types was introduced in C# 4, which allows us to bypass compile-time type checking (type checking still happens at runtime) when we use the dynamic keyword, e.g.:

image

this code works so long as the ‘+’ operator is defined for the runtime types of obj1 and obj2, otherwise, you’ll get a RuntimeBinderException when this code is executed. This kind of type checking is often referred to as Duck typing.

Duck typing is a very powerful language feature but it also makes your code less safe, as you now face the possibility of having runtime type errors that would have otherwise been caught by the compiler.

It’s a damn shame that there’s no way for you to combine the powers of generics and dynamic typing.. because if you could, you’d be able to write DoSomethingElse like this instead:

public void DoSomethingElse<T, U, V>(T obj1, U obj2) where T + U => V

{

    var obj3 = obj1 + obj2;

    Console.WriteLine(obj3);

}

which stops you from mistakenly trying to invoke the method with an int and a List<int> at compile time, and saves you 10 mins of debugging and bug fixing time.

Well, would it blow your mind if I tell you that such feature already exists in the .Net framework today? No, I kid you not, member constraints in F# lets you do just that!

Member Constraints

In F#, you can specify a statically resolved type parameter by prefixing it with the caret symbol ( ^ ), e.g. ^T. These type parameters can be used with member constraints which allows you to specify that a type argument must have a particular member or members in order to be used.

Take the following F# code for instance:

image

It’s equivalent to the earlier C# version of DoSomethingElse, but this function has the following signature:

image

which says that the types ^a and ^b used in the function must have a static member ‘+’ defined on either one of them that can be used on instances of these two types.

You can explicitly state member constraints too, for instance:

image

Member constraints are very useful in those tight spots where you find yourself wrestling with the .Net type system, it gives you the flexibility of duck typing but also the safety of static typing, so really it gives you the best of both worlds!

Restrictions

However, useful as it may be, there are some restrictions to when you can use member constraints.

First, they cannot be used with generic type parameters, they can only be used with statically resolved type parameters.

Secondly, they can only be used with functions or methods that are inline.

Inline functions

Which brings us to the topic of inline functions, which as the name suggests, creates the functions ‘inline’.

But what does that mean? It means that, at every call site to an inlined function or method, the concrete types for the statically resolved type parameters (e.g. ^T, ^U) are resolved and the specific code for those types are inserted at the call site.

This has the obvious implication that the compiler will generate many duplicated IL code for inline functions and it increases the size of your assembly.

By contrast, a normal function or method will generate only one instance of that function or method and every call site makes a ‘jump’ to the location of that function or method in memory to execute it. The inlining behaviour also has some performance implications which we will go through shortly.

Restrictions

The one major restriction on inline functions is that an inlined function cannot be overloaded.

When to use inline functions

As with most powerful features, such as duck typing and inline functions, they are often open to abuse.. Personally, I think it’s important for one to think carefully about what it is that they’re trying to do before deciding to use the latest and greatest language features just for the sake of it.

Take the last code snippet for example, there’s no reason why I can’t solve this problem using interfaces and generic type parameters – define an ISpeaker interface with a Speak member and requiring the parameter x to be an instance of ISpeaker.

Doing so will achieve the same end goal, there will be a stronger contract (a well understood, shared interface) and the code will be cleaner and easier to read:

image

As far as I’m aware, there are really only two main reasons for using inline functions:

Generic Arithmetic

This is the problem inline functions were designed to solve, and a problem that can’t be solved by using interfaces and generics because there’s no way to specific constraints on operators without using member constraints (and therefore inline functions).

Take this simple add function for example:

image

without making it inline the parameters a and b will be inferred to be of type int, and there’s no way to make this function generic and usable with every numeric type (int32, int64, uint32, uint64, float, etc.). This is a prime example of when you should inline a function, doing so solves the aforementioned problem:

image

Performance Optimization

In some cases, it’s possible to get a performance improvement by making a function inline as it removes the jumps from call sites to the function’s location in memory.

Jon Harrop gave this example of how by making the following fold function inline it can run 5x faster:

image

image

As you can see, in my test, the inline version managed to run a whopping 8.5x faster!

This might just be too much temptation to those of us constantly seeking better performance from our apps, but it’s important to keep a couple of things in mind too:

  • making a function or method inline does not always guarantee a performance improvement
  • overuse can add significant bloat and makes your assembly bigger and takes longer to load, hence potentially impeding overall performance
  • always measure/profile your app first, don’t prematurely optimize

Further readings

MSDN – Statically resolved type parameters (preceded by a caret ^ symbol)

MSDN – Inline functions

Statically typed duck typing in F#

Whenever you’re ready, here are 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop to level up your serverless skills quickly.
  2. Do you want to know how to test serverless architectures with a fast dev & test loop? Check out my latest course, Testing Serverless Architectures and learn the smart way to test serverless.
  3. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.

5 thoughts on “F# – inline functions and member constraints”

  1. Pingback: F# – Simple QuickSort implementation | theburningmonk.com

  2. Pingback: Why I like Go’s interfaces | theburningmonk.com

  3. Might another use case for statically-resolved member type parameters be when you don’t control or own at least one of the types?

Leave a Comment

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