11.9.5 Overloading and Autoloading
Functions can be overloaded to work with different input arguments. For example, the operator ’+’ has been overloaded in Octave to work with single, double, uint8, int32, and many other arguments. The preferred way to overload functions is through classes and object oriented programming (see Function Overloading). Occasionally, however, one needs to undo user overloading and call the default function associated with a specific type. The builtin
function exists for this purpose.
- […] = builtin (f, …)
-
Call the base function f even if f is overloaded to another function for the given type signature.
This is normally useful when doing object-oriented programming and there is a requirement to call one of Octave’s base functions rather than the overloaded one of a new class.
A trivial example which redefines the
sin
function to be thecos
function shows howbuiltin
works.sin (0) ⇒ 0 function y = sin (x), y = cos (x); endfunction sin (0) ⇒ 1 builtin ("sin", 0) ⇒ 0
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 undesirable as it increases 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.
- autoload_map = autoload ()
- autoload (function, file)
- autoload (…, "remove")
-
Define function to autoload from file.
The second argument, file, should be an absolute filename or a file name in the same directory as the function or script from which the autoload command was run. file 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 Octave’s load path. To avoid having to hardcode directory names in file, if file is in the same directory as the PKG_ADD script thenautoload ("foo", "bar.oct");
will load the function
foo
from the filebar.oct
. The above usage whenbar.oct
is not in the same directory, or usages such asautoload ("foo", file_in_loadpath ("bar.oct"))
are strongly discouraged, as their behavior may be unpredictable.
With no arguments, return a structure containing the current autoload map.
If a third argument
"remove"
is given, the function is cleared and not loaded anymore during the current Octave session.See also: PKG_ADD.
© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v5.2.0/Overloading-and-Autoloading.html