1.22. Lazy expressions

There is a function which is so useful, there is a special syntax for it: the lazy expression.
Start felix section to tut/examples/tut_beg108.flx[1 /1 ]
     1: #line 1087 "./lpsrc/flx_tutorial.pak"
     2: #import <flx.flxh>
     3: var x = 1;
     4: var y = 2;
     5: 
     6: val f1 = {x + y}; // lazy expression
     7: fun f2():int = { return x + y; } // equivalent
     8: 
     9: print (f1 ());
    10: print (f2 ());
    11: 
    12: x = 2; // change value of variables
    13: y = 3;
    14: 
    15: print (f1 ());
    16: print (f2 ());
    17: endl;
    18: 
End felix section to tut/examples/tut_beg108.flx[1]
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 felix section to tut/examples/tut_beg109.flx[1 /1 ]
     1: #line 1113 "./lpsrc/flx_tutorial.pak"
     2: #import <flx.flxh>
     3: val x = 1;
     4: val f = { val y = x + 1; return y; };
     5: val eol = { endl; };
     6: 
     7: print (f ()); eol;
     8: 
End felix section to tut/examples/tut_beg109.flx[1]
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.