Skip to content

Commit 07c1245

Browse files
cadojomateuszbaran
andauthored
NamedTuple constructor support for FieldArray subtypes (#1246)
* NamedTuple constructor support for FieldArray subtypes * Remove 64 bit integer assumption from FieldVector tests Co-authored-by: Mateusz Baran <mateuszbaran89@gmail.com> * Bumps patch version; adds Base.convert test * Removing Base.convert method --------- Co-authored-by: Mateusz Baran <mateuszbaran89@gmail.com>
1 parent c5f3c74 commit 07c1245

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/FieldArray.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ similar_type(::Type{A}, ::Type{T}, S::Size) where {N, T, A<:FieldArray{N, T}} =
3131
_fieldarray_similar_type(A, T, NewSize::S, OldSize::S) where {S} = A
3232
_fieldarray_similar_type(A, T, NewSize, OldSize) =
3333
default_similar_type(T, NewSize, length_val(NewSize))
34+
35+
# Convenience constructors for NamedTuple types
36+
Base.NamedTuple(array::FieldArray) = Base.NamedTuple{propertynames(array)}(array)

test/FieldMatrix.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,15 @@
109109
@test length(x[1]) == 2
110110
@test x.x == (1, 2)
111111
end
112+
113+
@testset "FieldMatrix to NamedTuple" begin
114+
struct FieldMatrixNT{T} <: FieldMatrix{2,2,T}
115+
a::T
116+
b::T
117+
c::T
118+
d::T
119+
end
120+
121+
@test NamedTuple(FieldMatrixNT(1,2,3,4)) isa @NamedTuple{a::Int, b::Int, c::Int, d::Int}
122+
end
112123
end

test/FieldVector.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,14 @@
140140
end
141141
@test_throws ErrorException("The constructor for Position1088{Float64}(::Float64, ::Float64, ::Float64) is missing!") Position1088((1.,2.,3.))
142142
end
143+
144+
@testset "FieldVector to NamedTuple" begin
145+
struct FieldVectorNT{T} <: FieldVector{3,T}
146+
a::T
147+
b::T
148+
c::T
149+
end
150+
151+
@test NamedTuple(FieldVectorNT(1,2,3)) isa @NamedTuple{a::Int, b::Int, c::Int}
152+
end
143153
end

0 commit comments

Comments
 (0)