# Implementing singletons for configuration variables

**URL:** <https://discourse.julialang.org/t/implementing-singletons-for-configuration-variables/62177>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 1, 2021, 8:41am UTC](https://discourse.julialang.org/t/implementing-singletons-for-configuration-variables/62177 "2021-06-01T08:41:39Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [June 1, 2021, 10:29am UTC](https://discourse.julialang.org/t/implementing-singletons-for-configuration-variables/62177/4 "2021-06-01T10:29:04Z")

</div>

Your solution looks reasonable to me, except that redefining the constructor is very hackish (and do you need the struct the be mutable?).

You can instead check if the variable `s` is defined:

```julia
module Settings
    struct SettingsStruct
        a::Int64
        b::Int64
        function SettingsStruct(a, b)
            isdefined(Settings, :s) && error("Cannot instantiate SettingsStruct twice")
            return new(a, b)
        end
    end

    const s = SettingsStruct(2, 3)
end

```

See also [Implementing Singleton design pattern](https://discourse.julialang.org/t/implementing-singleton-design-pattern/61704).

However in this case I would just use a `const` with no additional check: you already get a warning if you change it:

> WARNING: redefinition of constant s. This may fail, cause incorrect answers, or produce other errors.

But yeah, I do wish it would give an error instead. The current behavior seems designed for playing around and prototyping rather than building robust applications.

This was recently discussed on [Behavior of reassignment to a `const` · Issue #38584 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/38584).

---

_[View the full topic](https://discourse.julialang.org/t/implementing-singletons-for-configuration-variables/62177)._
