# Resetting a variable to the unsigned state

**URL:** <https://discourse.julialang.org/t/resetting-a-variable-to-the-unsigned-state/34051>\
**Category:** General Usage\
**Created:** [January 31, 2020, 11:37pm UTC](https://discourse.julialang.org/t/resetting-a-variable-to-the-unsigned-state/34051 "2020-01-31T23:37:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [January 31, 2020, 11:37pm UTC](https://discourse.julialang.org/t/resetting-a-variable-to-the-unsigned-state/34051/1 "2020-01-31T23:37:36Z")

</div>

Hi,

I’m trying to convert all package global variables to _const global_, but some of them must be reset at the end (their state is checked at some code point).  
As an example

```julia
julia> a = Array{GMT.GMTcpt,1}(undef,1)
1-element Array{GMT.GMTcpt,1}:
 #undef

julia> a[1] = makecpt(range=(0,11,1));

julia> typeof(a[1])
GMT.GMTcpt

```

So far so good, but at the end of the usage loop I need to reset the variable to its initial _#undef_ state. Tried a couple variations around this but no success

```julia
julia> a[1] = Core.undef
ERROR: MethodError: Cannot `convert` an object of type UndefInitializer to an object of type GMT.GMTcpt
Closest candidates are:
  convert(::Type{T}, ::T) where T at essentials.jl:168
  GMT.GMTcpt(::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any) at C:\Users\joaqu\.julia\dev\GMT\src\gmt_main.jl:45
Stacktrace:
 [1] setindex!(::Array{GMT.GMTcpt,1}, ::UndefInitializer, ::Int64) at .\array.jl:782
 [2] top-level scope at REPL[20]:1

```

Is this resetting possible to do?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 31, 2020, 11:51pm UTC](https://discourse.julialang.org/t/resetting-a-variable-to-the-unsigned-state/34051/2 "2020-01-31T23:51:16Z")

</div>

It is not.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [February 1, 2020, 12:31am UTC](https://discourse.julialang.org/t/resetting-a-variable-to-the-unsigned-state/34051/3 "2020-02-01T00:31:04Z")

</div>

This seems a very strange way of coding. However, you can transform you vector to also hold `Nothing` and, when checking for `undef`, you can also check for `nothing`:

```julia
julia> mutable struct A
       a::Int
       end

julia> v = Vector{Union{A,Nothing}}(undef,10)
10-element Array{Union{Nothing, A},1}:
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef

julia> v[1] = A(1)
A(1)

julia> v
10-element Array{Union{Nothing, A},1}:
    A(1)
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef

julia> v[1] = nothing

julia> v
10-element Array{Union{Nothing, A},1}:
    nothing
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef

```

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 1, 2020, 12:42am UTC](https://discourse.julialang.org/t/resetting-a-variable-to-the-unsigned-state/34051/4 "2020-02-01T00:42:58Z")

</div>

Yes, it’s something around these lines that I’m considering

```julia
julia> a = Array{Any,1}(undef,1)
1-element Array{Any,1}:
 #undef

julia> a[1] = nothing

julia> a[1] = makecpt(range=(0,11,1));

julia> a[1] === nothing
false

...
julia> a[1] = nothing

julia> a[1] === nothing
true

```

What is it that you find so strange? To use _const global var_ and still be able to change _var_ I can change only the contents of _var_ but not its bindings. And at the end I need to reset the contents of _var_.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [February 1, 2020, 12:45am UTC](https://discourse.julialang.org/t/resetting-a-variable-to-the-unsigned-state/34051/5 "2020-02-01T00:45:25Z")

</div>

Using `Array{Any}` and things like that you will loose a lot of performance. What are you actually trying to accomplish?

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 1, 2020, 12:49am UTC](https://discourse.julialang.org/t/resetting-a-variable-to-the-unsigned-state/34051/6 "2020-02-01T00:49:29Z")

</div>

I know, but globals are not performant either (though those in question are not used often). What I’m trying to avoid is the doing [this](https://github.com/GenericMappingTools/GMT.jl/blob/master/src/GMT.jl#L21)  
that later may be changed to [this](https://github.com/GenericMappingTools/GMT.jl/blob/master/src/makecpt.jl#L93)  
but that at the end must become again [= nothing](https://github.com/GenericMappingTools/GMT.jl/blob/master/src/common_options.jl#L2319)

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [February 1, 2020, 1:20am UTC](https://discourse.julialang.org/t/resetting-a-variable-to-the-unsigned-state/34051/7 "2020-02-01T01:20:03Z")

</div>

I think what you are trying to do is implemented in the excellent package [OptionalData.jl](https://github.com/helgee/OptionalData.jl) created by @helgee

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 1, 2020, 1:31am UTC](https://discourse.julialang.org/t/resetting-a-variable-to-the-unsigned-state/34051/8 "2020-02-01T01:31:58Z")

</div>

Thanks. I’ll look at it.
