# Why do these two have different types?

**URL:** <https://discourse.julialang.org/t/why-do-these-two-have-different-types/102482>\
**Category:** New to Julia\
**Created:** [August 4, 2023, 12:48pm UTC](https://discourse.julialang.org/t/why-do-these-two-have-different-types/102482 "2023-08-04T12:48:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dali77](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dali77/32/51153_2.png) [@dali77](https://discourse.julialang.org/u/dali77)\
**Post date:** [August 4, 2023, 12:48pm UTC](https://discourse.julialang.org/t/why-do-these-two-have-different-types/102482/1 "2023-08-04T12:48:19Z")

</div>

```julia
using StructArrays

test1 = StructArray{NamedTuple{(:a, :b), NTuple{2, Float32}}}((a=[0f0,0f0,1f0,1f0,2f0,2f0,2f0],b=[0f0,0f0,0f0,0f0,0f0,0f0,0f0]))
7-element StructArray(::Vector{Float32}, ::Vector{Float32}) with eltype NamedTuple{(:a, :b), Tuple{Float32, Float32}}:
 (a = 0.0, b = 0.0)
 (a = 0.0, b = 0.0)
 (a = 1.0, b = 0.0)
 (a = 1.0, b = 0.0)
 (a = 2.0, b = 0.0)
 (a = 2.0, b = 0.0)
 (a = 2.0, b = 0.0)

test2 = StructArray{NamedTuple{(:a, :b), NTuple{2, Float32}}}((a=[0,0,1,1,2,2,2],b=[0,0,0,0,0,0,0]))
7-element StructArray(::Vector{Int64}, ::Vector{Int64}) with eltype NamedTuple{(:a, :b), Tuple{Float32, Float32}}:
 (a = 0.0, b = 0.0)
 (a = 0.0, b = 0.0)
 (a = 1.0, b = 0.0)
 (a = 1.0, b = 0.0)
 (a = 2.0, b = 0.0)
 (a = 2.0, b = 0.0)
 (a = 2.0, b = 0.0)

typeof(test1)
StructVector{NamedTuple{(:a, :b), Tuple{Float32, Float32}}, NamedTuple{(:a, :b), Tuple{Vector{Float32}, Vector{Float32}}}, Int64} (alias for StructArray{NamedTuple{(:a, :b), Tuple{Float32, Float32}}, 1, NamedTuple{(:a, :b), Tuple{Array{Float32, 1}, Array{Float32, 1}}}, Int64})

typeof(test2)
StructVector{NamedTuple{(:a, :b), Tuple{Float32, Float32}}, NamedTuple{(:a, :b), Tuple{Vector{Int64}, Vector{Int64}}}, Int64} (alias for StructArray{NamedTuple{(:a, :b), Tuple{Float32, Float32}}, 1, NamedTuple{(:a, :b), Tuple{Array{Int64, 1}, Array{Int64, 1}}}, Int64})

```

I thought that for both the element type is given explicitly, so why is the type of ttt1 and ttt2 different? That seems weird, I would either expect the elements of test2.a and test2.b to be interpreted as Float32 OR giving an error upon construction of test2. Any hints? Thanks.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [August 4, 2023, 1:11pm UTC](https://discourse.julialang.org/t/why-do-these-two-have-different-types/102482/2 "2023-08-04T13:11:29Z")

</div>

It seems that StructArray stores the original data you gave it, converting as you request it. And in doing that embeds the type of the original data in its type.

```julia
julia> z = StructArray{ComplexF64}(([1, 2], [3.0, 4.0]))
2-element StructArray(::Vector{Int64}, ::Vector{Float64}) with eltype ComplexF64:
 1.0 + 3.0im
 2.0 + 4.0im

julia> z.re
2-element Vector{Int64}:
 1
 2

julia> z.im
2-element Vector{Float64}:
 3.0
 4.0

julia> z[1]
1.0 + 3.0im

julia> typeof(z)
StructVector{ComplexF64, @NamedTuple{re::Vector{Int64}, im::Vector{Float64}}, Int64} (alias for StructArray{Complex{Float64}, 1, @NamedTuple{re::Array{Int64, 1}, im::Array{Float64, 1}}, Int64})

```
