let my_private_data_cache = Dict()
global some_visible_function
function some_visible_function(key, val)
if haskey(my_private_data_cache, key)
println("Has key already, not changing")
else
println("Adding key ", key)
my_private_data_cache[key] = val
end
end
global show_value
show_value(key) = my_private_data_cache[key]
end # let block
some_visible_function("one", 1)
show_value("one")
some_visible_function("one", 2)
show_value("one")
my_private_data_cache["one"] = 2 # gives an error
FYI this pattern is used in quite a few places in base/ (base + grep is often
your best tool for learning Julia tricks).
Best,
--Tim
On Sunday, February 02, 2014 02:24:15 PM Brian Nguyen wrote:
> > julia> let x=1; for i=1:37; x=2*x; end; return x; end
> > 137438953472
> >
> > julia> y = let x=1; for i=1:37; x=2*x; end; x; end
> > 137438953472
> >
> > julia> y = let x=1; for i=1:37; x=2*x; end; return x; end
> > ERROR: syntax: misplaced return statement
>
> How to make let statements with return work inside other expressions?