# Tree structure with parametric type

**URL:** https://discourse.julialang.org/t/tree-structure-with-parametric-type/9300
**Category:** General Usage
**Created:** [February 24, 2018, 5:33pm UTC](https://discourse.julialang.org/t/tree-structure-with-parametric-type/9300 "2018-02-24T17:33:33Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [February 24, 2018, 8:36pm UTC](https://discourse.julialang.org/t/tree-structure-with-parametric-type/9300/2 "2018-02-24T20:36:06Z")

</div>

Both are possible (and different), but neither is ideal, in my opinion. The issue with `A{A}(...)` is that `A` is a non-concrete type, so the outer layer will contain a `Vector{A}` whose elements are non-concrete. That _may_ harm performance.

`A{A{B}}` does fix that particular issue, but it’s starting to feel like an abuse of the type system. You definitely _can_ do this, but it means, for example, compiling new native code for every layer in the tree (because every layer is a different type).

In [RegionTrees.jl](https://github.com/rdeits/RegionTrees.jl/blob/master/src/cell.jl#L1) I essentially make every node _both_ and A and a B. That is, every node can contain data and/or a vector of children. I use `Nullable`s to keep everything concretely typed, and it seems to work pretty well.

Another option would be to use a small Union:

```julia
struct A
  children::Vector{Union{A, B}}
end

struct B
  data::Vector{Float64}
end

```

In particular, this may end up performing better on Julia v0.7 (nightly) than on the current v0.6 release.

But, really, the most important thing is: there is no single answer to efficiency questions like this. The only way to know for sure what the right approach is is to benchmark it.

---

_[View the full topic](https://discourse.julialang.org/t/tree-structure-with-parametric-type/9300)._
