\Header{match.fun}{Function Verification for ``Function Variables''}
\keyword{programming}{match.fun}
\begin{Description}\relax
When called inside functions that take a function as argument, extract
the desired function object while avoiding undesired matching to
objects of other types.\end{Description}
\begin{Usage}
\begin{verbatim}
match.fun(FUN, descend = TRUE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{FUN}] item to match as function.
\item[\code{descend}] logical; control whether to search past non-function
objects.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
\code{match.fun} is not intended to be used at the top level since it
will perform matching in the \emph{parent} of the caller.

If \code{FUN} is a function, it is returned.  If it is a symbol or a
character vector of length one, it will be looked up using \code{get}
in the environment of the parent of the caller.  If it is of any other
mode, it is attempted first to get the argument to the caller as a
symbol (using \code{substitute} twice), and if that fails, an error is
declared.

If \code{descend = TRUE}, \code{match.fun} will look past non-function
objects with the given name; otherwise if \code{FUN} points to a
non-function object then an error is generated.

This is now used in base functions such as \code{\Link{apply}},
\code{\Link{lapply}}, \code{\Link{outer}}, and \code{\Link{sweep}}.\end{Details}
\begin{Value}
A function matching \code{FUN} or an error is generated.\end{Value}
\begin{Section}{Bugs}
The \code{descend} argument is a bit of misnomer and probably not
actually needed by anything.  It may go away in the future.

It is impossible to fully foolproof this.  If one \code{attach}es a
list or data frame containing a character object with the same name
of a system function, it will be used.\end{Section}
\begin{Author}\relax
Peter Dalgaard and Robert Gentleman, based on an earlier version
by Jonathan Rougier.\end{Author}
\begin{SeeAlso}\relax
\code{\Link{match.arg}}, \code{\Link{get}}\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
# Same as get("*"):
match.fun("*")
# Overwrite outer with a vector
outer <- 1:5
## Don't run: 
match.fun(outer, descend = FALSE) #-> Error:  not a function
## End Don't run
match.fun(outer) # finds it anyway
is.function(match.fun("outer")) # as well
\end{ExampleCode}
\end{Examples}

