What is the Ada equivalent of #define in C++?

Viewed 264

I'm new to Ada and as far as I could explore on the internet, I was unable to find an analog to this C++ concept.

Say I have package_name.data_member (multiple variables in various packages). I'm hoping to shorten that to a more reader-friendly manner like below (without using the Use keyword) because those variables will be used multiple times in the same file -

#define A package_name.data_member
#define B package_name.data_member
...

Is there a way I can do the above in Ada?

2 Answers

In this case you need an object renaming declaration (ARM 8.5.1):

A : Data_Member_Type renames Package_Name.Data_Member;

If you’re using GNAT, it includes a tool gnatprep; the major differences from cpp are that

  • symbols to be substituted have to be marked in the source text, e.g.$foo($ isn’t in the Ada source character set),
  • substitutions can only be defined in a separate definitions file (or e.g -Dfoo=bar on the command line).

There is no exact analogue of #define (or any pre-processing) in standard Ada (although you could use a macro preprocessor if you need that), but for this use a renaming declaration should suit:

A : Atype renames package_name.data_memberA;

B : Btype renames package_name.data_memberB;

This has the advantage, over #define, that the tokens A and B are not mistakenly replaced by their #defines in unintended places.

Related