# Is accessing the \`instance\` field of a singleton type supported usage?

**URL:** <https://discourse.julialang.org/t/is-accessing-the-instance-field-of-a-singleton-type-supported-usage/93671>\
**Category:** General Usage\
**Tags:** type\
**Created:** [January 28, 2023, 12:08am UTC](https://discourse.julialang.org/t/is-accessing-the-instance-field-of-a-singleton-type-supported-usage/93671 "2023-01-28T00:08:03Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [January 28, 2023, 12:08am UTC](https://discourse.julialang.org/t/is-accessing-the-instance-field-of-a-singleton-type-supported-usage/93671/1 "2023-01-28T00:08:03Z")

</div>

> [@Constructors for \`\<:Function\` types when possible, like in C++](https://discourse.julialang.org/t/constructors-for-function-types-when-possible-like-in-c/86611/2):
>
> Every ‘singleton’ struct can be constructed from it’s type using `T.instance`

The above technique is very useful in rare cases, but I wonder whether it’s supported? I don’t think it’s documented.

For example; is the below guaranteed to work with future Julia versions?

```julia
julia> Tuple{Nothing, Nothing, Missing, Val{Val(2)}}.instance
(nothing, nothing, missing, Val{Val{2}()}())

```

FTR (for people other than Mason), an alternative technique, based on documented inner constructor behavior, for accomplishing the same task is demonstrated here:

> <https://gitlab.com/nsajko/InstanceOfSingletonType.jl/-/blob/262fe061cd79b2ca9601e76918881b4008b0eae4/src/InstanceOfSingletonType.jl>

---

<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:** [January 28, 2023, 12:31am UTC](https://discourse.julialang.org/t/is-accessing-the-instance-field-of-a-singleton-type-supported-usage/93671/2 "2023-01-28T00:31:36Z")

</div>

It’s probably worth noting that, although potentially useful, these techniques may also be dangerous, because they bypass inner constructors. Example:

```julia
julia> struct InstancesDisallowed
         InstancesDisallowed() = error("no can do")
       end

julia> InstancesDisallowed.instance
InstancesDisallowed()

julia> InstancesDisallowed()
ERROR: no can do

```

I think that a type’s inner constructor being bypassable is something that could hypothetically turn out to be unpleasantly surprising for someone in some situation.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 28, 2023, 12:48am UTC](https://discourse.julialang.org/t/is-accessing-the-instance-field-of-a-singleton-type-supported-usage/93671/3 "2023-01-28T00:48:56Z")

</div>

if you’re asking if this is part of public API that follows semVer, no.

Is it likely to change? also no.
