Here's a pattern I like to use a lot, that should be what you're looking 
for.  Wrap your value in a type, and have a global const of that type, with 
a getter and setter method (you can skip the getter/setter, but it makes it 
nicer).


julia> type GlobalVarHolder
           mybool::Bool
       end

julia> const GLOBALVAR = GlobalVarHolder(true)
GlobalVarHolder(true)

julia> gv() = GLOBALVAR.mybool
gv (generic function with 1 method)

julia> gv!(b::Bool) = (GLOBALVAR.mybool = b)
gv! (generic function with 1 method)

julia> f() = @show gv()
f (generic function with 1 method)

julia> f()
gv() = true
true

julia> gv!(false)
false

julia> f()
gv() = false
false




On Sunday, September 13, 2015 at 6:33:16 PM UTC-4, Seth wrote:
>
> Hi all,
>
> I'd like to track a setting throughout my module (that will cause the 
> [transparent] dispatch of either single-threaded or parallel versions of 
> many different functions). Is there a more Julian way of doing the 
> following? This seems inelegant:
>
> _parallel = false    # start off without parallelism - user calls 
> parallelize() to set/unset.
>
> function parallelize(p::Bool=true)
>     global _parallel = p
> end
>
>
> function foo(a::Int) # there will be many functions like this
>   if _parallel
>      _foo_parallel(a)
>   else
>     _foo_singlethread(a)
>   end
> end
>
>

Reply via email to