How to add condition for compiling functions?

Hey,

I have a code with multiple functions and variables in order to simulate a physical system.
I would like to be able to use the same code and just with a if(one input parameter) to choose which numerical method I use for a sub-part of my system.
The Issue is that I don’t want to declare all the arrays necessary for the different methods in my main while I will use only a part of them. But I if don’t declare all the array but use an if for the declaration, I have an error UndefVarError during compilation… Even if the value of the If will never change during the simulation …
Exemple with a simple solution (set array=nothing on condition):

v_temp = fft ? nothing : CUDA.zeros(Float64, Nx, Nz, 2)
fP = fft ? CUDA.zeros(ComplexF64, Lkx, Lkz, 2) : nothing

if fft
   solve_FFT!(..., fP, ...)
else
   solve_iter!(..., v_temp, ...)
end

I don’t know if there is a better way of doing it ? (Without creating 2 different files because 80% of the rest is the same.)

Thank you in advance for your help, I am sure it should exist an easy way :slight_smile:

There are probably better organization options at a large scale, but without knowing the whole picture a local change could be

if fft
    fP = CUDA.zeros(ComplexF64, Lkx, Lkz, 2)
    solver_specific_args = [fP]
else
    v_temp =  CUDA.zeros(Float64, Nx, Nz, 2)
    solver_specific_args = [v_temp]
end

if fft
   solve_FFT!(..., solver_specific_args..., ...)
else
   solve_iter!(..., solver_specific_args..., ...)
end