diff options
| author | Richard Stallman | 2020-01-05 17:21:41 -0500 |
|---|---|---|
| committer | Eli Zaretskii | 2020-01-11 11:48:17 +0200 |
| commit | fd8128f0c18d536cc31578b29f3fd426bbc61630 (patch) | |
| tree | c0605a95be7e8b95c6bc9a713c2cfd84f6b2b132 | |
| parent | 524441d6b3f1da298a048862d330258eebca1118 (diff) | |
| download | emacs-fd8128f0c18d536cc31578b29f3fd426bbc61630.tar.gz emacs-fd8128f0c18d536cc31578b29f3fd426bbc61630.zip | |
; Move the description of define-inline to a different node in functions.texi
| -rw-r--r-- | doc/lispref/functions.texi | 196 |
1 files changed, 100 insertions, 96 deletions
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi index 8e3e6aefb00..5cf67ba6473 100644 --- a/doc/lispref/functions.texi +++ b/doc/lispref/functions.texi | |||
| @@ -576,8 +576,9 @@ naming conventions, which are being phased out. | |||
| 576 | @cindex defining a function | 576 | @cindex defining a function |
| 577 | 577 | ||
| 578 | We usually give a name to a function when it is first created. This | 578 | We usually give a name to a function when it is first created. This |
| 579 | is called @dfn{defining a function}, and it is done with the | 579 | is called @dfn{defining a function}, and we usually do it with the |
| 580 | @code{defun} macro. | 580 | @code{defun} macro. This section also describes other ways to define |
| 581 | a function. | ||
| 581 | 582 | ||
| 582 | @defmac defun name args [doc] [declare] [interactive] body@dots{} | 583 | @defmac defun name args [doc] [declare] [interactive] body@dots{} |
| 583 | @code{defun} is the usual way to define new Lisp functions. It | 584 | @code{defun} is the usual way to define new Lisp functions. It |
| @@ -682,95 +683,8 @@ definition will have no effect on them. | |||
| 682 | and tells the Lisp compiler to perform inline expansion on it. | 683 | and tells the Lisp compiler to perform inline expansion on it. |
| 683 | @xref{Inline Functions}. | 684 | @xref{Inline Functions}. |
| 684 | 685 | ||
| 685 | Alternatively, you can define a function by providing the code which | 686 | To undefine a function name, use @code{fmakunbound}. |
| 686 | will inline it as a compiler macro. The following macros make this | 687 | @xref{Function Cells}. |
| 687 | possible. | ||
| 688 | |||
| 689 | @c FIXME: Can define-inline use the interactive spec? | ||
| 690 | @defmac define-inline name args [doc] [declare] body@dots{} | ||
| 691 | Define a function @var{name} by providing code that does its inlining, | ||
| 692 | as a compiler macro. The function will accept the argument list | ||
| 693 | @var{args} and will have the specified @var{body}. | ||
| 694 | |||
| 695 | If present, @var{doc} should be the function's documentation string | ||
| 696 | (@pxref{Function Documentation}); @var{declare}, if present, should be | ||
| 697 | a @code{declare} form (@pxref{Declare Form}) specifying the function's | ||
| 698 | metadata. | ||
| 699 | @end defmac | ||
| 700 | |||
| 701 | Functions defined via @code{define-inline} have several advantages | ||
| 702 | with respect to macros defined by @code{defsubst} or @code{defmacro}: | ||
| 703 | |||
| 704 | @itemize @minus | ||
| 705 | @item | ||
| 706 | They can be passed to @code{mapcar} (@pxref{Mapping Functions}). | ||
| 707 | |||
| 708 | @item | ||
| 709 | They are more efficient. | ||
| 710 | |||
| 711 | @item | ||
| 712 | They can be used as @dfn{place forms} to store values | ||
| 713 | (@pxref{Generalized Variables}). | ||
| 714 | |||
| 715 | @item | ||
| 716 | They behave in a more predictable way than @code{cl-defsubst} | ||
| 717 | (@pxref{Argument Lists,,, cl, Common Lisp Extensions for GNU Emacs | ||
| 718 | Lisp}). | ||
| 719 | @end itemize | ||
| 720 | |||
| 721 | Like @code{defmacro}, a function inlined with @code{define-inline} | ||
| 722 | inherits the scoping rules, either dynamic or lexical, from the call | ||
| 723 | site. @xref{Variable Scoping}. | ||
| 724 | |||
| 725 | The following macros should be used in the body of a function defined | ||
| 726 | by @code{define-inline}. | ||
| 727 | |||
| 728 | @defmac inline-quote expression | ||
| 729 | Quote @var{expression} for @code{define-inline}. This is similar to | ||
| 730 | the backquote (@pxref{Backquote}), but quotes code and accepts only | ||
| 731 | @code{,}, not @code{,@@}. | ||
| 732 | @end defmac | ||
| 733 | |||
| 734 | @defmac inline-letevals (bindings@dots{}) body@dots{} | ||
| 735 | This is similar to @code{let} (@pxref{Local Variables}): it sets up | ||
| 736 | local variables as specified by @var{bindings}, and then evaluates | ||
| 737 | @var{body} with those bindings in effect. Each element of | ||
| 738 | @var{bindings} should be either a symbol or a list of the form | ||
| 739 | @w{@code{(@var{var} @var{expr})}}; the result is to evaluate | ||
| 740 | @var{expr} and bind @var{var} to the result. The tail of | ||
| 741 | @var{bindings} can be either @code{nil} or a symbol which should hold | ||
| 742 | a list of arguments, in which case each argument is evaluated, and the | ||
| 743 | symbol is bound to the resulting list. | ||
| 744 | @end defmac | ||
| 745 | |||
| 746 | @defmac inline-const-p expression | ||
| 747 | Return non-@code{nil} if the value of @var{expression} is already | ||
| 748 | known. | ||
| 749 | @end defmac | ||
| 750 | |||
| 751 | @defmac inline-const-val expression | ||
| 752 | Return the value of @var{expression}. | ||
| 753 | @end defmac | ||
| 754 | |||
| 755 | @defmac inline-error format &rest args | ||
| 756 | Signal an error, formatting @var{args} according to @var{format}. | ||
| 757 | @end defmac | ||
| 758 | |||
| 759 | Here's an example of using @code{define-inline}: | ||
| 760 | |||
| 761 | @lisp | ||
| 762 | (define-inline myaccessor (obj) | ||
| 763 | (inline-letevals (obj) | ||
| 764 | (inline-quote (if (foo-p ,obj) (aref (cdr ,obj) 3) (aref ,obj 2))))) | ||
| 765 | @end lisp | ||
| 766 | |||
| 767 | @noindent | ||
| 768 | This is equivalent to | ||
| 769 | |||
| 770 | @lisp | ||
| 771 | (defsubst myaccessor (obj) | ||
| 772 | (if (foo-p obj) (aref (cdr obj) 3) (aref obj 2))) | ||
| 773 | @end lisp | ||
| 774 | 688 | ||
| 775 | @node Calling Functions | 689 | @node Calling Functions |
| 776 | @section Calling Functions | 690 | @section Calling Functions |
| @@ -2155,8 +2069,12 @@ this: | |||
| 2155 | An @dfn{inline function} is a function that works just like an | 2069 | An @dfn{inline function} is a function that works just like an |
| 2156 | ordinary function, except for one thing: when you byte-compile a call | 2070 | ordinary function, except for one thing: when you byte-compile a call |
| 2157 | to the function (@pxref{Byte Compilation}), the function's definition | 2071 | to the function (@pxref{Byte Compilation}), the function's definition |
| 2158 | is expanded into the caller. To define an inline function, use | 2072 | is expanded into the caller. |
| 2159 | @code{defsubst} instead of @code{defun}. | 2073 | |
| 2074 | The simple way to define an inline function, is to write | ||
| 2075 | @code{defsubst} instead of @code{defun}. The rest of the definition | ||
| 2076 | looks just the same, but using @code{defsubst} says to make it inline | ||
| 2077 | for byte compilation. | ||
| 2160 | 2078 | ||
| 2161 | @defmac defsubst name args [doc] [declare] [interactive] body@dots{} | 2079 | @defmac defsubst name args [doc] [declare] [interactive] body@dots{} |
| 2162 | This macro defines an inline function. Its syntax is exactly the same | 2080 | This macro defines an inline function. Its syntax is exactly the same |
| @@ -2194,9 +2112,95 @@ argument of an inline function is evaluated exactly once, you needn't | |||
| 2194 | worry about how many times the body uses the arguments, as you do for | 2112 | worry about how many times the body uses the arguments, as you do for |
| 2195 | macros. | 2113 | macros. |
| 2196 | 2114 | ||
| 2197 | As an alternative to @code{defsubst}, you can use | 2115 | Alternatively, you can define a function by providing the code which |
| 2198 | @code{define-inline} to define functions via their exhaustive compiler | 2116 | will inline it as a compiler macro. The following macros make this |
| 2199 | macro. @xref{Defining Functions, define-inline}. | 2117 | possible. |
| 2118 | |||
| 2119 | @c FIXME: Can define-inline use the interactive spec? | ||
| 2120 | @defmac define-inline name args [doc] [declare] body@dots{} | ||
| 2121 | Define a function @var{name} by providing code that does its inlining, | ||
| 2122 | as a compiler macro. The function will accept the argument list | ||
| 2123 | @var{args} and will have the specified @var{body}. | ||
| 2124 | |||
| 2125 | If present, @var{doc} should be the function's documentation string | ||
| 2126 | (@pxref{Function Documentation}); @var{declare}, if present, should be | ||
| 2127 | a @code{declare} form (@pxref{Declare Form}) specifying the function's | ||
| 2128 | metadata. | ||
| 2129 | @end defmac | ||
| 2130 | |||
| 2131 | Functions defined via @code{define-inline} have several advantages | ||
| 2132 | with respect to macros defined by @code{defsubst} or @code{defmacro}: | ||
| 2133 | |||
| 2134 | @itemize @minus | ||
| 2135 | @item | ||
| 2136 | They can be passed to @code{mapcar} (@pxref{Mapping Functions}). | ||
| 2137 | |||
| 2138 | @item | ||
| 2139 | They are more efficient. | ||
| 2140 | |||
| 2141 | @item | ||
| 2142 | They can be used as @dfn{place forms} to store values | ||
| 2143 | (@pxref{Generalized Variables}). | ||
| 2144 | |||
| 2145 | @item | ||
| 2146 | They behave in a more predictable way than @code{cl-defsubst} | ||
| 2147 | (@pxref{Argument Lists,,, cl, Common Lisp Extensions for GNU Emacs | ||
| 2148 | Lisp}). | ||
| 2149 | @end itemize | ||
| 2150 | |||
| 2151 | Like @code{defmacro}, a function inlined with @code{define-inline} | ||
| 2152 | inherits the scoping rules, either dynamic or lexical, from the call | ||
| 2153 | site. @xref{Variable Scoping}. | ||
| 2154 | |||
| 2155 | The following macros should be used in the body of a function defined | ||
| 2156 | by @code{define-inline}. | ||
| 2157 | |||
| 2158 | @defmac inline-quote expression | ||
| 2159 | Quote @var{expression} for @code{define-inline}. This is similar to | ||
| 2160 | the backquote (@pxref{Backquote}), but quotes code and accepts only | ||
| 2161 | @code{,}, not @code{,@@}. | ||
| 2162 | @end defmac | ||
| 2163 | |||
| 2164 | @defmac inline-letevals (bindings@dots{}) body@dots{} | ||
| 2165 | This is similar to @code{let} (@pxref{Local Variables}): it sets up | ||
| 2166 | local variables as specified by @var{bindings}, and then evaluates | ||
| 2167 | @var{body} with those bindings in effect. Each element of | ||
| 2168 | @var{bindings} should be either a symbol or a list of the form | ||
| 2169 | @w{@code{(@var{var} @var{expr})}}; the result is to evaluate | ||
| 2170 | @var{expr} and bind @var{var} to the result. The tail of | ||
| 2171 | @var{bindings} can be either @code{nil} or a symbol which should hold | ||
| 2172 | a list of arguments, in which case each argument is evaluated, and the | ||
| 2173 | symbol is bound to the resulting list. | ||
| 2174 | @end defmac | ||
| 2175 | |||
| 2176 | @defmac inline-const-p expression | ||
| 2177 | Return non-@code{nil} if the value of @var{expression} is already | ||
| 2178 | known. | ||
| 2179 | @end defmac | ||
| 2180 | |||
| 2181 | @defmac inline-const-val expression | ||
| 2182 | Return the value of @var{expression}. | ||
| 2183 | @end defmac | ||
| 2184 | |||
| 2185 | @defmac inline-error format &rest args | ||
| 2186 | Signal an error, formatting @var{args} according to @var{format}. | ||
| 2187 | @end defmac | ||
| 2188 | |||
| 2189 | Here's an example of using @code{define-inline}: | ||
| 2190 | |||
| 2191 | @lisp | ||
| 2192 | (define-inline myaccessor (obj) | ||
| 2193 | (inline-letevals (obj) | ||
| 2194 | (inline-quote (if (foo-p ,obj) (aref (cdr ,obj) 3) (aref ,obj 2))))) | ||
| 2195 | @end lisp | ||
| 2196 | |||
| 2197 | @noindent | ||
| 2198 | This is equivalent to | ||
| 2199 | |||
| 2200 | @lisp | ||
| 2201 | (defsubst myaccessor (obj) | ||
| 2202 | (if (foo-p obj) (aref (cdr obj) 3) (aref obj 2))) | ||
| 2203 | @end lisp | ||
| 2200 | 2204 | ||
| 2201 | @node Declare Form | 2205 | @node Declare Form |
| 2202 | @section The @code{declare} Form | 2206 | @section The @code{declare} Form |