- DelphiTools - https://www.delphitools.info -

Lazy parameters, $IF, compound assignments

Here is a highlight of recent changes on the SVN [1] side of DWScript [2]:

lazy parameters

Discussed [3] recently, they’re an extra parameter passing call convention, alongside of var and const. A lazy parameter passes neither the value nor the reference, but the expression. That expression is lazily evaluated [4] when the parameter is used in the procedure, and evaluated as often as it is used.

This allows to write conditional functions like the ternary operator (like Prism’s iif [5]) without involving compiler magic, logging functions where the log expression to isn’t evaluated unless the log is active like discussed there [6], or all kind of purposes like that of lightweight anonymous functions, f.i.

procedure PrintNTimes(n : Integer; lazy s: String);
   while n>0 do begin

var i : Integer = 0;
PrintNTimes(5, IntToStr(Inc(i))); // will print 12345

Implementation is still experimental, and units tests still limited, so more testing and issues reports [7] are welcome 😉
Note that lazy parameters are currently available in the language, are implicit in internal “magic” functions, but aren’t available yet for functions declared via a TdwsUnit.

$IF, Declared() and Defined()

These functions have been introduced for conditional directives, but are also accessible in the code:

$IF expressions have access to all constants defined up to the point where the $IF is located, as well as all stateless/invariant functions and operators operating on constants (f.i. you wished to, you could do hyperbolic sinus computation in a $IF, provided the parameter is from a constant expression).

Compound assignment operators

The +=, -=, *= and /= operators are now supported, but their use is at the moment only allowed on variables.
You can not use them on functions, properties, classes, or any other case where they could be made ambiguous by future language evolutions.

The limitation is introduced as they are planned to become first-class operators (as in C++), rather than just syntactical sugar or typing convenience like in C# or Freepascal [8]. They’re intended to allow mutability.