# Return statements and global consts in a struct

**URL:** <https://discourse.julialang.org/t/return-statements-and-global-consts-in-a-struct/57567>\
**Category:** General Usage\
**Created:** [March 19, 2021, 8:46pm UTC](https://discourse.julialang.org/t/return-statements-and-global-consts-in-a-struct/57567 "2021-03-19T20:46:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [March 19, 2021, 8:46pm UTC](https://discourse.julialang.org/t/return-statements-and-global-consts-in-a-struct/57567/1 "2021-03-19T20:46:22Z")

</div>

I posted this on Slack, but looking for some detailed answers. I recently saw a code snippet in Julia

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

```

Jeff described this as basically defining a global const, but doing it inside a struct suppresses the default constructor. Mason replied that this is equivalent (it’s not)

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

```

but here we see a `return` statement in a struct.

What are the reasons for this? How come the default constructor is not needed and how come we can put a `return` statement inside a struct?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 19, 2021, 11:39pm UTC](https://discourse.julialang.org/t/return-statements-and-global-consts-in-a-struct/57567/2 "2021-03-19T23:39:35Z")

</div>

> [@affans](#):
>
> Mason replied that this is equivalent (it’s not)

How is it not equivalent, what’s the difference?

Anyway, I’m not acquainted with Julia’s internals, but maybe this helps somewhat:

```julia
julia> struct S
               return nothing
       end

julia> S() # The default constructor
ERROR: MethodError: no method matching S()
Stacktrace:
 [1] top-level scope at REPL[2]:1
 [2] run_repl(::REPL.AbstractREPL, ::Any) at /build/julia/src/julia1.5.4/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:288

```

---

<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:** [March 20, 2021, 8:06am UTC](https://discourse.julialang.org/t/return-statements-and-global-consts-in-a-struct/57567/3 "2021-03-20T08:06:00Z")

</div>

I also ran into this, see here:

> [@Global const in struct](https://discourse.julialang.org/t/global-const-in-struct/27718/):
>
> In [#32407](https://github.com/JuliaLang/julia/pull/32407) I saw struct \_GLOBAL\_RNG \<: AbstractRNG global const GLOBAL\_RNG = \_GLOBAL\_RNG.instance end Can someone please explain what this does?

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [March 20, 2021, 5:29pm UTC](https://discourse.julialang.org/t/return-statements-and-global-consts-in-a-struct/57567/4 "2021-03-20T17:29:41Z")

</div>

Or maybe it is. I am not sure. Just trying to get an overview of what is happening here.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 20, 2021, 7:10pm UTC](https://discourse.julialang.org/t/return-statements-and-global-consts-in-a-struct/57567/5 "2021-03-20T19:10:51Z")

</div>

This trick is also used with `missing`, see [here](https://github.com/JuliaLang/julia/blob/master/base/essentials.jl).

```julia
"""
    Missing
A type with no fields whose singleton instance [`missing`](@ref) is used
to represent missing values.
"""
struct Missing end

"""
    missing
The singleton instance of type [`Missing`](@ref) representing a missing value.
"""
const missing = Missing()

```

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [March 20, 2021, 10:17pm UTC](https://discourse.julialang.org/t/return-statements-and-global-consts-in-a-struct/57567/6 "2021-03-20T22:17:40Z")

</div>

I think this is slightly different right? This actually sets up a default constuctor. Jeff, on Slack, said there are some uses to not have a default constructor as well. For example, in my original code, you’d want to prevent people from writing out `_GLOBAL_RNG()p` as it can be misunderstood as setting up a new RNG.
