# Data Layout

**URL:** <https://discourse.julialang.org/t/data-layout/34974>\
**Category:** General Usage\
**Created:** [February 21, 2020, 12:46pm UTC](https://discourse.julialang.org/t/data-layout/34974 "2020-02-21T12:46:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kruxigt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kruxigt/32/11694_2.png) [@Kruxigt](https://discourse.julialang.org/u/Kruxigt)\
**Post date:** [February 21, 2020, 12:46pm UTC](https://discourse.julialang.org/t/data-layout/34974/1 "2020-02-21T12:46:18Z")

</div>

Good day!

I’m trying to come up with the best data layout using structs for the following situation. I have a struct

```julia
mutable struct MyStruct
   field1::Float64
   field2::String
end

```

From this struct I would like to link other structs of various types, let’s call them layers. The number of linked structs can vary. My thoughts is that I could link this via adding for example a Tuple (or named Tuple) which links to the structs, i.e

```julia
mutable struct MyStruct
   field1::Float64
   field2::String
   layers::Tuple
end

```

I could also use for example an Array or a Dict but it seems that Tuple plays well with the StructArrays package. One problem I have with using a Tuple is that there doesn’t seem that a Tuple can have 0 or 1 element? I have tried searching for an answer to this, but no luck.

So all in all I have two questions.

1, Is it possible to have Tuples with 0 or 1 element?  
2, Is there a better way to set up the kind of data structure that I want to have?

Thanks! /Robin

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [February 21, 2020, 1:04pm UTC](https://discourse.julialang.org/t/data-layout/34974/2 "2020-02-21T13:04:26Z")

</div>

To really answer your question well, you would have to explain a bit more, how exactly you want to use the field `layers`, is the type and number of objects stored in `layers` always known when `MyStruct` is created or is it allowed to be changed afterwards. In the first case I would make the type of `layers` a type parameter and would have `nothing` represent the 0-element case and `Some(x)` the 1-element case. This is what it could look like:

```julia
mutable struct MyStruct{T<:Union{Nothing,Some}}
   field1::Float64
   field2::String
   layer::T
   MyStruct(field1, field2) = new{Nothing}(field1, field2, nothing)
   MyStruct(field1, field2, layer::T) where T = new{Some{T}}(field1, field2, Some(layer))
end

```

If you want to store multiple objects of arbitrary type even after `MyStruct` has been created though, your best bet is probably just using a vector with element type `Any`, which will bring with it a performance penalty, but since it’s not possible to make that case type stable, you’re probably not going to get rid of that overhead anyways.
