# Struct containing a tuple of unknown length

**URL:** <https://discourse.julialang.org/t/struct-containing-a-tuple-of-unknown-length/32202>\
**Category:** New to Julia\
**Created:** [December 12, 2019, 8:13pm UTC](https://discourse.julialang.org/t/struct-containing-a-tuple-of-unknown-length/32202 "2019-12-12T20:13:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbaxter](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@jbaxter](https://discourse.julialang.org/u/jbaxter)\
**Post date:** [December 12, 2019, 8:13pm UTC](https://discourse.julialang.org/t/struct-containing-a-tuple-of-unknown-length/32202/1 "2019-12-12T20:13:40Z")

</div>

I’m thinking about some kind of immutable version of the following mutable object:

mutable struct MGlob  
id::Int  
children::Array{MGlob}  
end

For example the immutable version, called struct Glob, would have the same id field but the children could be a Tuple of Globs now. If I use that approach, I guess I have two problems:

1. how to declare Glob, and
2. how to write the most efficient function to convert any MGlob to a Glob.  
The conversion would change each MGlob object in the array of MGlobs to a Glob object in the tuple of Globs. For some reason I can’t get it. Thanks for any help!

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [December 12, 2019, 8:31pm UTC](https://discourse.julialang.org/t/struct-containing-a-tuple-of-unknown-length/32202/2 "2019-12-12T20:31:56Z")

</div>

Is this what you’re looking for?

```julia
struct Glob{N}
    id::Int
    children::NTuple{N,Glob}
end

Glob(mg::MGlob) = Glob{length(mg.children)}(mg.id, ntuple(i -> Glob(mg.children[i]), length(mg.children)))

```

---

<div class="post-metadata">

**Author:** ![jbaxter](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@jbaxter](https://discourse.julialang.org/u/jbaxter)\
**Post date:** [December 12, 2019, 9:12pm UTC](https://discourse.julialang.org/t/struct-containing-a-tuple-of-unknown-length/32202/3 "2019-12-12T21:12:14Z")

</div>

Yes, that seems to work very well! I’ll have to think more about the parameter N but it does the job. I appreciate the help.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [December 12, 2019, 9:53pm UTC](https://discourse.julialang.org/t/struct-containing-a-tuple-of-unknown-length/32202/4 "2019-12-12T21:53:12Z")

</div>

You will have to be very careful with type stability if you expect to actually see a performance improvement from the immutable version.

Unless the number of children is constant enough to achieve that, so you can actually write

```julia
struct Glob{N,G<:Glob}
    id::Int
    children::NTuple{N,G}
end

```

I would stick with the `MGlob`s.

---

<div class="post-metadata">

**Author:** ![jbaxter](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@jbaxter](https://discourse.julialang.org/u/jbaxter)\
**Post date:** [December 12, 2019, 11:08pm UTC](https://discourse.julialang.org/t/struct-containing-a-tuple-of-unknown-length/32202/5 "2019-12-12T23:08:32Z")

</div>

That is good to know too. Right now I am interested in immutability for another reason also. Once I have constructed a big tree I would like to make sure I can’t inadvertently modify some part of it. (Well, maybe there are different ways to achieve the same goal which would be better!)  
Thanks!
