Conditional Compilation (DPL Code)

In almost every analysis, you encounter a situation in which you want to try a different formula for a calculation, you want to introduce some debugging output, or you want to add an uncertainty to see what effect it has on the final results. You can make a copy of the program and make the changes on the copy. Unfortunately, this practice results in multiple copies of your program. If you discover an error in a part of the program that is common to all alternative forms of the program, you must remember to make the same change in all versions.

Using conditional compilation, you can introduce variability into a single DPL program. The variation of the program that you wish to compile is controlled by constants or values that you set before compiling the program. Thus, you can change the values of a few constants and recompile the program, rather than creating multiple copies and compiling them.

Conditional compilation involves an if-then-else instruction for the compiler. When the compiler encounters an #if expression statement, it evaluates the expression. The expression must be either the name of a constant or variable or an exclamation point (!, logical NOT) followed by the name of a constant or variable. There should be exactly one space between #if and the expression and no space between the exclamation point and the variable name.

If the expression is true (does not equal 0), it compiles the code immediately following until it encounters a #else or #endif statement. If it finds a #else statement, it skips the code that follows the #else and searches for a #endif statement.

If the #if expression is false (equals 0), then the compiler skips the code between the #if and the #else statements and compiles the code between #else and #endif. Once the compiler finds a #endif statement, it starts compiling again. The #else statement is optional.

Conditional compilation is useful when you need two versions of a model. For example, you might have two ways to calculate a value: one that includes a lot of detail and one that is simpler. The one that includes a lot of detail takes more time to run, so you would like to be able to switch back and forth between it and the simpler way. Rather than maintain two virtually identical versions of the program, one for each formula, you could use conditional compilation. For example:

const detail = 1;
value Y ...
value Z ...
value P ...
value X =
#if detail
   Y + Z * 10;
#else
   P;
#endif

Now, if you want to use the detailed formula, you set the constant detail equal to 1 and compile the program. If you want to change to the simple formula, set the constant detail equal to 0 and recompile. Any changes you need to make to other, common, parts of the program only have to be made once. In addition, it is very clear what aspects of the program you are varying.

With conditional compilation, X is assigned one formula or the other for the entire analysis. If you want X to have one formula for one part of the analysis and the other formula for the rest of the analysis, then you should not use conditional compilation. Instead, you should use a value X that depends on the state of an event that can vary within the program, such as a chance or controlled event. The new definition of X would look like this:

chance A...
value X  | A = Y + Z * 10, P;

When A is in its first state, X will be assigned the first formula. When A is in its second state, X will be assigned the second formula. Of course, if you want, you can define A to be a controlled event and control the selection of the formula yourself in the sequence section.

In the example above, you wished to switch between alternative levels of detail. Another case in which you might switch between methods of calculating a value is in accounting for depreciation. Depending on what kind of bottom line number you need -- for example, taxes or balance sheet -- you might wish to use straight line depreciation, sum-of-the-years'-digits, or accelerated depreciation. Conditional compilation could also be useful if you wish to include display statements for debugging purposes.

You could use conditional compilation in another way if you are doing multiple analyses that vary only slightly from one another, perhaps by using very similar models of market uncertainty and cash flow but using different development decisions and strategies. By using conditional compilation, you can conduct several different analyses with the same program.

You can nest groups of conditional compilation statements. When this happens the #else statement refers to the most recent #if statement that does not yet have a corresponding #endif statement. Consider the following portion of a DPL program.

#if X
...
#if Y
...
#endif
...
#else
...
#endif

In this example the first, fourth, and fifth conditional compilation statements constitute one group, and the second and third statements constitute another. However if the statements are rearranged, the #else statement becomes part of the inner group.

#if X
...
#if Y
...
#else
...
#endif
...
#endif

In this example the first and fifth conditional compilation statements constitute one group, and the second, third, and fourth statements constitute another.

When a statement with the keyword #if followed by an expression appears in a DPL program, the DPL compiler must be able to evaluate the expression at the time it compiles the program. If the expression references a value that depends on events, DPL assumes that the events are in their first state.

When a statement with the keyword #if followed by an expression appears in a command procedure, DPL must be able to evaluate the expression at the time the statement is executed. The expression can reference a value that depends on events, but the states of the conditioning events must be known at the point where the expression occurs.

DPL has 32 global switches, called gsw1 through gsw32, that can take the values off (0) or on (1). These switches can be used in the expression following #if.

#if gsw1
   Y + Z * 10;
#else
   P;
#endif

By using these switches, you can control the conditional compilation without opening the program file. All global switches are set to 0 when a new DPL session is started.

Versions: DPL Professional, DPL Enterprise, DPL Portfolio

See Also

Compiler Directives

Writing DPL Programs