2
votes

What is the Julia equivalent of the following C code:

#ifdef _USE_NATURAL
    const scalar c=1.0;
    const scalar e=0.302822;
#else
    const scalar c=2.99792458e10;
    const scalar e=4.80320425e-10;
#endif

I need c and e to be defined at module level. They are just constants, but I want to give the user the option to choose which set of constants they want to use (which physically corresponds to different set of units; this is a physical simulation).

This is baby-easy in C due to the existence of the preprocessor but I can't seem to figure out how to change the behavior of modules on import. Is it possible?

1

1 Answers

2
votes

A macro can get you part of the way

julia> macro use_natural(t)
           if eval(t) == 1
               return esc(quote
                              const c=1.0
                              const e=0.302822
                          end)
           else
               return esc(quote
                              const c=2.99793e10
                              const e=4.803e-10
                          end)
           end
       end

julia> userchoice = 0
0

julia> @eval @use_natural $userchoice
4.803e-10

julia> c, e
(2.99793e10,4.803e-10)

However, it sound like you want userchoice to be define at import time and depending on the global namespace in another module... Not sure if that can be accomplished.