Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 750
colinfang Avatar asked Sep 20 '12 22:09

colinfang


People also ask

What is the point of inline?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

Are member functions inline?

As a result, member functions of a local class are implicitly inline functions.

When should inline be used C++?

Inline functions are commonly used when the function definitions are small, and the functions are called several times in a program. Using inline functions saves time to transfer the control of the program from the calling function to the definition of the called function.

What is C++ inline function?

Inline function in C++ is an enhancement feature that improves the execution time and speed of the program. The main advantage of inline functions is that you can use them with C++ classes as well.


1 Answers

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.

like image 75
Gus Avatar answered Oct 11 '22 12:10

Gus