I’ve learned that you can create type-stable references to uninitialized objects like so:
julia> struct Foo{A} a::A end
julia> x = Ref{Foo{Int}}()
Base.RefValue{Foo{Int64}}(Foo{Int64}(1847082903376))
julia> x[] = Foo(1)
Foo{Int64}(1)
julia> y = Array{Foo{Int}}(undef)
0-dimensional Array{Foo{Int64}, 0}:
Foo{Int64}(140703227812064)
julia> y[] = Foo(1)
Foo{Int64}(1)
I have a function which receives a wide range of possible argument types (including types).
I’d like a way to communicate to this function the type of an uninitialized object to make a reference to, in a way that simultaneously communicates that a reference to an uninitialized object should be created (i.e., when simply sending normal types I want it not to behave this way), and without forcing the other arguments to also be uninitialized.
I’m thinking maybe something similar to Val(Foo{Int})
, but not using Val
because that usually means something different. Something along the lines of maybe Undef(Foo{Int})
, if it worked like Undef(Foo{Int}) === Undef{Foo{Int}}()
?
I imagine if Ref
’s constructor had a type-specialization for it, such a thing could work like this:
#= simulated =#
julia> Ref(Undef(Foo{Int}))
Base.RefValue{Foo{Int64}}(Foo{Int64}(1847085789424))
or that you could initialize an array like so:
#= simulated =#
julia> Matrix(Undef(Int), 4, 4)
4×4 Matrix{Int64}:
1847054304048 1847054304816 1847054305072 1847054306160
1847054304624 1847054304880 1847054305328 1847054306224
1847054304688 1847054304944 1847054306032 1847054306288
1847054304752 1847054305008 1847054306096 1847054306352
Is there any existing type that’s suitable for this?