Skip to content

Commit 73c8b29

Browse files
committed
revamp constructors
Goals: * support `undef` constructor methods with incompletely specified type, lacking dimensionality, e.g., `FixedArray{Int}(undef, 3, 7)` * support `undef` constructor methods with `size` passed as tuple instead of as `Vararg` To make this possible, it was necessary to get rid of the automatically generated inner constructor methods, to prevent ambiguity. A specific reason why these changes are necessary is that some methods of `similar` defined in `Base` expect such constructors to exist.
1 parent 8dd781b commit 73c8b29

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/FixedSizeArrays.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@ export FixedSizeArray, FixedSizeVector, FixedSizeMatrix
55
struct FixedSizeArray{T,N} <: DenseArray{T,N}
66
mem::Memory{T}
77
size::NTuple{N,Int}
8+
function FixedSizeArray{T,N}(mem::Memory{T}, size::NTuple{N,Int}) where {T,N}
9+
new{T,N}(mem, size)
10+
end
811
end
912

1013
const FixedSizeVector{T} = FixedSizeArray{T,1}
1114
const FixedSizeMatrix{T} = FixedSizeArray{T,2}
1215

16+
function FixedSizeArray{T,N}(::UndefInitializer, size::NTuple{N,Int}) where {T,N}
17+
FixedSizeArray{T,N}(Memory{T}(undef, prod(size)), size)
18+
end
1319
function FixedSizeArray{T,N}(::UndefInitializer, size::Vararg{Int,N}) where {T,N}
14-
return FixedSizeArray(Memory{T}(undef, prod(size)), size)
20+
FixedSizeArray{T,N}(undef, size)
21+
end
22+
function FixedSizeArray{T}(::UndefInitializer, size::Vararg{Int,N}) where {T,N}
23+
FixedSizeArray{T,N}(undef, size)
24+
end
25+
function FixedSizeArray{T}(::UndefInitializer, size::NTuple{N,Int}) where {T,N}
26+
FixedSizeArray{T,N}(undef, size)
1527
end
1628

1729
Base.IndexStyle(::Type{<:FixedSizeArray}) = IndexLinear()

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ using FixedSizeArrays
1313
@test eltype(similar(v, Int)) == Int
1414
@test copy(v) isa FixedSizeVector{Float64}
1515
@test zero(v) isa FixedSizeVector{Float64}
16+
@test similar(FixedSizeVector{Int}, (2,)) isa FixedSizeVector{Int}
17+
@test similar(FixedSizeArray{Int}, (2,)) isa FixedSizeVector{Int}
18+
@test FixedSizeArray{Int}(undef, 2) isa FixedSizeVector{Int}
1619
end
1720

1821
@testset "FixedSizeMatrix" begin
@@ -26,5 +29,8 @@ using FixedSizeArrays
2629
@test eltype(similar(m, Int)) == Int
2730
@test copy(m) isa FixedSizeMatrix{Float64}
2831
@test zero(m) isa FixedSizeMatrix{Float64}
32+
@test similar(FixedSizeMatrix{Int}, (2, 3)) isa FixedSizeMatrix{Int}
33+
@test similar(FixedSizeArray{Int}, (2, 3)) isa FixedSizeMatrix{Int}
34+
@test FixedSizeArray{Int}(undef, 2, 3) isa FixedSizeMatrix{Int}
2935
end
3036
end

0 commit comments

Comments
 (0)