Defines a macro.
Description
This macro preprocessor directive defines a macro. It has the form:
#define Macro[(Args)] [Value]
where
-
Macro is the name of the macro being defined. A valid macro name is an alphanumeric string.
-
Args (optional) are the one or more arguments that it accepts. These are of the form (arg1, arg2, ...). The name of each variable specifying a macro argument must begin with a percent sign. Argument values cannot include commas.
-
Value (optional) is the value being assigned to the macro, where the value can be any valid ObjectScript code. This can be something as simple as a literal or as complex as an expression.
If a macro is defined with a value, then that value replaces the macro in ObjectScript code. If a macro is defined without a value, then code can use other preprocessor directives to test for the existence of the macro and then perform actions accordingly.
You can use ##continue to continue a #define directive to the next line. You can use ##; to append a comment to a #define line. But you cannot use ##continue and ##; on the same line.
Macros with Values
Macros with values provide a mechanism for simple textual substitutions in ObjectScript code. Wherever the ObjectScript compiler encounters the invocation of a macro (in the form $$$MacroName), it replaces the value specified for the macro at the current position in ObjectScript code. The value of a macro can be any valid ObjectScript code. This includes:
Macro arguments cannot include commas. If commas are required, the #def1arg directive is available.
The following are examples of definitions for macros being used in various ways.
#define Macro1 22
#define Macro2 "Wilma"
#define Macro3 x+y
#define Macro4 $Length(x)
#define Macro5 film.Title
#define Macro6 +$h
#define Macro7 SET x = 4
#define Macro8 DO ##class(%Library.PopulateUtils).Name()
#define Macro9 READ !,"Name: ",name WRITE !,"Nice to meet you, ",name,!
#define Macro1A(%x) 22+%x
#define Macro2A(%x) "Wilma" _ ": %x"
#define Macro3A(%x) (x+y)*%x
#define Macro4A(%x) $Length(x) + $Length(%x)
#define Macro5A(%x) film.Title _ ": " _ film.%x
#define Macro6A(%x) +$h - %x
#define Macro7A(%x) SET x = 4+%x
#define Macro8A(%x) DO ##class(%Library.PopulateUtils).Name(%x)
#define Macro9A(%x) READ !,"Name: ",name WRITE !,"%x ",name,!
#define Macro9B(%x,%y) READ !,"Name: ",name WRITE !,"%x %y",name,!
Conventions for Macro Values
Though a macro can have any value, the convention is a macro is literal expression or complete executable line. For example, the following is valid ObjectScript syntax:
#define Macro7 SET x =
where the macro might be invoked with code such as:
$$$Macro7 22
which the preprocessor would expand to
SET x = 22
Though this is clearly valid ObjectScript syntax, this use of macros is discouraged.