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

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> 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:

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

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.

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

Is it likely to change? also no.

2 Likes