# Emulate local static variable?

**URL:** https://discourse.julialang.org/t/emulate-local-static-variable/956
**Category:** General Usage
**Tags:** question
**Created:** [December 14, 2016, 9:31pm UTC](https://discourse.julialang.org/t/emulate-local-static-variable/956 "2016-12-14T21:31:27Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [December 14, 2016, 9:31pm UTC](https://discourse.julialang.org/t/emulate-local-static-variable/956/1 "2016-12-14T21:31:27Z")

</div>

Julia has no `static` keyboard. In `C`, a `static` variable inside a function has a value that persists over function calls.

One way to emulate this in Julia is with a `global` variable. This has two limitations: first, this pollutes the global scope; second, a global variable should be `const` for performance, since its type cannot be annotated. What if I want a non-const static variable?

Is there a way to emulate a local static variable in Julia?  
Local in the sense that it should be visible only inside the scope of the function that uses it.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [December 14, 2016, 9:43pm UTC](https://discourse.julialang.org/t/emulate-local-static-variable/956/2 "2016-12-14T21:43:38Z")

</div>

I’m not quite sure what `static` mean but I think it is this: A let block and a closure is the usual style. Here an example [https://github.com/JuliaLang/julia/blob/a304c557928d9eaa0a28af5fe96327d5da70d14b/base/random.jl#L1397](https://github.com/JuliaLang/julia/blob/a304c557928d9eaa0a28af5fe96327d5da70d14b/base/random.jl#L1397)

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [December 14, 2016, 9:44pm UTC](https://discourse.julialang.org/t/emulate-local-static-variable/956/3 "2016-12-14T21:44:47Z")

</div>

It’s unclear what the semantic of a static local variable in julia should be. On possible implementation with a certain semantic is

```julia
julia> macro static(expr)
           if Meta.isexpr(:(::), expr)
               T = eval(current_module(), expr.args[2])
               name = expr.args[1]
           else
               T = Any
               name = expr
           end
           :($(esc(name)) = $(Ref{T}()))
       end

```

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [December 14, 2016, 10:55pm UTC](https://discourse.julialang.org/t/emulate-local-static-variable/956/4 "2016-12-14T22:55:35Z")

</div>

Can you use this in an example?

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [December 14, 2016, 10:56pm UTC](https://discourse.julialang.org/t/emulate-local-static-variable/956/5 "2016-12-14T22:56:13Z")

</div>

@mauro3 Yes, this works. Thanks.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [December 15, 2016, 12:35am UTC](https://discourse.julialang.org/t/emulate-local-static-variable/956/6 "2016-12-15T00:35:20Z")

</div>

Or just do call overloading. This example has 2 constants which can be used inside the function:

```julia
type LotkaVolterra <: ParameterizedFunction
         a::Float64
         b::Float64
end
f = LotkaVolterra(0.0,0.0)
(p::LotkaVolterra)(t,u,du) = begin
         du[1] = p.a * u[1] - p.b * u[1]*u[2]
         du[2] = -3 * u[2] + u[1]*u[2]
end

```

The last line makes `f` a function `f(t,u,du)`, and uses the fields `a` and `b` from the type.

Since `f` is just an instance of a type, you can also change these fields outside of the function if you want. `f.a = 1`. Or you can only use them in the function, take your pick.
