Skip to content

Commit e38a342

Browse files
committed
Support new zeros/ones methods from #19635
1 parent 9a95537 commit e38a342

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ Currently, the `@compat` macro supports the following syntaxes:
149149

150150
* `broadcast` is supported on tuples of the same lengths on 0.5. ([#16986])
151151

152+
* `zeros` and `ones` support an interface the same as `similar` ([#19635])
153+
152154
## Renamed functions
153155

154156
* `pointer_to_array` and `pointer_to_string` have been replaced with `unsafe_wrap(Array, ...)` and `unsafe_wrap(String, ...)` respectively
@@ -340,3 +342,4 @@ includes this fix. Find the minimum version from there.
340342
[#20414]: https://github.com/JuliaLang/julia/issues/20414
341343
[#20418]: https://github.com/JuliaLang/julia/issues/20418
342344
[#20500]: https://github.com/JuliaLang/julia/issues/20500
345+
[#19635]: https://github.com/JuliaLang/julia/issues/19635

src/Compat.jl

+12
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,18 @@ if VERSION < v"0.6.0-dev.2840"
14111411
IndexStyle(args...) = Base.linearindexing(args...)
14121412
end
14131413

1414+
if VERSION < v"0.6.0-dev.1653"
1415+
for (fname, felt) in ((:zeros,:zero), (:ones,:one))
1416+
@eval begin
1417+
# allow signature of similar
1418+
Base.$fname(a::AbstractArray, T::Type, dims::Tuple) = fill!(similar(a, T, dims), $felt(T))
1419+
Base.$fname(a::AbstractArray, T::Type, dims...) = fill!(similar(a,T,dims...), $felt(T))
1420+
Base.$fname(a::AbstractArray, T::Type=eltype(a)) = fill!(similar(a,T), $felt(T))
1421+
end
1422+
end
1423+
end
1424+
1425+
14141426
include("to-be-deprecated.jl")
14151427

14161428
end # module Compat

test/runtests.jl

+13
Original file line numberDiff line numberDiff line change
@@ -1763,4 +1763,17 @@ let a = CompatArray.CartesianArray(rand(2,3)), b = CompatArray.LinearArray(rand(
17631763
@test IndexStyle(b) === IndexLinear()
17641764
end
17651765

1766+
for (A,val) in ((zeros(1:5, Float32, 3, 2), 0),
1767+
(ones(1:5, Float32, 3, 2), 1),
1768+
(zeros(1:5, Float32, (3, 2)), 0),
1769+
(ones(1:5, Float32, (3, 2)), 1))
1770+
@test isa(A, Matrix{Float32}) && size(A) == (3,2) && all(x->x==val, A)
1771+
end
1772+
for (A,val) in ((zeros(1:5, Float32), 0),
1773+
(ones(1:5, Float32), 1))
1774+
@test isa(A, Vector{Float32}) && size(A) == (5,) && all(x->x==val, A)
1775+
end
1776+
17661777
include("to-be-deprecated.jl")
1778+
1779+
nothing

0 commit comments

Comments
 (0)