diff --git a/src/StructsOfArrays.jl b/src/StructsOfArrays.jl index 12c2751..c42b048 100644 --- a/src/StructsOfArrays.jl +++ b/src/StructsOfArrays.jl @@ -46,4 +46,21 @@ end x end end + +function Base.getindex{T}(A::StructOfArrays{T}, s::Symbol) + idx = findfirst(fieldnames(T), s) + if (idx == 0) + throw(KeyError(s)) + end + A.arrays[idx] +end + +function Base.setindex!{T}(A::StructOfArrays{T}, x, s::Symbol) + idx = findfirst(fieldnames(T), s) + if (idx == 0) + throw(KeyError(s)) + end + A.arrays[idx][:] = x +end + end # module diff --git a/test/runtests.jl b/test/runtests.jl index a03e03a..4bddb44 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -31,3 +31,10 @@ small = StructOfArrays(Complex64, 2) @test typeof(similar(small, SubString)) === Vector{SubString} @test typeof(similar(small, OneField)) === Vector{OneField} @test typeof(similar(small, Complex128)) <: StructOfArrays + +fields = StructOfArrays(OneField, 2) +@test typeof(fields[:x]) === Vector{Int} +@test length(fields[:x]) == 2 + +fields[:x] = [1, 2] +@test fields[:x] == [1, 2]