Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the :inline keyword in Clojure functions?

Tags:

clojure

I've came across this code snippet in Clojure's source code:

(defn ==
  "Returns non-nil if nums all have the equivalent
  value (type-independent), otherwise false"
  {:inline (fn [x y] `(. clojure.lang.Numbers (equiv ~x ~y)))
   :inline-arities #{2}
   :added "1.0"}
   .....

What does :inline do?

Update: Also found good example here.

like image 201
Ertuğrul Çetin Avatar asked May 30 '17 11:05

Ertuğrul Çetin


People also ask

What is inline keyword?

The inline keyword tells the compiler to substitute the code within the function definition for every instance of a function call. Using inline functions can make your program faster because they eliminate the overhead associated with function calls.

What is the function of the __ inline?

The __inline keyword causes a function to be inlined only if you specify the optimize option. If optimize is specified, whether or not __inline is honored depends on the setting of the inline optimizer option. By default, the inline option is in effect whenever the optimizer is run.

What is inline function syntax?

The syntax for defining the function inline is: inline return-type function-name(parameters) { // function code } Remember, inlining is only a request to the compiler, not a command. Compiler can ignore the request for inlining.

What is inline and macros?

An inline function is defined by the inline keyword. Whereas the macros are defined by the #define keyword. 2. Through inline function, the class's data members can be accessed. Whereas macro can't access the class's data members.


1 Answers

An inline definition for a Clojure function gives the compiler the option of treating the operator as a macro instead of as a function. The trouble is that you have to provide a distinct macro body that need have nothing in common with the function body. You could define an inlinable version of + that actually did -!

This is highly dubious and quite undocumented. It seems to be used to plant faster code for small core functions.


By way of contrast, In C++, inline is a hint to the compiler to consider an inline expansion of any call of the function. An inline expansion is required to have the same semantics as an ordinary function call. Accessor and mutator functions are often inlined.

It is odd indeed to find an aspect of the Clojure language with soggier semantics than the corresponding C++.


I thought the feature was deprecated. It is not. It is the similar definline operator that is - not deprecated - but experimental. To be deprecated, it would have to be established first, and it has never reached that stage.


I am indebted to Alan Malloy's comments for the above corrections.

like image 145
Thumbnail Avatar answered Sep 28 '22 00:09

Thumbnail