# Constant type but changing value

**URL:** <https://discourse.julialang.org/t/constant-type-but-changing-value/26125>\
**Category:** Performance\
**Created:** [July 8, 2019, 12:45am UTC](https://discourse.julialang.org/t/constant-type-but-changing-value/26125 "2019-07-08T00:45:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [July 8, 2019, 12:45am UTC](https://discourse.julialang.org/t/constant-type-but-changing-value/26125/1 "2019-07-08T00:45:26Z")

</div>

Is it possible to create a constant type but changing value variable? For example, I would like to create a max variable and a call to a function increments the max value and returns the new max. The max variable needs to be global so new calls to the function from different places return the right value. You can’t do this with const since the value of the constant is fixed in the function. If you do it with a regular variable, the type instability makes it slower. My workaround is to create an array but that seems less than ideal

Constant values don’t work

```julia
const maxVal3 = 10

function inc3()
    return global maxVal3 += 1
end 

```

```julia
julia> inc3()
WARNING: redefining constant maxVal3
11

julia> inc3()
11

julia> inc3()
11

```

Global regular variables are slower

```julia
maxVal = 10

function inc()
    return global maxVal += 1
end

```

```julia
julia> @btime inc()
  18.832 ns (1 allocation: 16 bytes)
500514

```

Array is my current workaround

```julia
const maxVal2 = [10]

function inc2()
    push!(maxVal2, pop!(maxVal2) + 1)
    return maxVal2[1]
end

```

```julia
julia> @btime inc2()
  10.280 ns (0 allocations: 0 bytes)
500517

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [July 8, 2019, 1:39am UTC](https://discourse.julialang.org/t/constant-type-but-changing-value/26125/2 "2019-07-08T01:39:49Z")

</div>

Don’t use a global variable, just pass in the current value and return the new value.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 8, 2019, 1:55am UTC](https://discourse.julialang.org/t/constant-type-but-changing-value/26125/3 "2019-07-08T01:55:12Z")

</div>

You can use a `Ref`, which behaves like a very lightweight zero-dimensional array holding a single value.

---

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [July 8, 2019, 4:31am UTC](https://discourse.julialang.org/t/constant-type-but-changing-value/26125/4 "2019-07-08T04:31:51Z")

</div>

@dpsanders Unfortunately, based on the setup of the code, that is not possible

@rdeits Can you show a quick example of that? I was looking at those but couldn’t figure it out

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [July 8, 2019, 4:56am UTC](https://discourse.julialang.org/t/constant-type-but-changing-value/26125/5 "2019-07-08T04:56:10Z")

</div>

```julia
const maxVal = Ref(10)

function inc()
    return maxVal[] += 1
end

```

---

<div class="post-metadata">

**Author:** ![traktofon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/traktofon/32/591_2.png) [@traktofon](https://discourse.julialang.org/u/traktofon)\
**Post date:** [July 9, 2019, 11:42am UTC](https://discourse.julialang.org/t/constant-type-but-changing-value/26125/6 "2019-07-09T11:42:01Z")

</div>

Another possibility is to define your own callable type.

Assuming that the existing code which calls the `maxval`-updating function is something like

```julia
function foo(n)
   for i=1:n 
      inc3()
   end
end

```

Define a type which holds the maxval, and overload the call operator:

```julia
mutable struct Inc3
   maxval::Int # adjust the data type as necessary
end

@inline function (f::Inc3)()
   f.maxval+=1
   nothing
end

```

Instantiate it at the beginning of your program:

```julia
const inc3 = Inc3(10) # initializing the maxval to 10

```

This lets you run the `inc3`-using code with good performance:

```julia
@btime foo(1)
  1.557 ns (0 allocations: 0 bytes)

```

and you can access the resulting maximum via `inc3.maxval`.
