Use of `inline` in F#

前端 未结 4 2022
無奈伤痛
無奈伤痛 2020-11-28 23:59

The inline keyword in F# seems to me to have a somewhat different purpose than what I\'m used to in e.g. C. For example, it seems to affect a function\'s type (

4条回答
  •  我在风中等你
    2020-11-29 00:57

    When should I be using inline functions?

    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:

      let inline fold f a (xs: _ []) =
         let mutable a = a
         for i=0 to xs.Length-1 do
            a <- f a xs.[i]
         a
    

    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.

提交回复
热议问题