# Global const in struct

**URL:** https://discourse.julialang.org/t/global-const-in-struct/27718
**Category:** General Usage
**Tags:** question
**Created:** [August 19, 2019, 3:35pm UTC](https://discourse.julialang.org/t/global-const-in-struct/27718 "2019-08-19T15:35:43Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 19, 2019, 3:35pm UTC](https://discourse.julialang.org/t/global-const-in-struct/27718/1 "2019-08-19T15:35:43Z")

</div>

In [#32407](https://github.com/JuliaLang/julia/pull/32407) I saw

```julia
struct _GLOBAL_RNG <: AbstractRNG
    global const GLOBAL_RNG = _GLOBAL_RNG.instance
end

```

Can someone please explain what this does?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 21, 2019, 8:09am UTC](https://discourse.julialang.org/t/global-const-in-struct/27718/2 "2019-08-21T08:09:20Z")

</div>

Anyone? I still could not figure out what this does.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [August 21, 2019, 8:37am UTC](https://discourse.julialang.org/t/global-const-in-struct/27718/3 "2019-08-21T08:37:39Z")

</div>

I think it just executes the code inside the `struct`? I wasn’t aware that something like this was possible:

```julia
julia> struct Foo
           println("Hello world!")
       end
Hello world!

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 21, 2019, 8:45am UTC](https://discourse.julialang.org/t/global-const-in-struct/27718/4 "2019-08-21T08:45:31Z")

</div>

@rfourquet asked about it

[https://github.com/JuliaLang/julia/pull/32407/files#r305580244](https://github.com/JuliaLang/julia/pull/32407/files#r305580244)

but got no reply.

@jameson, sorry for the direct ping, but can you please explain this code in your PR? I am rather curious.

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [August 21, 2019, 4:08pm UTC](https://discourse.julialang.org/t/global-const-in-struct/27718/5 "2019-08-21T16:08:46Z")

</div>

It’s just the same as defining constructors, or whatever use you usually do:

```julia
struct A <: B
    A() = new()
    global copy() = new()
    x = 5
    println(x)
end

```

---

<div class="post-metadata">

### Author: ![zennatavares](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zennatavares/32/6205_2.png) [@zennatavares](https://discourse.julialang.org/u/zennatavares)
#### Post date: [June 15, 2020, 1:17pm UTC](https://discourse.julialang.org/t/global-const-in-struct/27718/6 "2020-06-15T13:17:08Z")

</div>

This answer surprised me more than the initial question (which I also had having looked at Random)

I did not know all these things are valid within a struct. What do they mean?

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [June 15, 2020, 1:52pm UTC](https://discourse.julialang.org/t/global-const-in-struct/27718/7 "2020-06-15T13:52:38Z")

</div>

You can basically write any code inside a struct definition. It’s just like a `let` clause or `for` loop, for example, in that it does introduce a new scope. That is why you need `global` if you want to use a variable outside the module definition. (The struct name is already global because the `struct` keyword creates a new constant global type, so you don’t need `global` for defining a default constructor.) The only difference if you are inside a struct definition is that you can use `new` to create a new instance of the struct. (FWIW you can use `eval(Expr(:new, :T, x))` to create an instance of `T` with field `x`, like `new` does, even outside the struct definition, but that is definitely not something I’d recommend.)

---

<div class="post-metadata">

### Author: ![zennatavares](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zennatavares/32/6205_2.png) [@zennatavares](https://discourse.julialang.org/u/zennatavares)
#### Post date: [June 15, 2020, 2:05pm UTC](https://discourse.julialang.org/t/global-const-in-struct/27718/8 "2020-06-15T14:05:19Z")

</div>

Thanks!
