I have a set of secondary forms that MUST check if this global variable has been declared and then, will use its value. If not declared, a certain procedure o function will be executed.You really mean "declared"? I think your program won't compile if you did not declare a required variable. You probably mean "already used". Initialize the variable with a particular value which is not used for real purposes; suppose your variable is something list FStartDate (type TDate) - initialize it to -1. Then when you access the FStartDate check for "if FStartDate <> -1 then...".
The question: {IF DECLARED x} will also work when checking in compile time the declaration of a global variable in the main unit, or it only checks it in the unit where the {$if declared x} is written? I am not sure if I explain myself clearly.
DECLARED() can only become true if the identifier is in scope using the standard Pascal scoping rules. So if you don't use a unit that identifier is not in scope. And you can't use the main program file. So in short: no, for your case it won't help you.
Could I double-check something there please: should declared() work correctly if checking a variable or procedure/function exposed by an imported unit, or is it strictly same-unit?
I presume that declared() is not intended to work with unit names, types and so on.
DECLARED() can only become true if the identifier is in scope using the standard Pascal scoping rules. So if you don't use a unit that identifier is not in scope. And you can't use the main program file. So in short: no, for your case it won't help you.
Could I double-check something there please: should declared() work correctly if checking a variable or procedure/function exposed by an imported unit, or is it strictly same-unit?
I presume that declared() is not intended to work with unit names, types and so on.
Is there any compiler-version dependency in the above?
Observations without intending to be critical: playing around, I notice that if I have a unit called Python this fails:
{$if declared(LoadVarargsRoutine) } Python.LoadVarargsRoutine('*', true); (* Try to load all varargs routines *) {$endif }
PythonDemo.lpr(86,5) Error: Evaluating a conditional compiling expression
while this is successful:
{$if declared(LoadVarargsRoutine) } Python.LoadVarargsRoutine('*', true); (* Try to load all varargs routines *) {$endif }
If instead of being a unit Python is an instance of class TPython (contained in unit python_dynamic which has been appropriately imported) then neither of these forms works:
{$if declared(TPython.LoadVarargsRoutine) } ... {$if declared(Python.LoadVarargsRoutine) }
I didn't much expect the last of those to work, but thought it worth checking the various possibilities.