diff --git a/src/FieldArray.jl b/src/FieldArray.jl index e23c0f79..0314ffab 100644 --- a/src/FieldArray.jl +++ b/src/FieldArray.jl @@ -31,3 +31,6 @@ similar_type(::Type{A}, ::Type{T}, S::Size) where {N, T, A<:FieldArray{N, T}} = _fieldarray_similar_type(A, T, NewSize::S, OldSize::S) where {S} = A _fieldarray_similar_type(A, T, NewSize, OldSize) = default_similar_type(T, NewSize, length_val(NewSize)) + +# Convenience constructors for NamedTuple types +Base.NamedTuple(array::FieldArray) = Base.NamedTuple{propertynames(array)}(array) diff --git a/test/FieldMatrix.jl b/test/FieldMatrix.jl index 054fa6e0..830e88e0 100644 --- a/test/FieldMatrix.jl +++ b/test/FieldMatrix.jl @@ -109,4 +109,15 @@ @test length(x[1]) == 2 @test x.x == (1, 2) end + + @testset "FieldMatrix to NamedTuple" begin + struct FieldMatrixNT{T} <: FieldMatrix{2,2,T} + a::T + b::T + c::T + d::T + end + + @test NamedTuple(FieldMatrixNT(1,2,3,4)) isa @NamedTuple{a::Int, b::Int, c::Int, d::Int} + end end diff --git a/test/FieldVector.jl b/test/FieldVector.jl index 376277ae..0d21278d 100644 --- a/test/FieldVector.jl +++ b/test/FieldVector.jl @@ -140,4 +140,14 @@ end @test_throws ErrorException("The constructor for Position1088{Float64}(::Float64, ::Float64, ::Float64) is missing!") Position1088((1.,2.,3.)) end + + @testset "FieldVector to NamedTuple" begin + struct FieldVectorNT{T} <: FieldVector{3,T} + a::T + b::T + c::T + end + + @test NamedTuple(FieldVectorNT(1,2,3)) isa @NamedTuple{a::Int, b::Int, c::Int} + end end