# How to make a vector of parametric composite types?

**URL:** <https://discourse.julialang.org/t/how-to-make-a-vector-of-parametric-composite-types/2439>\
**Category:** New to Julia\
**Created:** [March 4, 2017, 1:35pm UTC](https://discourse.julialang.org/t/how-to-make-a-vector-of-parametric-composite-types/2439 "2017-03-04T13:35:11Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![Olivier\_Merchiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivier_merchiers/32/4073_2.png) [@Olivier\_Merchiers](https://discourse.julialang.org/u/Olivier_Merchiers)\
**Post date:** [March 6, 2017, 4:57pm UTC](https://discourse.julialang.org/t/how-to-make-a-vector-of-parametric-composite-types/2439/11 "2017-03-06T16:57:14Z")

</div>

Thanks for this correction.

But there remains the issue with allocation and time for the vector of layers with this implementation:

```julia
abstract OptProp

immutable Al <: OptProp end
immutable Vac <: OptProp end

immutable Layer{T <: OptProp}
          material::Type{T}
          thickness::Float64
 end

@benchmark ml = [Layer(Al,0.0); Layer(Vac,0.0)]
BenchmarkTools.Trial: 
  memory estimate: 1.97 kb
  allocs estimate: 48
  --------------
  minimum time: 10.937 μs (0.00% GC)
  median time: 11.656 μs (0.00% GC)
  mean time: 11.802 μs (0.00% GC)
  maximum time: 72.133 μs (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1
  time tolerance: 5.00%
  memory tolerance: 1.00%

```

While with

```julia
abstract OptProp

immutable Al <: OptProp end
immutable Vac <: OptProp end

immutable Layer
          material::OptProp
          thickness::Float64
 end

@benchmark ml = [Layer(Al(),0.0); Layer(Vac(),0.0)]
BenchmarkTools.Trial: 
  memory estimate: 160.00 bytes
  allocs estimate: 3
  --------------
  minimum time: 43.866 ns (0.00% GC)
  median time: 47.852 ns (0.00% GC)
  mean time: 61.659 ns (21.21% GC)
  maximum time: 2.199 μs (96.82% GC)
  --------------
  samples: 10000
  evals/sample: 990
  time tolerance: 5.00%
  memory tolerance: 1.00%

```

This is quite important for me, since I need to pass such multilayer structures to other functions that will compute reflectivity and other physical quantities.

Finally, doing the test with the permittivity function

```julia
function permittivity(x::Al)
              2
       end

```

I get

```julia
@benchmark permittivity(Al())
BenchmarkTools.Trial: 
  memory estimate: 0.00 bytes
  allocs estimate: 0
  --------------
  minimum time: 1.409 ns (0.00% GC)
  median time: 1.680 ns (0.00% GC)
  mean time: 1.711 ns (0.00% GC)
  maximum time: 90.449 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1000
  time tolerance: 5.00%
  memory tolerance: 1.00%

```

Which is almost 10 times faster.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-make-a-vector-of-parametric-composite-types/2439)._
