Skip to content

Commit 68eca4c

Browse files
authored
Merge pull request #343 from rdeits/fieldvector-constructor
don't allow FieldVector to be constructed from tuple of the wrong size
2 parents 8f195da + 6270447 commit 68eca4c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/FieldVector.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ For example:
1717
abstract type FieldVector{N, T} <: StaticVector{N, T} end
1818

1919
# Is this a good idea?? Should people just define constructors that accept tuples?
20-
@inline (::Type{FV})(x::Tuple) where {FV <: FieldVector} = FV(x...)
20+
@inline (::Type{FV})(x::Tuple{Vararg{Any, N}}) where {N, FV <: FieldVector{N}} = FV(x...)
2121

2222
@propagate_inbounds getindex(v::FieldVector, i::Int) = getfield(v, i)
2323
@propagate_inbounds setindex!(v::FieldVector, x, i::Int) = setfield!(v, i, x)

test/FieldVector.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636

3737
# Issue 146
3838
@test [[Point3D(1.0,2.0,3.0)]; [Point3D(4.0,5.0,6.0)]]::Vector{Point3D} == [Point3D(1.0,2.0,3.0), Point3D(4.0,5.0,6.0)]
39+
40+
# Issue 342
41+
@test_throws ErrorException Point3D(1,2,3,4)
3942
end
4043

4144
@testset "Mutable Point2D" begin
@@ -69,4 +72,20 @@
6972
@test @inferred(similar_type(Point2D{Float64}, Size(4))) == SVector{4,Float64}
7073
@test @inferred(similar_type(Point2D{Float64}, Float32, Size(4))) == SVector{4,Float32}
7174
end
75+
76+
@testset "FieldVector with Tuple fields" begin
77+
# verify that having a field which is itself a Tuple
78+
# doesn't break anything
79+
80+
eval(quote
81+
struct TupleField <: FieldVector{1, NTuple{2, Int}}
82+
x::NTuple{2, Int}
83+
end
84+
end)
85+
86+
x = TupleField((1,2))
87+
@test length(x) == 1
88+
@test length(x[1]) == 2
89+
@test x.x == (1, 2)
90+
end
7291
end

0 commit comments

Comments
 (0)