set macro value depending on compilation parameters

View: New views
2 Messages — Rating Filter:   Alert me  

set macro value depending on compilation parameters

by Roberto Ostinelli :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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


Re: set macro value depending on compilation parameters

by Zoltan Lajos Kis :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org