|
|||
2. Types, Operators, and Expressions 8. Type and Constant Definitions 34. Statically Defined Tracing for User Applications |
InlinesD named constants can also be defined using inline directives, which provide a more general means of creating identifiers that are replaced by predefined values or expressions during compilation. Inline directives are a more powerful form of lexical replacement than the #define directive provided by the C preprocessor because the replacement is assigned an actual type and is performed using the compiled syntax tree and not simply a set of lexical tokens. An inline directive is specified using a declaration of the form: inline type name = expression ; where type is a type declaration of an existing type, name is any valid D identifier that is not previously defined as an inline or global variable, and expression is any valid D expression. Once the inline directive is processed, the D compiler substitutes the compiled form of expression for each subsequent instance of name in the program source. For example, the following D program would trace the string "hello" and integer value 123: inline string hello = "hello"; inline int number = 100 + 23; BEGIN { trace(hello); trace(number); } An inline name may be used anywhere a global variable of the corresponding type can be used. If the inline expression can be evaluated to an integer or string constant at compile time, then the inline name can also be used in contexts that require constant expressions, such as scalar array dimensions. The inline expression is validated for syntax errors as part of evaluating the directive. The expression result type must be compatible with the type defined by the inline, according to the same rules used for the D assignment operator (=). An inline expression may not reference the inline identifier itself: recursive definitions are not permitted. The DTrace software packages install a number of D source files in the system directory /usr/lib/dtrace that contain inline directives you can use in your D programs. For example, the signal.d library includes directives of the form: inline int SIGHUP = 1; inline int SIGINT = 2; inline int SIGQUIT = 3; ... These inline definitions provide you access to the current set of Solaris signal names described in signal(3HEAD). Similarly, the errno.d library contains inline directives for the C errno constants described in Intro(2). By default, the D compiler includes all of the provided D library files automatically so you can use these definitions in any D program. |
||
|