I cannot figure out how I could use IfThen in this case. Maybe you can suggest some way?
Well, the idea is that the generic ifthen can take functional and procedural parameters to allow different execution paths.
That is how I designed it.
Afaik Michael has also added an example to the documentation for that based on my own example when I submitted that feature.
It may take a little redesign of your code, but to me it looks possible.
The signature is like this:
generic function IfThen<T>(val:boolean;const iftrue:T; const iffalse:T) :T; inline; overload;
Where T can be anything, including procedural/functional types.
As usual, what Sven suggested gives even more options if implemented.