Skip to content

Commit 8dd781b

Browse files
authored
specialize similar (#10)
Now `similar` will be able to return `FixedSizeArray`s instead of `Array`s. Updates #4
1 parent 6ad4fb8 commit 8dd781b

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/FixedSizeArrays.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ Base.@propagate_inbounds Base.setindex!(A::FixedSizeArray, v, i::Int) = A.mem[i]
2020

2121
Base.size(a::FixedSizeArray) = getfield(a, :size)
2222

23+
function Base.similar(::FixedSizeArray, ::Type{S}, size::NTuple{N,Int}) where {S,N}
24+
FixedSizeArray{S,N}(undef, size...)
25+
end
26+
2327
end # module FixedSizeArrays

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ using FixedSizeArrays
88
@test length(v) == 3
99
v .= 1:3
1010
@test v == 1:3
11+
@test typeof(v) == typeof(similar(v)) == FixedSizeVector{Float64}
12+
@test similar(v, Int) isa FixedSizeVector{Int}
13+
@test eltype(similar(v, Int)) == Int
14+
@test copy(v) isa FixedSizeVector{Float64}
15+
@test zero(v) isa FixedSizeVector{Float64}
1116
end
1217

1318
@testset "FixedSizeMatrix" begin
@@ -16,5 +21,10 @@ using FixedSizeArrays
1621
@test length(m) == 9
1722
m .= m_ref
1823
@test m == m_ref
24+
@test typeof(m) == typeof(similar(m)) == FixedSizeMatrix{Float64}
25+
@test similar(m, Int) isa FixedSizeMatrix{Int}
26+
@test eltype(similar(m, Int)) == Int
27+
@test copy(m) isa FixedSizeMatrix{Float64}
28+
@test zero(m) isa FixedSizeMatrix{Float64}
1929
end
2030
end

0 commit comments

Comments
 (0)