On Saturday, November 1, 2014 9:15:43 AM UTC-7, Kapil Agarwal wrote:
>
> I need those variables as globals as otherwise I will have to pass them 
> from one function to another all the time.
>

It's generally better long-term design to pass the relevant state around 
than to have it be global. One thing that helps is to define a type that 
encapsulates the relevant state, so that you only have to pass one thing 
around between functions instead of many. E.g. if I have

f(a, b, c, d)
    mutatea!(a)
    mutateb!(b)
    mutatec!(c)
    mutated!(d)
    return g(a,b,c,d)
end

and suppose that g calls another function that also depends on a, b, c, and 
d, and so on, it can get kind of tedious to pass all the arguments 
separately.

But you can do

type algorithmState
    a::Ta
    b::Tb
    c::Tc
    d::Td
end

(where the Ta, Tb, Tc, and Td should be replaced by appropriate concrete 
types).

Then you can do

f(x::algorithmState)
    mutateState!(x)
    g(x)
end
 

Reply via email to