Next: Function Locking, Previous: Subfunctions, Up: Function Files

#### 12.7.3 Overloading and Autoloading

The `dispatch` function can be used to alias one function name to another. It can be used to alias all calls to a particular function name to another function, or the alias can be limited to only a particular variable type. Consider the example

         function y = spsin (x)
           printf ("Calling spsin\n");
           fflush(stdout);
           y = spfun ("sin", x);
         endfunction

         dispatch ("sin", "spsin", "sparse matrix");
         y0 = sin(eye(3));
         y1 = sin(speye(3));

Which aliases the `spsin` to `sin`, but only for real sparse matrices. Note that the builtin `sin` already correctly treats sparse matrices and so this example is only illustrative.

— Loadable Function: dispatch (f, r, type)

> Replace the function f with a dispatch so that function r is called when f is called with the first argument of the named type. If the type is any then call r if no other type matches. The original function f is accessible using `builtin (`f`, ...)`.

> If r is omitted, clear dispatch function associated with type.

> If both r and type are omitted, list dispatch functions for f.

> See also: builtin.

— Loadable Function: [...] builtin (f, ...)

> Call the base function f even if f is overloaded to some other function for the given type signature.

> See also: dispatch.

A single dynamically linked file might define several functions. However, as Octave searches for functions based on the functions filename, Octave needs a manner in which to find each of the functions in the dynamically linked file. On operating systems that support symbolic links, it is possible to create a symbolic link to the original file for each of the functions which it contains.

However, there is at least one well known operating system that doesn't support symbolic links. Making copies of the original file for each of the functions is also possible, but is undesirable as it multiples the amount of disk space used by Octave. Instead Octave supplies the `autoload` function, that permits the user to define in which file a certain function will be found.

— Built-in Function: autoload (function, file)

> Define function to autoload from file.

> The second argument, file, should be an absolute file name and should not depend on the Octave load path.

> Normally, calls to `autoload` appear in PKG_ADD script files that are evaluated when a directory is added to the Octave's load path. To avoid having to hardcode directory names in file, it is customary to use

>               autoload ("foo",
>                         fullfile (fileparts (mfilename ("fullpath")),
>                                   "bar.oct"));

> Uses like

>               autoload ("foo", file_in_loadpath ("bar.oct"))

> are strongly discouraged.

> With no arguments, return a structure containing the current autoload map.

> See also: PKG_ADD.