Conditional Macros

theeepan theepan@REDACTED
Fri Aug 11 12:47:27 CEST 2006



Richard:
Even the preprocessor follows this rule, so your example does not work.

Theepan:
The PP runs even before Lexical analysis phase in many passes and I
thought PP macros are not presented to the parser?

Anyway there are workarounds for this, one is as you mentioned below.

Tx&Rx,
Theepan.



-----Original Message-----
From: Richard Carlsson [mailto:richardc@REDACTED] 
Sent: Friday, August 11, 2006 4:32 PM
To: theeepan
Cc: erlang-questions@REDACTED
Subject: Re: Conditional Macros

theeepan wrote:
> Isn't the following treatment of conditional macros applicable? I get 
> 'syntax error' on compilation
> 
> init(State) ->
>             {ok,{{one_for_one, 100, 3600},
>                  [
> -ifdef(WITH_SPEC1).
>                  Child_spec1,
> -endif.
>                  Child_spec2,
>                  Child_spec3,
>                  .
>                  ]
>             }}.

No. Each section that ends with '.' (and is followed by at least
one character of whitespace, or end of file, or a comment) is
treated separately. So the section that starts with 'init' does
not end at the '}}.', but at the 'WITH_SPEC1).', and the next
section is 'Child_spec1, -endif.', and so on. Even the
preprocessor follows this rule, so your example does not work.

You can rewrite your example to something like this:

   init(State) ->
       {ok,{{one_for_one, 100, 3600}, specs()}}.

   specs() ->
     optional_specs()
     ++ [Child_spec2,
         Child_spec3,
         .
        ].

   -ifdef(WITH_SPEC1).
   optional_specs() ->
     [Child_spec1,...].
   -else.
   optional_specs() -> [].
   -endif.


	/Richard





More information about the erlang-questions mailing list