It lets you use a constant global (which is much more performant than a mutable global). The const has known type so the compiler can do a better job inferring types for other variables that interact with it. See: http://julia.readthedocs.org/en/latest/manual/performance-tips/#avoid-global-variables
On Sun, Sep 13, 2015 at 10:49 PM, Seth <[email protected]> wrote: > That's interesting - but what advantage does it have over a single global > variable? > > > On Sunday, September 13, 2015 at 7:03:24 PM UTC-7, Tom Breloff wrote: >> >> 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 >>> >>>
