Partial initialisation of a (Named)Tuple?

I’m trying to figure out whether it’s possible to partially initialise a Tuple / NamedTuple.

A concrete example: I would like to be able to write something like

x = Tuple{Vector{Float64}, Vector{Float64}}(randn(5))
isdefined(x, :2) # false

and a similar thing for NamedTuples.

Is this something that is achievable?

You can surelly initialise tuples with empty vectors and then modify them.
What you can’t do is reassign the tuple position/key to other objects or add/delete elements from the tuple, i.e. modify the tuple itself rather than one of its objects.

Unfortunately I do actually need to make things undefined, rather than just using empty vectors because I’m not just interested in arrays – the concrete example didn’t make that clear.

I really care about being able to put any arbitrary type in, and get whatever the usual undefined behaviour is for it.

1 Like

The return of isdefined refers to the definition of the field, not of its content, i. e.:

julia> a = (1,undef)
(1, UndefInitializer())

julia> isdefined(a,1)
true

julia> isdefined(a,2)
true

julia> isdefined(a,3)
false

So I don’t think you can have a tuple with 2 elements and get isdefined as false for one of the elements, whatever its content is.

(ps: I’m aware that the value above is not “undefined”, is a function, I don’t see how there could be anything there and still be “undefined”)

Just to check, you are aware that if a Tuple started with an undefined field it would always have that field undefined, as it is immutable, correct? Is this yet useful to you?

(ps: I’m aware that the value above is not “undefined”, is a function, I don’t see how there could be anything there and still be “undefined”)

I wrote a whole paragraph before reading this, XD. But anyway, it is not a function, it is the singleton object of type UndefInitializer.

1 Like

Is there anything that can be called “undefined” and still be instantiated?

Just to check, you are aware that if a Tuple started with an undefined field it would always have that field undefined, as it is immutable, correct? Is this yet useful to you?

Yes, and it is useful to me :slight_smile:

(The use-case is that I need to be able to build a NamedTuple which has the same names and initialisation state as a given struct.)

The standard undefined example is something like

struct Foo{T}
    a::T
    function Foo{T}() where {T}
        new{T}()
    end
end

julia> Foo{Float64}()
Foo{Float64}(2.7778122606e-314)

julia> Foo{Vector{Float64}}()
Foo{Vector{Float64}}(#undef)
2 Likes