[erlang-questions] set macro value depending on compilation parameters

Zoltan Lajos Kis kiszl@REDACTED
Sun Oct 25 02:00:08 CEST 2009


Roberto Ostinelli wrote:
> dear all,
>
> i need to define a macro to expand differently depending on a macro
> value passed at compilation time.
>
> for instance, consider the following macro declared inside a module 'test':
>
> -ifdef(foo).
> -define(FCTN, true).
> -endif.
>
> now, what i would like is that if i compile the module 'test' like this:
>
> 1>c(test, {d, foo, bar}).
> {ok, test}
>
> then inside module 'foo' the macro ?FCTN expands to true [as per the
> example above], while if i compile like this:
>
> 1>c(test, {d, foo, baz}).
> {ok, test}
>
> then inside module 'foo' the macro ?FCTN expands to false.
>
> the way i'm currently using to achieve this is:
>
> -ifdef(foo).
> -define(FCTN, case ?foo of
> 	bar ->
> 		true;
> 	baz ->
> 		false
> end).
> -endif.
>
> however, i do not want to have FCTN perform a case condition every
> time it is called, since it will ALWAYS return one of the two options
> once it is compiled. is erlc already optimizing this, ignoring the
> case condition? or is there another [and better] way to achieve this
> result?
>
> thank you for any suggestions you may have.
>
> cheers,
>
> r.
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>   
Hello,

If you only need a binary solution, you can switch between the two by 
defining or not defining foo (with an arbitrary value):

-ifdef(foo).
-define(FCTN, true).
-else.
-define(FCTN, false).
-endif.


If you need multiple values I think your best bet currently is to pass 
the desired value with the definition:

-ifdef(foo).
-define(FCTN, ?foo).
-else.
-define(FCTN, false).
-endif.

Regards,
Zoltan.


More information about the erlang-questions mailing list