# Advice for building nested objects

**URL:** <https://discourse.julialang.org/t/advice-for-building-nested-objects/25269>\
**Category:** General Usage\
**Tags:** suggestions, data\_structures\
**Created:** [June 13, 2019, 8:13pm UTC](https://discourse.julialang.org/t/advice-for-building-nested-objects/25269 "2019-06-13T20:13:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julia\_coder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julia_coder/32/8960_2.png) [@Julia\_coder](https://discourse.julialang.org/u/Julia_coder)\
**Post date:** [June 13, 2019, 8:13pm UTC](https://discourse.julialang.org/t/advice-for-building-nested-objects/25269/1 "2019-06-13T20:13:18Z")

</div>

Hello, I am looking for some advice on how to set up nested objects in Julia.

For example, if I wanted to create “schoolchildren” (with variables “age”, “testGrade”,“favouriteSubject”) which are then grouped into “classrooms” (with variables “numberOfStudents”,“averageTestGrade”,“mostPopularSubject”), which are then nested into a single “school”, what sort of data structures should I use for each level, and how would I implement them?

As may be obvious I would want the “classrooms” to be able to accumulate meta-data about the “schoolchildren” in them.

Structs seem an easy match for the schoolchildren but I’m not sure if they’ll apply to the higher-level objects.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [June 13, 2019, 11:38pm UTC](https://discourse.julialang.org/t/advice-for-building-nested-objects/25269/2 "2019-06-13T23:38:30Z")

</div>

you might start with this and modify it to suit your needs

```julia
mutable struct Child
  firstname::String
  lastname::String
  age::Int
  testgrade::String
  metadata::String
end

struct Classroom
  children::Vector{Child}
end

numberofstudents(x::Classroom) = length(x.children)

struct School
  classrooms::Vector{Classroom}
end

numberofclassrooms(x::Classroom) = length(x.classrooms)

const Grades =
  Dict("A+" => 1, "A" => 2, "A-" => 3,
       "B+" => 4, "B" => 5, "B-" => 6)

function averagetestgrade(x::School)
    gradesum = 0
    gradecount = 0
    for class in x.classrooms
        for student in class.children
            gradesum += Grades[student.testgrade]
            gradecount += 1
        end
    end
    return gradesum / gradecount
end

AmyAmile = Child("Amy", "Amile", 10, "A")
BillyButton = Child("Billy", "Button", 10, "B+")
CindyConnoly = Child("Cindy", "Connoly", 10, "A-")

mathclass = Classroom([AmyAmile, BillyButton, CindyConnoly])
civicsclass = Classroom([BillyButton, CindyConnoly])

school = School([mathclass, civicsclass])

```

I’m sure you know that, full scale, this sort of app is usually done using a database.

---

<div class="post-metadata">

**Author:** ![Julia\_coder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julia_coder/32/8960_2.png) [@Julia\_coder](https://discourse.julialang.org/u/Julia_coder)\
**Post date:** [June 14, 2019, 2:42pm UTC](https://discourse.julialang.org/t/advice-for-building-nested-objects/25269/3 "2019-06-14T14:42:49Z")

</div>

This looks great, I’ll give it a try. Thanks a bunch!
