Re: [Vala] Define constant at compile time



On Tue, Nov 16, 2010 at 11:44 AM, Jan Hudec <bulb ucw cz> wrote:
On Tue, Nov 16, 2010 at 18:51:12 +0100, Günther Wutz wrote:
It is possible to use cpp as Preprocessor. I'm working on a
wrapper-program to combine cpp and give it valac to compile. The easiest
way i found actually.

Sorry. That's an extremely bad idea for most cases.

The easiest way is to tell vala to emit the symbols that will then be defined
when compiling the generated C code. That way vala will guard you from name
conflicts and the code will be more readable.

"preprocessor" is probably a bad word as it makes people think about
C-style preprocessor. If I understand the request correctly one wants
"compile-time type-safe constants substitution".

In C, the preprocessor is generally used for two things:
 - symbolic constants -- but real constants serve the purpose better and

But what if you want to change the real constant value at the
compile-time? A classical example is DEBUG vs OPTIMIZED mode. During
development I want to log of information to stdout, like this:

if (DEBUG) {
  stdout.printf("User ID is: $user");
}

and I don't want to have this code in the release version. To make it
happen one can use something like

bool DEBUG=false;

and compile the vala program with -DDEBUG (or its equivalent
-DDEBUG=true). This compiler flag changes the default value of the
const.

Type-safety means that vala compile knows that DEBUG is boolean and
complains if someone tries to use number/string here (-DDEBUG=foo) or
tries to set value for unexisting constant.

 - generic constructs -- but vala does have generics, so you don't need
  those.
Even in C++ you usually avoid the preprocessor for most things, because
preprocessor symbols cannot be scoped. Which is doubly so applicable to vala.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]