Function object in Scol » History » Revision 2
Revision 1 (iri, 09/25/2012 12:36 AM) → Revision 2/4 (iri, 09/25/2012 12:38 AM)
h1. Function object in Scol
To call a function, we write the function name followed by parameters, if any.
<pre>
fun <function_name_1> ( <args> )=
instructions
;;
fun <function_name_2> ( <args> )=
instructions;
<function_name_1> <parameters>;
instructions
;;
</pre>
However, we can use a function as an object. A such object could be useful for a subsequent operation. This is common with the reflex (callbacks) : call a function when an event occurs. You can need these objects in other cases. The function object can be passed as an argument what is important for us.
To create it, add @ before the name of function : @@<function_name_1>@
Here is a very basic example (adpated from the Scol tutoriel by Sylvain huet) :
Three ways to add two numbers.
h2. The classic way :
a function is called from another function.
<pre>
fun add (x, y)=
x+y;;
fun main ()=
_showconsole;
_fooId add 1 2; // displays 3 in the console
0;;
</pre>
h2. Using exec ... with [...] :
We create a function object. We call it with exec.
@exec <function_object> with [<arguments>]@
<pre>
fun add (x, y)=
x+y;; // displays 3 in the console
fun main ()=
_showconsole;
_fooId exec @add with [1 2]; // object created by a @ before the function name
0;;
</pre>
@exec <function_name> with <tuple>@ apply to an object some arguments and return the result. Note that the arguments are passed in a tuple. exec returns the same type than the <function_name>.
@exec @add with [1 2];@ : calculate the function in the object @add with two arguments in the tuple [1 2]
h2. Passing an object in parameter :
<pre>
fun add (x, y)=
x+y;; // displays 3 in the console
fun fooadd (x, y, objFun)=
exec objFun with [x y];;
fun main ()=
_showconsole;
_fooId fooadd 1 2 @add; // call fooadd with an function object @add as parameter
0;;
</pre>
Of course, in these different manners, you must keep a typing correct.
h2. A fourth way !
There is a fourth answer : create a node from a function from another function and an argument.
In fact, we "add" an argument to a subsequent use. A function with N-1 argument "becomes" a function with N arguments. This is commonly used in the callbacks.
To use this, we have mkfunN functions (N : 2 -> 8)
Here, we get again the previous and very basic example.
<pre>
fun add (x, y)=
x+y;; // displays 3 in the console
fun fooadd (x)=
let mkfun2 @add 1 -> foofun in // we fix the second argument to 1
exec foofun with [x];;
fun main ()=
_showconsole;
_fooId fooadd 2;;
</pre>
Now, another example using callback with this last way.
We create a window object and when the user closes it a message is displayed in the console and a value is returned.
The function called when the user closes a window has two arguments. In this example, we want three arguments, we use thus mkfun3.
<pre>
/* mkfun_parameter is the third argument added when the callback has been defined */
fun endWin (objwin, user_parameter, mkfun_parameter)=
_fooS mkfun_parameter;
user_parameter;;
fun crWin ()=
// create a simple window
let _CRwindow _channel nil 0 0 250 50 WN_NORMAL "function objects" -> window in
let "THE END" -> string in
/* define a callback when the user closes the window creating a function object.
This callback takes two arguments normally (the object window and an user parameter).
With mkfun3, we "add" a third argument */
_CBwinDestroy window mkfun3 @endWin string 0;
0;;
fun main ()=
_showconsole;
crWin;;
</pre>
Of course, this example is simply. We get the same result passing a tuple as our user_parameter ...
License : "CC-BY-SA-2.0":https://creativecommons.org/licenses/by-sa/2.0/
Tutorial by iri
Updated by /