Skip to content

Commit 1dd280b

Browse files
Merge pull request #220 from dehann/patch-1
Base.length dispatch for ArrayPartition (StaticArrays)
2 parents 9b9d8e6 + 7e0f1a4 commit 1dd280b

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/array_partition.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,11 @@ function Base.convert(::Type{ArrayPartition{T,S}}, A::ArrayPartition{<:Any,<:NTu
503503
ntuple((@inline i -> convert(S.parameters[i], A.x[i])), Val(N))
504504
)
505505
end
506+
507+
@generated function Base.length(::Type{<:ArrayPartition{F,T}}) where {F,N,T <: NTuple{N, StaticArraysCore.StaticArray}}
508+
sum_expr = Expr(:call, :+)
509+
for param in T.parameters
510+
push!(sum_expr.args, :(length($param)))
511+
end
512+
return sum_expr
513+
end

test/partitions_and_static_arrays.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ p = ArrayPartition((zeros(Float32, 2), zeros(SMatrix{2,2, Int64},2), zeros(SVect
88

99
p2 = similar(p)
1010
@test typeof(p2)==typeof(p)
11+
12+
p3 = ArrayPartition(SA[1.0, 2.0], MMatrix{2,2}([3.0 4.0; 3.0 5.0]))
13+
@test (@inferred length(typeof(p3))) == 6

0 commit comments

Comments
 (0)