1.21. Lazy expressions
There is a function which is so useful, there
is a special syntax for it: the lazy expression.
Start C++ section to tut/examples/tut108.flx[1
/1
]
1: include "std";
2: var x = 1;
3: var y = 2;
4:
5: val f1 = {x + y}; // lazy expression
6: fun f2():int = { return x + y; } // equivalent
7:
8: print (f1 ());
9: print (f2 ());
10:
11: x = 2; // change value of variables
12: y = 3;
13:
14: print (f1 ());
15: print (f2 ());
16: endl;
17:
The curly brackets denote a lazy expression, it is
a function which evaluates the expression when passed
the special unit value () explained below, the return type
is the type of the expression.
You can also put statements inside curly brackets
to define a lazy function:
Start C++ section to tut/examples/tut109.flx[1
/1
]
1: include "std";
2: val x = 1;
3: val f = { val y = x + 1; return y; };
4: val eol = { endl; };
5:
6: print (f ()); eol;
7:
If there is no return statement, a block procedure is denoted,
otherwise the return type is the type of the return
statement arguments, which must all be the same.