| 1 | <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Skutečně lokální proměnné</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"><link rel="home" href="index.html" title="Příručka k aplikaci Genius"><link rel="up" href="ch07.html" title="Chapter 7. Pokročilé programování v jazyce GEL"><link rel="prev" href="ch07s03.html" title="Vracení funkcí"><link rel="next" href="ch07s05.html" title="Spouštěcí procedura GEL"></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">Skutečně lokální proměnné</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch07s03.html">Prev</a> </td><th width="60%" align="center">Chapter 7. Pokročilé programování v jazyce 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>Skutečně lokální proměnné</h2></div></div></div><p lang="en"> |
| 2 | When passing functions into other functions, the normal scoping of |
| 3 | variables might be undesired. For example: |
| 4 | </p><pre lang="en" class="programlisting">k := 10; |
| 5 | function r(x) = (x+k); |
| 6 | function f(g,x) = ( |
| 7 | k := 5; |
| 8 | g(x) |
| 9 | ); |
| 10 | f(r,1) |
| 11 | </pre><p lang="en"> |
| 12 | you probably want the function <code class="function">r</code> |
| 13 | when passed as <code class="function">g</code> into <code class="function">f</code> |
| 14 | to see <code class="varname">k</code> as 10 rather than 5, so that |
| 15 | the code returns 11 and not 6. However, as written, the function |
| 16 | when executed will see the <code class="varname">k</code> that is |
| 17 | equal to 5. There are two ways to solve this. One would be |
| 18 | to have <code class="function">r</code> get <code class="varname">k</code> in a |
| 19 | private dictionary using the square bracket notation section |
| 20 | <a class="link" href="ch07s03.html" title="Vracení funkcí">Returning |
| 21 | Functions</a>. |
| 22 | </p><p lang="en"> |
| 23 | But there is another solution. Since version 1.0.7 there are |
| 24 | true local variables. These are variables that are visible only |
| 25 | from the current context and not from any called functions. |
| 26 | We could define <code class="varname">k</code> as a local variable in the |
| 27 | function <code class="function">f</code>. To do this add a |
| 28 | <span class="command"><strong>local</strong></span> statement as the first statement in the |
| 29 | function (it must always be the first statement in the function). |
| 30 | You can also make any arguments be local variables as well. |
| 31 | That is, |
| 32 | </p><pre lang="en" class="programlisting">function f(g,x) = ( |
| 33 | local g,x,k; |
| 34 | k := 5; |
| 35 | g(x) |
| 36 | ); |
| 37 | </pre><p lang="en"> |
| 38 | Then the code will work as expected and prints out 11. |
| 39 | Note that the <span class="command"><strong>local</strong></span> statement initializes |
| 40 | all the referenced variables (except for function arguments) to |
| 41 | a <code class="constant">null</code>. |
| 42 | </p><p lang="en"> |
| 43 | If all variables are to be created as locals you can just pass an |
| 44 | asterisk instead of a list of variables. In this case the variables |
| 45 | will not be initialized until they are actually set. |
| 46 | So the following definition of <code class="function">f</code> |
| 47 | will also work: |
| 48 | </p><pre lang="en" class="programlisting">function f(g,x) = ( |
| 49 | local *; |
| 50 | k := 5; |
| 51 | g(x) |
| 52 | ); |
| 53 | </pre><p lang="en"> |
| 54 | </p><p>Je dobrým zvykem, aby všechny funkce, které přebírají jiné funkce jako argumenty, používaly lokální proměnné. Je to způsob, jak zabránit, aby předávaná funkce neviděla implementační podrobnosti a nemátlo ji to.</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">Vracení funkcí </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Spouštěcí procedura GEL</td></tr></table></div></body></html> |