# Global Type-Locking

**URL:** <https://discourse.julialang.org/t/global-type-locking/15620>\
**Category:** Internals & Design\
**Created:** [September 28, 2018, 3:27pm UTC](https://discourse.julialang.org/t/global-type-locking/15620 "2018-09-28T15:27:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [September 28, 2018, 3:27pm UTC](https://discourse.julialang.org/t/global-type-locking/15620/1 "2018-09-28T15:27:01Z")

</div>

[https://github.com/JuliaLang/julia/issues/8870](https://github.com/JuliaLang/julia/issues/8870)

I just ran (forgetfully) into the global performance issue. after it has bitten me often enough, I will probably remember it.

I cannot use a const, because I need to change the value. But I do not need to change the type. If I could just tell julia `constT` that the type is constant, I am guessing that the compiler could generate better code, and my code would be less buggy, too.

Is there a plan to create a mechanism that allows fixed typing of a global?

---

<div class="post-metadata">

**Author:** ![jrevels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jrevels/32/10393_2.png) [@jrevels](https://discourse.julialang.org/u/jrevels)\
**Post date:** [September 28, 2018, 4:07pm UTC](https://discourse.julialang.org/t/global-type-locking/15620/2 "2018-09-28T16:07:29Z")

</div>

This can already be done by using a `const` binding to mutable (but well-typed) state:

```julia
julia> const MY_CONST = Ref{Int}()
Base.RefValue{Int64}(4545197904)

julia> MY_CONST[] = 1
1

julia> MY_CONST[]
1

julia> MY_CONST[] = 2
2

julia> MY_CONST[]
2

```

This should be as performant as any built-in implementation could possibly be, AFAIU.

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [September 28, 2018, 4:43pm UTC](https://discourse.julialang.org/t/global-type-locking/15620/3 "2018-09-28T16:43:25Z")

</div>

in this case, could one define a `constT` that makes this all transparent to users of julia, and add it into the language?

---

<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:** [September 28, 2018, 5:57pm UTC](https://discourse.julialang.org/t/global-type-locking/15620/4 "2018-09-28T17:57:48Z")

</div>

The plan here is to allow `var::T` in global scope to constrain the type of `var` to being `T` much like you can created typed locals with the same syntax in local scopes. The implementation will likely be that `var` is stored in a const typed `Ref` since that behaves in the right way already and just has less convenient syntax.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [September 28, 2018, 6:02pm UTC](https://discourse.julialang.org/t/global-type-locking/15620/5 "2018-09-28T18:02:47Z")

</div>

with less conveint syntax

```julia
julia> mutable struct Global{T}
           value::Ref{T}

           function Global(x::T) where {T}
               return new{T}(x)
           end
       end

julia> Base.getproperty(x::Global{T}, sym::Symbol) where T = getfield(x,sym)[]

julia> Base.show(io::IO, x::Global{T}) where {T} = show(io, x.value)

julia> globalint = Global(5)
5

julia> globalint
5

julia> globalint.value = 6
6

julia> globalint
6

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 28, 2018, 6:13pm UTC](https://discourse.julialang.org/t/global-type-locking/15620/6 "2018-09-28T18:13:14Z")

</div>

> [@JeffreySarnoff](#):
>
> with less conveint syntax

What’s the goal of this implementation?

FWIW, `globalint.value` is longer than `globalint[]`…

Also, `Ref` isn’t a concrete type. `Global` doesn’t need to be mutable, and the field type doesn’t need to be `Ref` anyway…

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [September 28, 2018, 6:48pm UTC](https://discourse.julialang.org/t/global-type-locking/15620/7 "2018-09-28T18:48:35Z")

</div>

no goal – just wondered how far current syntax would go.  
It also does not allow use like `b = a + globalint`

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [September 28, 2018, 6:53pm UTC](https://discourse.julialang.org/t/global-type-locking/15620/8 "2018-09-28T18:53:23Z")

</div>

> [@StefanKarpinski](#):
>
> The plan here is to allow `var::T` in global scope to constrain the type of `var` to being `T` much like you can created typed locals with the same syntax in local scopes. The implementation will likely be that `var` is stored in a const typed `Ref` since that behaves in the right way already and just has less convenient syntax.

fantastic. planned for 1.5? 2.0?

---

<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:** [September 28, 2018, 6:58pm UTC](https://discourse.julialang.org/t/global-type-locking/15620/9 "2018-09-28T18:58:22Z")

</div>

It’s a non-breaking change so it would be in some 1.x release.
