Skip to content

Commit

Permalink
NamedTuple constructor support for FieldArray subtypes (#1246)
Browse files Browse the repository at this point in the history
* NamedTuple constructor support for FieldArray subtypes

* Remove 64 bit integer assumption from FieldVector tests

Co-authored-by: Mateusz Baran <[email protected]>

* Bumps patch version; adds Base.convert test

* Removing Base.convert method

---------

Co-authored-by: Mateusz Baran <[email protected]>
  • Loading branch information
cadojo and mateuszbaran authored Feb 26, 2024
1 parent c5f3c74 commit 07c1245
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/FieldArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 11 additions & 0 deletions test/FieldMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions test/FieldVector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 07c1245

Please sign in to comment.