Search code examples
f#inlinecurryingpointfree

Was point free functions able to inline?


let inline myfunction x y = ...

let inline mycurried = myfunction x // error, only functions may be marked inline

It seems impossible to explicitly inline curried functions. So whenever mycurried is called, it won't get inlined even if myfunction is inlined properly, is it correct?

So can this be regarded as one of the drawback of curried function?


Solution

  • I think your question is whether a point-free function can be inlined or not.

    The limitation you found is not because of the curried function. Note that in your example the curried function is on the right side, on the left side you have a point-free function.

    F# only allows functions to be inline, not constants.

    I principle you may think this could be considered as a bug given that type inference is smart enough to find out that is a (point-free) function, but read the notes from Tomas regarding side-effects.

    Apparently when the compiler finds on the left side only an identifier it fails with this error:

    let inline myfunction x y = x + y
    
    let inline mycurried  = myfunction 1
    
    --> Only functions may be marked 'inline'
    

    As Brian said a workaround is adding an explicit parameter on both sides:

    let inline mycurried x  = (myfunction 1) x
    

    but then your function is no longer point-free, it's the same as:

    let inline mycurried x  = myfunction 1 x
    

    Another way might be to add an explicit generic parameter:

    let inline mycurried<'a>  = myfunction 1
    

    when generic parameters are present explicitly on the left side it compiles.

    I wish they remove the error message and turn it a warning, something like:

    Since only functions can be 'inline' this value will be compiled as a function.
    

    UPDATE

    Thanks Tomas for your answer (and your downvote).

    My personal opinion is this should be a warning, so you are aware that the semantic of your code will eventually change, but then it's up to you to decide what to do.

    You say that inline is "just an optimization" but that's not entirely true:

    . Simply turning all your functions inline does not guarantee optimal code.

    . You may want to use static constraints and then you have to use inline.

    I would like to be able to define my (kind-of) generic constants, as F# library already does (ie: GenericZero and GenericOne). I know my code will be pure, so I don't care if it is executed each time.