Small Delphi tricks: initialise out parameters in the caller when they are stored in local variables; a Run for TProc
Posted by jpluimers on 2020/12/29
Two small Delphi tricks:
On out
parameters
When you have out
parameters, and the caller passes local variables for them, remember that:
- the compiler then will not issue an “unitialised variable” warning any more because it expects the callee to fill that parameter
- if the local variable is non-managed, it will have a random value depending on the call stack state
A RunProc
method that executes a TProc
Every so often you want to refactor a method to use two or more different algorithm implementations.
A good start is to have some conditional, to choose the algorithm, then have anonymous methods implement each algorithm.
This quickly gives you a feel of where the local vars of the original method need to go:
- local to an implementation
- local to the original method
- parameters to the algorithm
A parameterless runner can be a good start to call these methods:
uses System.SysUtils; /... procedure RunProc(const AProc: TProc); begin AProc(); end;
You can extend this to TProc<T>
and further generic forms, by adding parameters. In that case however, the need to be inside a record (because global methods cannot be generic).
–jeroen
Leave a comment