2.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/tutorial/tut-1.22-0.flx[1 /1 ]
     1: #line 2754 "./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;
End felix section to tut/tutorial/tut-1.22-0.flx[1]
Start data section to tut/tutorial/tut-1.22-0.expect[1 /1 ]
     1: 3355
End data section to tut/tutorial/tut-1.22-0.expect[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/tutorial/tut-1.22-1.flx[1 /1 ]
     1: #line 2785 "./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;
End felix section to tut/tutorial/tut-1.22-1.flx[1]
Start data section to tut/tutorial/tut-1.22-1.expect[1 /1 ]
     1: 2
End data section to tut/tutorial/tut-1.22-1.expect[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.