# Partial initialisation of a (Named)Tuple?

**URL:** <https://discourse.julialang.org/t/partial-initialisation-of-a-named-tuple/76011>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 8, 2022, 10:43am UTC](https://discourse.julialang.org/t/partial-initialisation-of-a-named-tuple/76011 "2022-02-08T10:43:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![willtebbutt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willtebbutt/32/6790_2.png) [@willtebbutt](https://discourse.julialang.org/u/willtebbutt)\
**Post date:** [February 8, 2022, 10:43am UTC](https://discourse.julialang.org/t/partial-initialisation-of-a-named-tuple/76011/1 "2022-02-08T10:43:51Z")

</div>

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

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

```

and a similar thing for `NamedTuple`s.

Is this something that is achievable?

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 8, 2022, 11:30am UTC](https://discourse.julialang.org/t/partial-initialisation-of-a-named-tuple/76011/2 "2022-02-08T11:30:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![willtebbutt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willtebbutt/32/6790_2.png) [@willtebbutt](https://discourse.julialang.org/u/willtebbutt)\
**Post date:** [February 8, 2022, 12:21pm UTC](https://discourse.julialang.org/t/partial-initialisation-of-a-named-tuple/76011/3 "2022-02-08T12:21:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 8, 2022, 12:48pm UTC](https://discourse.julialang.org/t/partial-initialisation-of-a-named-tuple/76011/4 "2022-02-08T12:48:06Z")

</div>

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

```julia
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”)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 8, 2022, 12:48pm UTC](https://discourse.julialang.org/t/partial-initialisation-of-a-named-tuple/76011/5 "2022-02-08T12:48:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 8, 2022, 12:59pm UTC](https://discourse.julialang.org/t/partial-initialisation-of-a-named-tuple/76011/6 "2022-02-08T12:59:57Z")

</div>

> (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`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 8, 2022, 1:01pm UTC](https://discourse.julialang.org/t/partial-initialisation-of-a-named-tuple/76011/7 "2022-02-08T13:01:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![willtebbutt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willtebbutt/32/6790_2.png) [@willtebbutt](https://discourse.julialang.org/u/willtebbutt)\
**Post date:** [February 8, 2022, 1:20pm UTC](https://discourse.julialang.org/t/partial-initialisation-of-a-named-tuple/76011/8 "2022-02-08T13:20:02Z")

</div>

> [@Henrique\_Becker](#):
>
> 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?

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

(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`.)

---

<div class="post-metadata">

**Author:** ![willtebbutt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willtebbutt/32/6790_2.png) [@willtebbutt](https://discourse.julialang.org/u/willtebbutt)\
**Post date:** [February 8, 2022, 1:21pm UTC](https://discourse.julialang.org/t/partial-initialisation-of-a-named-tuple/76011/9 "2022-02-08T13:21:52Z")

</div>

> [@lmiq](#):
>
> Is there anything that can be called “undefined” and still be instantiated?

The standard undefined example is something like

```julia
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)

```
