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
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>True Local Variables</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"><link rel="home" href="index.html" title="Genius Manual"><link rel="up" href="ch07.html" title="Chapter 7. Advanced Programming with GEL"><link rel="prev" href="ch07s03.html" title="Returning Functions"><link rel="next" href="ch07s05.html" title="GEL Startup Procedure"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">True Local Variables</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch07s03.html">Prev</a> </td><th width="60%" align="center">Chapter 7. Advanced Programming with GEL</th><td width="20%" align="right"> <a accesskey="n" href="ch07s05.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="genius-gel-true-local-variables"></a>True Local Variables</h2></div></div></div><p>
When passing functions into other functions, the normal scoping of
variables might be undesired. For example:
</p><pre class="programlisting">k := 10;
function r(x) = (x+k);
function f(g,x) = (
k := 5;
g(x)
);
f(r,1)
</pre><p>
you probably want the function <code class="function">r</code>
when passed as <code class="function">g</code> into <code class="function">f</code>
to see <code class="varname">k</code> as 10 rather than 5, so that
the code returns 11 and not 6. However, as written, the function
when executed will see the <code class="varname">k</code> that is
equal to 5. There are two ways to solve this. One would be
to have <code class="function">r</code> get <code class="varname">k</code> in a
private dictionary using the square bracket notation section
<a class="link" href="ch07s03.html" title="Returning Functions">Returning
Functions</a>.
</p><p>
But there is another solution. Since version 1.0.7 there are
true local variables. These are variables that are visible only
from the current context and not from any called functions.
We could define <code class="varname">k</code> as a local variable in the
function <code class="function">f</code>. To do this add a
<span class="command"><strong>local</strong></span> statement as the first statement in the
function (it must always be the first statement in the function).
You can also make any arguments be local variables as well.
That is,
</p><pre class="programlisting">function f(g,x) = (
local g,x,k;
k := 5;
g(x)
);
</pre><p>
Then the code will work as expected and prints out 11.
Note that the <span class="command"><strong>local</strong></span> statement initializes
all the referenced variables (except for function arguments) to
a <code class="constant">null</code>.
</p><p>
If all variables are to be created as locals you can just pass an
asterisk instead of a list of variables. In this case the variables
will not be initialized until they are actually set.
So the following definition of <code class="function">f</code>
will also work:
</p><pre class="programlisting">function f(g,x) = (
local *;
k := 5;
g(x)
);
</pre><p>
</p><p>
It is good practice that all functions that take other functions
as arguments use local variables. This way the passed function
does not see implementation details and get confused.
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch07s03.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch07.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch07s05.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Returning Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> GEL Startup Procedure</td></tr></table></div></body></html>
|