1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Overloading and Autoloading</title>
<meta name="description" content="GNU Octave: Overloading and Autoloading">
<meta name="keywords" content="GNU Octave: Overloading and Autoloading">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Function-Files.html#Function-Files" rel="up" title="Function Files">
<link href="Function-Locking.html#Function-Locking" rel="next" title="Function Locking">
<link href="Nested-Functions.html#Nested-Functions" rel="prev" title="Nested Functions">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Overloading-and-Autoloading"></a>
<div class="header">
<p>
Next: <a href="Function-Locking.html#Function-Locking" accesskey="n" rel="next">Function Locking</a>, Previous: <a href="Nested-Functions.html#Nested-Functions" accesskey="p" rel="prev">Nested Functions</a>, Up: <a href="Function-Files.html#Function-Files" accesskey="u" rel="up">Function Files</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Overloading-and-Autoloading-1"></a>
<h4 class="subsection">11.9.5 Overloading and Autoloading</h4>
<p>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 <a href="Function-Overloading.html#Function-Overloading">Function Overloading</a>). Occasionally, however, one needs to undo
user overloading and call the default function associated with a specific
type. The <code>builtin</code> function exists for this purpose.
</p>
<a name="XREFbuiltin"></a><dl>
<dt><a name="index-builtin"></a>Built-in Function: <em>[…] =</em> <strong>builtin</strong> <em>(<var>f</var>, …)</em></dt>
<dd><p>Call the base function <var>f</var> even if <var>f</var> is overloaded to
another function for the given type signature.
</p>
<p>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.
</p>
<p>A trivial example which redefines the <code>sin</code> function to be the
<code>cos</code> function shows how <code>builtin</code> works.
</p>
<div class="example">
<pre class="example">sin (0)
⇒ 0
function y = sin (x), y = cos (x); endfunction
sin (0)
⇒ 1
builtin ("sin", 0)
⇒ 0
</pre></div>
</dd></dl>
<p>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.
</p>
<p>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
<code>autoload</code> function, that permits the user to define in which
file a certain function will be found.
</p>
<a name="XREFautoload"></a><dl>
<dt><a name="index-autoload"></a>Built-in Function: <em><var>autoload_map</var> =</em> <strong>autoload</strong> <em>()</em></dt>
<dt><a name="index-autoload-1"></a>Built-in Function: <em></em> <strong>autoload</strong> <em>(<var>function</var>, <var>file</var>)</em></dt>
<dt><a name="index-autoload-2"></a>Built-in Function: <em></em> <strong>autoload</strong> <em>(…, "remove")</em></dt>
<dd><p>Define <var>function</var> to autoload from <var>file</var>.
</p>
<p>The second argument, <var>file</var>, should be an absolute file name or
a file name in the same directory as the function or script from which
the autoload command was run. <var>file</var> <em>should not</em> depend on the
Octave load path.
</p>
<p>Normally, calls to <code>autoload</code> 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 <var>file</var>, if <var>file</var>
is in the same directory as the PKG_ADD script then
</p>
<div class="example">
<pre class="example">autoload ("foo", "bar.oct");
</pre></div>
<p>will load the function <code>foo</code> from the file <code>bar.oct</code>. The above
usage when <code>bar.oct</code> is not in the same directory, or usages such as
</p>
<div class="example">
<pre class="example">autoload ("foo", file_in_loadpath ("bar.oct"))
</pre></div>
<p>are strongly discouraged, as their behavior may be unpredictable.
</p>
<p>With no arguments, return a structure containing the current autoload map.
</p>
<p>If a third argument <code>"remove"</code> is given, the function is cleared and
not loaded anymore during the current Octave session.
</p>
<p><strong>See also:</strong> <a href="Creating-Packages.html#XREFPKG_005fADD">PKG_ADD</a>.
</p></dd></dl>
<hr>
<div class="header">
<p>
Next: <a href="Function-Locking.html#Function-Locking" accesskey="n" rel="next">Function Locking</a>, Previous: <a href="Nested-Functions.html#Nested-Functions" accesskey="p" rel="prev">Nested Functions</a>, Up: <a href="Function-Files.html#Function-Files" accesskey="u" rel="up">Function Files</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|