# var::MyType the variable keeps MyType as type and can't change further on

**URL:** <https://discourse.julialang.org/t/var-mytype-the-variable-keeps-mytype-as-type-and-cant-change-further-on/51955>\
**Category:** Internals & Design\
**Created:** [December 17, 2020, 1:02am UTC](https://discourse.julialang.org/t/var-mytype-the-variable-keeps-mytype-as-type-and-cant-change-further-on/51955 "2020-12-17T01:02:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [December 17, 2020, 1:02am UTC](https://discourse.julialang.org/t/var-mytype-the-variable-keeps-mytype-as-type-and-cant-change-further-on/51955/1 "2020-12-17T01:02:39Z")

</div>

Hey community,

I know that Julia is dynamic and variables can change type. But would it be nice to have a way of restricting that?

My ideas is:

```julia
var::Int64 = 10
var = 10.0 # throws an error 

```

```julia
var2 = 10
var2 = 10.0 # no error here

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 17, 2020, 1:25am UTC](https://discourse.julialang.org/t/var-mytype-the-variable-keeps-mytype-as-type-and-cant-change-further-on/51955/2 "2020-12-17T01:25:22Z")

</div>

That works in local scopes. There is an issue to make it work in global scope, but the dev work needs to be done. It’s been a low priority issue since 2014.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [December 17, 2020, 4:51am UTC](https://discourse.julialang.org/t/var-mytype-the-variable-keeps-mytype-as-type-and-cant-change-further-on/51955/3 "2020-12-17T04:51:11Z")

</div>

That’s precisely how `const var = 10` automatically behaves. Oh there’s something printed out if you change it to another `Int` but who reads warnings anyways. It works.

(Seriously, though, that’s why this has been such low priority — `const` covers a large swatch of potential use-cases. And conversely, `const` isn’t available in local scopes but type constants are!)

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 17, 2020, 4:57am UTC](https://discourse.julialang.org/t/var-mytype-the-variable-keeps-mytype-as-type-and-cant-change-further-on/51955/4 "2020-12-17T04:57:09Z")

</div>

That said, `const` really shouldn’t work that way (and isn’t guaranteed to). Also functions that use the `const` value already don’t work sometimes depending on what the compiler chooses to do.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 17, 2020, 10:27pm UTC](https://discourse.julialang.org/t/var-mytype-the-variable-keeps-mytype-as-type-and-cant-change-further-on/51955/5 "2020-12-17T22:27:44Z")

</div>

> [@mbauman](#):
>
> And conversely, `const` isn’t available in local scopes but type constants are!

This is also a really big annoyance for metaprogramming.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 17, 2020, 10:47pm UTC](https://discourse.julialang.org/t/var-mytype-the-variable-keeps-mytype-as-type-and-cant-change-further-on/51955/6 "2020-12-17T22:47:46Z")

</div>

> [@mbauman](#):
>
> That’s precisely how `const var = 10` automatically behaves. Oh there’s something printed out if you change it to another `Int` but who reads warnings anyways. It works.

I’d also take issue with the ‘automatically behaves’ part. It does exactly what the warning says if that’s what you’d qualify as behaving.

```julia
julia> const a = 1
1

julia> f(x) = a*x + 1
f (generic function with 1 method)

julia> f(1)
2
julia> const a = typemax(Int)
WARNING: redefinition of constant a. This may fail, cause incorrect answers, or produce other errors.
9223372036854775807

julia> f(1)
2

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [December 17, 2020, 10:51pm UTC](https://discourse.julialang.org/t/var-mytype-the-variable-keeps-mytype-as-type-and-cant-change-further-on/51955/7 "2020-12-17T22:51:41Z")

</div>

Apparently I need to be more obvious with my tongue-in-cheek sarcasm. Sorry about that. Key point was simply that `const` covers a subset of the use-cases for this functionality. As the warning states, I would highly dissuade you from using it for real mutable work. A decent combination/workaround here is a `const Ref`.

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [December 18, 2020, 8:49am UTC](https://discourse.julialang.org/t/var-mytype-the-variable-keeps-mytype-as-type-and-cant-change-further-on/51955/8 "2020-12-18T08:49:43Z")

</div>

```julia
julia> let
           var::Int = 1
           var = 1.
       end
1.0

julia> let
           var::Int = 1
           var = 1.1
       end
ERROR: InexactError: Int64(1.1)
Stacktrace:
 [1] Int64 at .\float.jl:710 [inlined]
 [2] convert(::Type{Int64}, ::Float64) at .\number.jl:7
 [3] top-level scope at REPL[9]:3

julia> let
           var::Int = 1
           var = 1.
           var
       end
1

```

That’s cool and should help static linting. But

```julia
julia> function f(i::Int)
           i = 1
           i = 1.
           i
       end
f (generic function with 1 method)

julia> f(1)
1.0

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 18, 2020, 10:37pm UTC](https://discourse.julialang.org/t/var-mytype-the-variable-keeps-mytype-as-type-and-cant-change-further-on/51955/9 "2020-12-18T22:37:19Z")

</div>

> [@Impressium](#):
>
> That’s cool and should help static linting. But
> 
> ```julia
> julia> function f(i::Int)
> i = 1
> i = 1.
> i
> end
> f (generic function with 1 method)
> 
> julia> f(1)
> 1.0
> 
> ```

The type assertions in method arguments are just for dispatch and many would feel it’d be annoying if they blocked you from changing the type of an argument later on in the body. One thing you could do is make a let block inside the function (perhaps even automate this with a macro?)

```julia
julia> function f(i::Int)
           let i::Int = i
               i = 1.0
               i
           end
       end
               
f (generic function with 1 method)

julia> f(1)
1

```

or skip the `let` entirely:

```julia
julia> function f(i::Int)
           i::Int = i
           i = 1.0
           i
       end
f (generic function with 1 method)

julia> f(1)
1

```
