Conditional Macros

Richard Carlsson richardc@REDACTED
Fri Aug 11 12:31:42 CEST 2006


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