Expr.doc: notes about function context
This commit is contained in:
parent
6e9205abc4
commit
5da5a61a42
@ -58,7 +58,7 @@ The expression context is analogous to the stack-frame of other programming lang
|
||||
|
||||
Function contexts are created by cloning the calling context. More details on this topic are given later in this document.
|
||||
|
||||
_Expr_ creates and keeps a inner _global context_ where it stores imported functions, either from builtin or plugin modules. To perform calculations, the calling program must provide its own context; this is the _main context_. All calculations take place in this context. As mentioned eralier, when a function is called, a new context is created by cloning the calling context. The created context can be called _function context_.
|
||||
_Expr_ creates and keeps a inner _global context_ where it stores imported functions, either from builtin or plugin modules. To perform calculations, the user program must provide its own context; this is the _main context_. All calculations take place in this context. As mentioned eralier, when a function is called, a new context is created by cloning the calling context. The created context can be called _function context_.
|
||||
|
||||
Imported functions are registerd in the _global context_. When an expression first calls an imported function, that function is linked to the current context; this can be the _main context_ or a _function context_.
|
||||
|
||||
@ -913,8 +913,33 @@ _param-name_ = _identifier_
|
||||
[green]`15`
|
||||
|
||||
|
||||
Functions compute values in a local context (scope) that do not make effects on the calling context. This is the normal behavior. Using the reference operator [blue]`@` it is possibile to export local definition to the calling context.
|
||||
=== Function context
|
||||
Functions compute values in a local context (scope) that do not make effects on the calling context. This is the normal behavior. Using the _clone_ modifier [blue]`@` it is possibile to export local definition to the calling context. The clone modifier must be used as prefix to variable names and it is part of the name. E.g. [blue]`@x` is not the same as [blue]`x`; they are two different and un related variables.
|
||||
|
||||
Clone variables are normal local variables. The only diffence will appear when the defining function terminate, just before the destruction of its local context. At that point, all local clone variables are cloned in the calling context with the same names but the [blue]`@` symbol.
|
||||
|
||||
.Example
|
||||
`>>>` [blue]`f = func() { @x = 3; x = 5 }` [gray]_// f() declares two *different* local variables: ``@x`` and ``x``_ +
|
||||
[green]`f():any{}` +
|
||||
`>>>` [blue]`f()` [gray]_// The multi-expression (two) in f() is calculated and the last result is returned_ +
|
||||
[green]`5` +
|
||||
`>>>` [blue]`x` [gray]_// The `x` variable was not defined in the main context before the f() invocation. It appears in the main context by cloning the `@x` variable, local to f() after its termnation._ +
|
||||
[green]`3`
|
||||
|
||||
NOTE: The clone modifier [blue]`@` does not make a variable a reference variable, as the ampersand character `&` does in languages such as C and C++.
|
||||
|
||||
[IMPORTANT]
|
||||
====
|
||||
The clone modifier can also be used to declare the formal parameters of functions, because they are local variables too.
|
||||
|
||||
.Example
|
||||
`>>>` [blue]`g = func(@p) {2+@p}`
|
||||
g(@p):any{}`
|
||||
`>>>` [blue]`g(9)`
|
||||
11`
|
||||
`>>>` [blue]`p
|
||||
9
|
||||
====
|
||||
|
||||
== Iterators
|
||||
#TODO: function calls operations#
|
||||
|
@ -576,6 +576,7 @@ pre.rouge .ss {
|
||||
<li><a href="#_expr_function_definition">6.1. <em>Expr</em> function definition</a></li>
|
||||
<li><a href="#_golang_function_definition">6.2. <em>Golang</em> function definition</a></li>
|
||||
<li><a href="#_function_calls">6.3. Function calls</a></li>
|
||||
<li><a href="#_function_context">6.4. Function context</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="#_iterators">7. Iterators</a></li>
|
||||
@ -657,7 +658,7 @@ pre.rouge .ss {
|
||||
<p>Function contexts are created by cloning the calling context. More details on this topic are given later in this document.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p><em>Expr</em> creates and keeps a inner <em>global context</em> where it stores imported functions, either from builtin or plugin modules. To perform calculations, the calling program must provide its own context; this is the <em>main context</em>. All calculations take place in this context. As mentioned eralier, when a function is called, a new context is created by cloning the calling context. The created context can be called <em>function context</em>.</p>
|
||||
<p><em>Expr</em> creates and keeps a inner <em>global context</em> where it stores imported functions, either from builtin or plugin modules. To perform calculations, the user program must provide its own context; this is the <em>main context</em>. All calculations take place in this context. As mentioned eralier, when a function is called, a new context is created by cloning the calling context. The created context can be called <em>function context</em>.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Imported functions are registerd in the <em>global context</em>. When an expression first calls an imported function, that function is linked to the current context; this can be the <em>main context</em> or a <em>function context</em>.</p>
|
||||
@ -2338,8 +2339,58 @@ These operators have a high priority, in particular higher than the operator <co
|
||||
<code>>>></code> <code class="blue">triple(5)</code><br>
|
||||
<code class="green">15</code></p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="_function_context"><a class="anchor" href="#_function_context"></a><a class="link" href="#_function_context">6.4. Function context</a></h3>
|
||||
<div class="paragraph">
|
||||
<p>Functions compute values in a local context (scope) that do not make effects on the calling context. This is the normal behavior. Using the reference operator <code class="blue">@</code> it is possibile to export local definition to the calling context.</p>
|
||||
<p>Functions compute values in a local context (scope) that do not make effects on the calling context. This is the normal behavior. Using the <em>clone</em> modifier <code class="blue">@</code> it is possibile to export local definition to the calling context. The clone modifier must be used as prefix to variable names and it is part of the name. E.g. <code class="blue">@x</code> is not the same as <code class="blue">x</code>; they are two different and un related variables.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Clone variables are normal local variables. The only diffence will appear when the defining function terminate, just before the destruction of its local context. At that point, all local clone variables are cloned in the calling context with the same names but the <code class="blue">@</code> symbol.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<div class="title">Example</div>
|
||||
<p><code>>>></code> <code class="blue">f = func() { @x = 3; x = 5 }</code> <em class="gray">// f() declares two <strong>different</strong> local variables: <code>@x</code> and <code>x</code></em><br>
|
||||
<code class="green">f():any{}</code><br>
|
||||
<code>>>></code> <code class="blue">f()</code> <em class="gray">// The multi-expression (two) in f() is calculated and the last result is returned</em><br>
|
||||
<code class="green">5</code><br>
|
||||
<code>>>></code> <code class="blue">x</code> <em class="gray">// The <code>x</code> variable was not defined in the main context before the f() invocation. It appears in the main context by cloning the <code>@x</code> variable, local to f() after its termnation.</em><br>
|
||||
<code class="green">3</code></p>
|
||||
</div>
|
||||
<div class="admonitionblock note">
|
||||
<table>
|
||||
<tr>
|
||||
<td class="icon">
|
||||
<i class="fa icon-note" title="Note"></i>
|
||||
</td>
|
||||
<td class="content">
|
||||
The clone modifier <code class="blue">@</code> does not make a variable a reference variable, as the ampersand character <code>&</code> does in languages such as C and C++.
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
<div class="admonitionblock important">
|
||||
<table>
|
||||
<tr>
|
||||
<td class="icon">
|
||||
<i class="fa icon-important" title="Important"></i>
|
||||
</td>
|
||||
<td class="content">
|
||||
<div class="paragraph">
|
||||
<p>The clone modifier can also be used to declare the formal parameters of functions, because they are local variables too.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<div class="title">Example</div>
|
||||
<p><code>>>></code> <code class="blue">g = func(@p) {2+@p}</code>
|
||||
g(@p):any{}`
|
||||
<code>>>></code> <code class="blue">g(9)</code>
|
||||
11`
|
||||
<code>>>></code> [blue]`p
|
||||
9</p>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -2381,7 +2432,7 @@ These operators have a high priority, in particular higher than the operator <co
|
||||
</div>
|
||||
<div id="footer">
|
||||
<div id="footer-text">
|
||||
Last updated 2024-09-18 20:46:46 +0200
|
||||
Last updated 2024-09-30 07:29:03 +0200
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
Loading…
Reference in New Issue
Block a user