Next: Function Locking, Previous: Nested Functions, Up: Function Files [Contents][Index]

#### 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 the `cos` function shows how `builtin` 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 then

      autoload ("foo", "bar.oct");

  will load the function `foo` from the file `bar.oct`. The above usage when `bar.oct` is not in the same directory, or usages such as

      autoload ("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.

Next: Function Locking, Previous: Nested Functions, Up: Function Files [Contents][Index]