# Could I change the type of a declared variable?

**URL:** https://discourse.julialang.org/t/could-i-change-the-type-of-a-declared-variable/88933
**Category:** General Usage
**Created:** [October 18, 2022, 9:03pm UTC](https://discourse.julialang.org/t/could-i-change-the-type-of-a-declared-variable/88933 "2022-10-18T21:03:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![swish47](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swish47/32/43921_2.png) [@swish47](https://discourse.julialang.org/u/swish47)
#### Post date: [October 18, 2022, 9:03pm UTC](https://discourse.julialang.org/t/could-i-change-the-type-of-a-declared-variable/88933/1 "2022-10-18T21:03:47Z")

</div>

It is known that we cannot clear a variable after set them. However is it possible to change a decleared variable? For example:

```julia
a::Int16=1001;

```

Then what ever we set:

```julia
a=nothing

```

or

```julia
a=1.000001

```

will both receive

```julia
InexactError: Int16(1.0001)

Stacktrace:
 [1] Int16
   @ .\float.jl:767 [inlined]
 [2] convert(#unused#::Type{Int16}, x::Float64)
   @ Base .\number.jl:7
 [3] top-level scope
   @ In[6]:1
 [4] eval
   @ .\boot.jl:368 [inlined]
 [5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base .\loading.jl:1428

```

Therefore, could we change type of a, then redefine it?

---

<div class="post-metadata">

### Author: ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)
#### Post date: [October 18, 2022, 9:12pm UTC](https://discourse.julialang.org/t/could-i-change-the-type-of-a-declared-variable/88933/2 "2022-10-18T21:12:16Z")

</div>

You can’t change the type of a variable with a declared type, the compiler relies on knowing it’s type for it’s optimizations.

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [October 18, 2022, 9:49pm UTC](https://discourse.julialang.org/t/could-i-change-the-type-of-a-declared-variable/88933/3 "2022-10-18T21:49:27Z")

</div>

As @gbaraldi said, you can’t change a declared type. You can remove the declaration, and then the statements would go through. The price would be somewhat slower code when this global variable is used (the price is not so steep if this variable is not used as a global variable within functions).

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [October 18, 2022, 9:58pm UTC](https://discourse.julialang.org/t/could-i-change-the-type-of-a-declared-variable/88933/4 "2022-10-18T21:58:03Z")

</div>

If you just write

```julia
a = Int16(1001); 

```

instead, it should work fine. Writing `a::Int16=1001` means that you are promising _not_ to change the type.
