Putting the fun in functional programming since 2005!
Friday, 4 May 2012
When should I use the inline keyword in F#?
The most valuable application of the inline keyword in practice is inlining higher-order functions to the call site where their function arguments are also inlined in order to produce a singly fully-optimized piece of code.
For example, the inline in the following fold function makes it 5× faster:
letinline fold f a (xs: _ )=letmutable a = a
for i=0to xs.Length-1do
a <- f a xs.[i]
Note that this bears little resemblance to what inline does in most other languages. You can achieve a similar effect using template metaprogramming in C++ but F# can also inline between compiled assemblies because inline is conveyed via .NET metadata.