This is done by parenthesizing each of the parameterized functions types so that they represent a
single type:
var fun1 : String - > String;
fun1 = function( p1 : String ) : String
{
// do code...
return someString;
}
var fun2 : String - > ( String - > String ) - > String;
fun2 = function( p1 : String, p2 : ( String - > String ) ) : String
{
return p2( p1 );
}
var tmp = fun2( ???someString???, fun1 );
As you can see, this can appear a little similar to the parentheses applied to the Void types of functions.
However, parenthesizing Void types of a local function only requires one type to be enclosed, while a
full function definition will always require that two or more types are enclosed.
Multi - Function Recursion
Being able to type a variable to hold a function before it is assigned allows for some very interesting
tricks. It is quite possible for two or more functions to make use of each other, and effectively create a
recursive loop that spans across multiple functions. Tying functions together in this manner can prove
very useful when parsing tree structures:
var add2 : Int - > Dynamic - > Int;
var minus1 : Int - > Dynamic - > Int;
add2 = function( p1 : Int, p2 : Dynamic ) : Int
{
p1 += 2;
trace( p1 );
if ( p1 < 5 ) p1 = p2( p1, add2 );
return p1;
}
minus1 = function( p1 : Int, p2 : Dynamic ) : Int
{
p1--;
trace( p1 );
p1 = p2( p1, minus1 );
return p1;
}
add2( 0, minus1 );
// Outputs: 2, 1, 3, 2, 4, 3, 5
Here, one function adds 2 to a given variable while the other function deducts 1 from the variable until a
certain limit is reached.
Pages:
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214