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
const maxVal3 = 10
function inc3()
return global maxVal3 += 1
end
julia> inc3()
WARNING: redefining constant maxVal3
11
julia> inc3()
11
julia> inc3()
11
Global regular variables are slower
maxVal = 10
function inc()
return global maxVal += 1
end
julia> @btime inc()
18.832 ns (1 allocation: 16 bytes)
500514
Array is my current workaround
const maxVal2 = [10]
function inc2()
push!(maxVal2, pop!(maxVal2) + 1)
return maxVal2[1]
end
julia> @btime inc2()
10.280 ns (0 allocations: 0 bytes)
500517