Skip to content

Commit 4cd2f2a

Browse files
authored
Merge pull request #457 from JuliaArrays/fe/undef
[RFC] use undef for uninitialized MArray constructors
2 parents 93701b5 + d6e2bb3 commit 4cd2f2a

File tree

10 files changed

+68
-50
lines changed

10 files changed

+68
-50
lines changed

src/MArray.jl

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@ mutable struct MArray{S <: Tuple, T, N, L} <: StaticArray{S, T, N}
3030
new{S,T,N,L}(convert_ntuple(T, x))
3131
end
3232

33-
function MArray{S,T,N,L}() where {S,T,N,L}
33+
function MArray{S,T,N,L}(::UndefInitializer) where {S,T,N,L}
3434
check_array_parameters(S, T, Val{N}, Val{L})
3535
new{S,T,N,L}()
3636
end
37+
38+
# deprecated empty constructor
39+
function MArray{S,T,N,L}() where {S,T,N,L}
40+
Base.depwarn("`MArray{S,T,N,L}()` is deprecated, use `MArray{S,T,N,L}(undef)` instead", :MArray)
41+
return MArray{S,T,N,L}(undef)
42+
end
3743
end
3844

3945
@generated function (::Type{MArray{S,T,N}})(x::Tuple) where {S,T,N}
@@ -57,17 +63,25 @@ end
5763
end
5864
end
5965

60-
@generated function (::Type{MArray{S,T,N}})() where {S,T,N}
66+
function (::Type{MArray{S,T,N}})() where {S,T,N}
67+
Base.depwarn("`MArray{S,T,N}()` is deprecated, use `MArray{S,T,N}(undef)` instead", :MArray)
68+
return MArray{S,T,N}(undef)
69+
end
70+
@generated function (::Type{MArray{S,T,N}})(::UndefInitializer) where {S,T,N}
6171
return quote
6272
$(Expr(:meta, :inline))
63-
MArray{S, T, N, $(tuple_prod(S))}()
73+
MArray{S, T, N, $(tuple_prod(S))}(undef)
6474
end
6575
end
6676

67-
@generated function (::Type{MArray{S,T}})() where {S,T}
77+
function (::Type{MArray{S,T}})() where {S,T}
78+
Base.depwarn("`MArray{S,T}()` is deprecated, use `MArray{S,T}(undef)` instead", :MArray)
79+
return MArray{S,T}(undef)
80+
end
81+
@generated function (::Type{MArray{S,T}})(::UndefInitializer) where {S,T}
6882
return quote
6983
$(Expr(:meta, :inline))
70-
MArray{S, T, $(tuple_length(S)), $(tuple_prod(S))}()
84+
MArray{S, T, $(tuple_length(S)), $(tuple_prod(S))}(undef)
7185
end
7286
end
7387

src/MMatrix.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ end
4646
end
4747
end
4848

49-
@generated function (::Type{MMatrix{S1,S2,T}})() where {S1,S2,T}
49+
function (::Type{MMatrix{S1,S2,T}})() where {S1,S2,T}
50+
Base.depwarn("`MMatrix{S1,S2,T}()` is deprecated, use `MMatrix{S1,S2,T}(undef)` instead", :MMatrix)
51+
return MMatrix{S1,S2,T}(undef)
52+
end
53+
@generated function (::Type{MMatrix{S1,S2,T}})(::UndefInitializer) where {S1,S2,T}
5054
return quote
5155
$(Expr(:meta, :inline))
52-
MMatrix{S1, S2, T, $(S1*S2)}()
56+
MMatrix{S1, S2, T, $(S1*S2)}(undef)
5357
end
5458
end
5559

src/abstractarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ similar(::Type{A},s::Size{S}) where {A<:AbstractArray,S} = similar(A,eltype(A),s
9292
similar(::A,::Type{T},s::Size{S}) where {A<:AbstractArray,T,S} = similar(A,T,s)
9393

9494
# defaults to built-in mutable types
95-
similar(::Type{A},::Type{T},s::Size{S}) where {A<:AbstractArray,T,S} = mutable_similar_type(T,s,length_val(s))()
95+
similar(::Type{A},::Type{T},s::Size{S}) where {A<:AbstractArray,T,S} = mutable_similar_type(T,s,length_val(s))(undef)
9696

9797
# both SizedArray and Array return SizedArray
9898
similar(::Type{SA},::Type{T},s::Size{S}) where {SA<:SizedArray,T,S} = sizedarray_similar_type(T,s,length_val(s))()

test/MArray.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
@test MArray{Tuple{1},Int,1,1}((1,)).data === (1,)
44
@test MArray{Tuple{1},Float64,1,1}((1,)).data === (1.0,)
55
@test MArray{Tuple{2,2},Float64,2,4}((1, 1.0, 1, 1)).data === (1.0, 1.0, 1.0, 1.0)
6-
@test isa(MArray{Tuple{1},Int,1,1}(), MArray{Tuple{1},Int,1,1})
7-
@test isa(MArray{Tuple{1},Int,1}(), MArray{Tuple{1},Int,1,1})
8-
@test isa(MArray{Tuple{1},Int}(), MArray{Tuple{1},Int,1,1})
6+
@test isa(MArray{Tuple{1},Int,1,1}(undef), MArray{Tuple{1},Int,1,1})
7+
@test isa(MArray{Tuple{1},Int,1}(undef), MArray{Tuple{1},Int,1,1})
8+
@test isa(MArray{Tuple{1},Int}(undef), MArray{Tuple{1},Int,1,1})
99

1010
# Bad input
1111
@test_throws Exception MArray{Tuple{2},Int,1,2}((1,))
@@ -147,7 +147,7 @@
147147
@test_throws BoundsError setindex!(mm, 4, 82)
148148

149149
# setindex with non-elbits type
150-
m = MArray{Tuple{2,2,2}, String}()
150+
m = MArray{Tuple{2,2,2}, String}(undef)
151151
@test_throws ErrorException setindex!(m, "a", 1, 1, 1)
152152
end
153153

test/MMatrix.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
@test MMatrix{1,1,Int,1}((1,)).data === (1,)
44
@test MMatrix{1,1,Float64,1}((1,)).data === (1.0,)
55
@test MMatrix{2,2,Float64,4}((1, 1.0, 1, 1)).data === (1.0, 1.0, 1.0, 1.0)
6-
@test isa(MMatrix{1,1,Int,1}(), MMatrix{1,1,Int,1})
7-
@test isa(MMatrix{1,1,Int}(), MMatrix{1,1,Int,1})
6+
@test isa(MMatrix{1,1,Int,1}(undef), MMatrix{1,1,Int,1})
7+
@test isa(MMatrix{1,1,Int}(undef), MMatrix{1,1,Int,1})
88

99
# Bad input
1010
@test_throws Exception MMatrix{2,1,Int,2}((1,))
@@ -106,7 +106,7 @@
106106
@test m.data === (11, 12, 13, 14)
107107

108108
# setindex with non-elbits type
109-
m = MMatrix{2,2,String}()
109+
m = MMatrix{2,2,String}(undef)
110110
@test_throws ErrorException setindex!(m, "a", 1, 1)
111111
end
112112
end

test/MVector.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
@test MVector{1,Int}((1,)).data === (1,)
44
@test MVector{1,Float64}((1,)).data === (1.0,)
55
@test MVector{2,Float64}((1,1.0)).data === (1.0,1.0)
6-
@test isa(MVector{1,Int}(), MVector{1,Int})
6+
@test isa(MVector{1,Int}(undef), MVector{1,Int})
77

88
@test_throws Exception MVector{2,Int}((1,))
99
@test_throws Exception MVector{1,Int}(())
@@ -85,7 +85,7 @@
8585
@test_throws BoundsError setindex!(v, 4., 4)
8686

8787
# setindex with non-elbits type
88-
v = MVector{2,String}()
88+
v = MVector{2,String}(undef)
8989
@test_throws ErrorException setindex!(v, "a", 1)
9090
end
9191
end

test/arraymath.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import StaticArrays.arithmetic_closure
4343
end
4444

4545
@testset "fill!()" begin
46-
m = MMatrix{4,16,Float64}()
46+
m = MMatrix{4,16,Float64}(undef)
4747
fill!(m, 3)
4848
@test all(m .== 3.)
4949
end

test/indexing.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ using StaticArrays, Test
2626
vec = @SVector [4,5,6,7]
2727

2828
# SVector
29-
mv = MVector{4,Int}()
29+
mv = MVector{4,Int}(undef)
3030
@test (mv[SVector(1,2,3,4)] = vec; (@inferred getindex(mv, SVector(4,3,2,1)))::SVector{4,Int} == SVector((7,6,5,4)))
3131

32-
mv = MVector{4,Int}()
32+
mv = MVector{4,Int}(undef)
3333
@test (mv[SVector(1,2,3,4)] = [4, 5, 6, 7]; (@inferred getindex(mv, SVector(4,3,2,1)))::SVector{4,Int} == SVector((7,6,5,4)))
3434
@test (mv[SVector(1,2,3,4)] = 2; (@inferred getindex(mv, SVector(4,3,2,1)))::SVector{4,Int} == SVector((2,2,2,2)))
3535

3636
# Colon
37-
mv = MVector{4,Int}()
37+
mv = MVector{4,Int}(undef)
3838
@test (mv[:] = vec; (@inferred getindex(mv, :))::SVector{4,Int} == SVector((4,5,6,7)))
3939
@test (mv[:] = [4, 5, 6, 7]; (@inferred getindex(mv, :))::SVector{4,Int} == SVector((4,5,6,7)))
4040
@test (mv[:] = 2; (@inferred getindex(mv, :))::SVector{4,Int} == SVector((2,2,2,2)))
@@ -49,11 +49,11 @@ using StaticArrays, Test
4949
vec = @SVector [4,5,6,7]
5050

5151
# SVector
52-
mm = MMatrix{2,2,Int}()
52+
mm = MMatrix{2,2,Int}(undef)
5353
@test (mm[SVector(1,2,3,4)] = vec; (@inferred getindex(mm, SVector(4,3,2,1)))::SVector{4,Int} == SVector((7,6,5,4)))
5454

5555
# Colon
56-
mm = MMatrix{2,2,Int}()
56+
mm = MMatrix{2,2,Int}(undef)
5757
@test (mm[:] = vec; (@inferred getindex(mm, :))::SVector{4,Int} == SVector((4,5,6,7)))
5858
end
5959

@@ -93,16 +93,16 @@ using StaticArrays, Test
9393
sm = @MMatrix [1 3; 2 4]
9494

9595
# Tuple, scalar
96-
@test (mm = MMatrix{2,2,Int}(); mm[SVector(2,1),SVector(2,1)] = sm[SVector(2,1),SVector(2,1)]; (@inferred getindex(mm, SVector(2,1), SVector(2,1)))::SMatrix == @SMatrix [4 2; 3 1])
97-
@test (mm = MMatrix{2,2,Int}(); mm[1,SVector(1,2)] = sm[1,SVector(1,2)]; (@inferred getindex(mm, 1, SVector(1,2)))::SVector == @SVector [1,3])
98-
@test (mm = MMatrix{2,2,Int}(); mm[SVector(1,2),1] = sm[SVector(1,2),1]; (@inferred getindex(mm, SVector(1,2), 1))::SVector == @SVector [1,2])
96+
@test (mm = MMatrix{2,2,Int}(undef); mm[SVector(2,1),SVector(2,1)] = sm[SVector(2,1),SVector(2,1)]; (@inferred getindex(mm, SVector(2,1), SVector(2,1)))::SMatrix == @SMatrix [4 2; 3 1])
97+
@test (mm = MMatrix{2,2,Int}(undef); mm[1,SVector(1,2)] = sm[1,SVector(1,2)]; (@inferred getindex(mm, 1, SVector(1,2)))::SVector == @SVector [1,3])
98+
@test (mm = MMatrix{2,2,Int}(undef); mm[SVector(1,2),1] = sm[SVector(1,2),1]; (@inferred getindex(mm, SVector(1,2), 1))::SVector == @SVector [1,2])
9999

100100
# Colon
101-
@test (mm = MMatrix{2,2,Int}(); mm[:,:] = sm[:,:]; (@inferred getindex(mm, :, :))::SMatrix == @MMatrix [1 3; 2 4])
102-
@test (mm = MMatrix{2,2,Int}(); mm[SVector(2,1),:] = sm[SVector(2,1),:]; (@inferred getindex(mm, SVector(2,1), :))::SMatrix == @SMatrix [2 4; 1 3])
103-
@test (mm = MMatrix{2,2,Int}(); mm[:,SVector(2,1)] = sm[:,SVector(2,1)]; (@inferred getindex(mm, :, SVector(2,1)))::SMatrix == @SMatrix [3 1; 4 2])
104-
@test (mm = MMatrix{2,2,Int}(); mm[1,:] = sm[1,:]; (@inferred getindex(mm, 1, :))::SVector == @SVector [1,3])
105-
@test (mm = MMatrix{2,2,Int}(); mm[:,1] = sm[:,1]; (@inferred getindex(mm, :, 1))::SVector == @SVector [1,2])
101+
@test (mm = MMatrix{2,2,Int}(undef); mm[:,:] = sm[:,:]; (@inferred getindex(mm, :, :))::SMatrix == @MMatrix [1 3; 2 4])
102+
@test (mm = MMatrix{2,2,Int}(undef); mm[SVector(2,1),:] = sm[SVector(2,1),:]; (@inferred getindex(mm, SVector(2,1), :))::SMatrix == @SMatrix [2 4; 1 3])
103+
@test (mm = MMatrix{2,2,Int}(undef); mm[:,SVector(2,1)] = sm[:,SVector(2,1)]; (@inferred getindex(mm, :, SVector(2,1)))::SMatrix == @SMatrix [3 1; 4 2])
104+
@test (mm = MMatrix{2,2,Int}(undef); mm[1,:] = sm[1,:]; (@inferred getindex(mm, 1, :))::SVector == @SVector [1,3])
105+
@test (mm = MMatrix{2,2,Int}(undef); mm[:,1] = sm[:,1]; (@inferred getindex(mm, :, 1))::SVector == @SVector [1,2])
106106
end
107107

108108
@testset "3D scalar indexing" begin
@@ -112,7 +112,7 @@ using StaticArrays, Test
112112
@test sa[1,2,1] === 9
113113
@test sa[2,1,1] === 12
114114

115-
ma = MArray{Tuple{2,2,2}, Int}()
115+
ma = MArray{Tuple{2,2,2}, Int}(undef)
116116
@test (ma[1,1,2] = 8; ma[1,1,2] === 8)
117117
@test (ma[1,2,1] = 9; ma[1,2,1] === 9)
118118
@test (ma[2,1,1] = 12; ma[2,1,1] === 12)
@@ -126,7 +126,7 @@ using StaticArrays, Test
126126
@test sa[1,2,1,1] === 36
127127
@test sa[2,1,1,1] === 48
128128

129-
ma = MArray{Tuple{2,2,2,2}, Int}()
129+
ma = MArray{Tuple{2,2,2,2}, Int}(undef)
130130
@test (ma[1,1,1,2] = 30; ma[1,1,1,2] === 30)
131131
@test (ma[1,1,2,1] = 32; ma[1,1,2,1] === 32)
132132
@test (ma[1,2,1,1] = 36; ma[1,2,1,1] === 36)

test/mapreduce.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ end
77
@testset "map and map!" begin
88
v1 = @SVector [2,4,6,8]
99
v2 = @SVector [4,3,2,1]
10-
mv = MVector{4, Int}()
10+
mv = MVector{4, Int}(undef)
1111

1212
normal_v1 = [2,4,6,8]
1313
normal_v2 = [4,3,2,1]
@@ -19,10 +19,10 @@ end
1919

2020
map!(+, mv, v1, v2)
2121
@test mv == @MVector [6, 7, 8, 9]
22-
mv2 = MVector{4, Int}()
22+
mv2 = MVector{4, Int}(undef)
2323
map!(x->x^2, mv2, v1)
2424
@test mv2 == @MVector [4, 16, 36, 64]
25-
mv3 = MVector{4, Int}()
25+
mv3 = MVector{4, Int}(undef)
2626
v3 = @SVector [1, 2, 3, 4]
2727
map!(+, mv3, v1, v2, v3)
2828
@test mv3 == @MVector [7, 9, 11, 13]
@@ -110,8 +110,8 @@ end
110110
c = 2
111111
v1 = @SVector [2,4,6,8]
112112
v2 = @SVector [4,3,2,1]
113-
mv = MVector{4, Int}()
114-
mm = MMatrix{4, 2, Int}()
113+
mv = MVector{4, Int}(undef)
114+
mm = MMatrix{4, 2, Int}(undef)
115115
M = @SMatrix [1 2; 3 4; 5 6; 7 8]
116116

117117
@test_throws DimensionMismatch broadcast(+, v1, @SMatrix [4 3 2 1; 5 6 7 8])
@@ -126,7 +126,7 @@ end
126126
@test @inferred(broadcast(+, v1, v2)) === map(+, v1, v2)
127127
@test @inferred(broadcast(+, v1, M)) === @SMatrix [3 4; 7 8; 11 12; 15 16]
128128

129-
@test_throws DimensionMismatch broadcast!(-, MVector{5, Int}(), v1)
129+
@test_throws DimensionMismatch broadcast!(-, MVector{5, Int}(undef), v1)
130130

131131
broadcast!(-, mv, v1)
132132
@test mv == @MVector [-2, -4, -6, -8]

test/matrix_multiply.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,22 +226,22 @@ using StaticArrays, Test, LinearAlgebra
226226
m = @SMatrix [1 2; 3 4]
227227
n = @SMatrix [2 3; 4 5]
228228

229-
outvec = MVector{2,Int}()
229+
outvec = MVector{2,Int}(undef)
230230
mul!(outvec, m, v)
231231
@test outvec == @MVector [10,22]
232232
outvec2 = Vector{Int}(undef, 2)
233233
mul!(outvec2, m, v2)
234234
@test outvec2 == [10,22]
235235

236236
# Bad dimensions
237-
outvec_bad = MVector{3,Int}()
237+
outvec_bad = MVector{3,Int}(undef)
238238
@test_throws DimensionMismatch mul!(outvec_bad, m, v)
239239

240-
a = MMatrix{2,2,Int,4}()
240+
a = MMatrix{2,2,Int,4}(undef)
241241
mul!(a, m, n)
242242
@test a::MMatrix{2,2,Int,4} == @MMatrix [10 13; 22 29]
243243

244-
a = MMatrix{2,2,Int,4}()
244+
a = MMatrix{2,2,Int,4}(undef)
245245
mul!(a, m', n)
246246
@test a::MMatrix{2,2,Int,4} == @MMatrix [14 18; 20 26]
247247
mul!(a, m, n')
@@ -255,7 +255,7 @@ using StaticArrays, Test, LinearAlgebra
255255
mul!(a, transpose(m), transpose(n))
256256
@test a::MMatrix{2,2,Int,4} == @MMatrix [11 19; 16 28]
257257

258-
a2 = MArray{Tuple{2,2},Int,2,4}()
258+
a2 = MArray{Tuple{2,2},Int,2,4}(undef)
259259
mul!(a2, m, n)
260260
@test a2::MArray{Tuple{2,2},Int,2,4} == @MArray [10 13; 22 29]
261261

@@ -266,7 +266,7 @@ using StaticArrays, Test, LinearAlgebra
266266

267267
m_2 = MMatrix{10,10}(m_array_2)
268268
n_2 = MMatrix{10,10}(n_array_2)
269-
a_2 = MMatrix{10,10,Int}()
269+
a_2 = MMatrix{10,10,Int}(undef)
270270
mul!(a_2, m_2, n_2)
271271
@test a_2 == a_array_2
272272

@@ -277,7 +277,7 @@ using StaticArrays, Test, LinearAlgebra
277277

278278
m_3 = MMatrix{4,4}(m_array_3)
279279
n_3 = MMatrix{4,4}(n_array_3)
280-
a_3 = MMatrix{4,4,Float64}()
280+
a_3 = MMatrix{4,4,Float64}(undef)
281281
mul!(a_3, m_3, n_3)
282282
@test a_3 a_array_3
283283

@@ -287,7 +287,7 @@ using StaticArrays, Test, LinearAlgebra
287287

288288
m_4 = MMatrix{10,10}(m_array_4)
289289
n_4 = MMatrix{10,10}(n_array_4)
290-
a_4 = MMatrix{10,10,Float64}()
290+
a_4 = MMatrix{10,10,Float64}(undef)
291291
mul!(a_4, m_4, n_4)
292292
@test a_4 a_array_4
293293

@@ -297,7 +297,7 @@ using StaticArrays, Test, LinearAlgebra
297297

298298
m_5 = MMatrix{16,16}(m_array_5)
299299
n_5 = MMatrix{16,16}(n_array_5)
300-
a_5 = MMatrix{16,16,Int}()
300+
a_5 = MMatrix{16,16,Int}(undef)
301301
mul!(a_5, m_5, n_5)
302302
@test a_5 a_array_5
303303

@@ -307,7 +307,7 @@ using StaticArrays, Test, LinearAlgebra
307307

308308
m_6 = MMatrix{8,10}(m_array_6)
309309
n_6 = MMatrix{10,8}(n_array_6)
310-
a_6 = MMatrix{8,8,Int}()
310+
a_6 = MMatrix{8,8,Int}(undef)
311311
mul!(a_6, m_6, n_6)
312312
@test a_6 == a_array_6
313313

@@ -316,7 +316,7 @@ using StaticArrays, Test, LinearAlgebra
316316
vf2 = [2.0, 4.0]
317317
mf = @SMatrix [1.0 2.0; 3.0 4.0]
318318

319-
outvecf = MVector{2,Float64}()
319+
outvecf = MVector{2,Float64}(undef)
320320
mul!(outvecf, mf, vf)
321321
@test outvecf @MVector [10.0, 22.0]
322322
outvec2f = Vector{Float64}(undef, 2)

0 commit comments

Comments
 (0)