Skip to content

Commit 86bd62b

Browse files
timholyTotalVerb
authored andcommitted
Support new zeros/ones methods from #19635 (#330)
1 parent ac46027 commit 86bd62b

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

README.md

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

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

156+
* `zeros` and `ones` support an interface the same as `similar` ([#19635])
157+
156158
## Renamed functions
157159

158160
* `pointer_to_array` and `pointer_to_string` have been replaced with `unsafe_wrap(Array, ...)` and `unsafe_wrap(String, ...)` respectively
@@ -346,3 +348,4 @@ includes this fix. Find the minimum version from there.
346348
[#20414]: https://github.com/JuliaLang/julia/issues/20414
347349
[#20418]: https://github.com/JuliaLang/julia/issues/20418
348350
[#20500]: https://github.com/JuliaLang/julia/issues/20500
351+
[#19635]: https://github.com/JuliaLang/julia/issues/19635

src/Compat.jl

+11
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,17 @@ if VERSION < v"0.6.0-dev.2840"
14211421
IndexStyle(args...) = Base.linearindexing(args...)
14221422
end
14231423

1424+
if VERSION < v"0.6.0-dev.1653"
1425+
for (fname, felt) in ((:zeros,:zero), (:ones,:one))
1426+
@eval begin
1427+
# allow signature of similar
1428+
Base.$fname(a::AbstractArray, T::Type, dims::Tuple) = fill!(similar(a, T, dims), $felt(T))
1429+
Base.$fname(a::AbstractArray, T::Type, dims...) = fill!(similar(a,T,dims...), $felt(T))
1430+
Base.$fname(a::AbstractArray, T::Type=eltype(a)) = fill!(similar(a,T), $felt(T))
1431+
end
1432+
end
1433+
end
1434+
14241435
# https://github.com/JuliaLang/julia/pull/20203
14251436
if VERSION < v"0.6.0-dev.2283"
14261437
# not exported

test/runtests.jl

+13
Original file line numberDiff line numberDiff line change
@@ -1773,9 +1773,22 @@ let a = CompatArray.CartesianArray(rand(2,3)), b = CompatArray.LinearArray(rand(
17731773
@test IndexStyle(b) === IndexLinear()
17741774
end
17751775

1776+
for (A,val) in ((zeros(1:5, Float32, 3, 2), 0),
1777+
(ones(1:5, Float32, 3, 2), 1),
1778+
(zeros(1:5, Float32, (3, 2)), 0),
1779+
(ones(1:5, Float32, (3, 2)), 1))
1780+
@test isa(A, Matrix{Float32}) && size(A) == (3,2) && all(x->x==val, A)
1781+
end
1782+
for (A,val) in ((zeros(1:5, Float32), 0),
1783+
(ones(1:5, Float32), 1))
1784+
@test isa(A, Vector{Float32}) && size(A) == (5,) && all(x->x==val, A)
1785+
end
1786+
17761787
# PR 20203
17771788
@test Compat.readline(IOBuffer("Hello, World!\n")) == "Hello, World!"
17781789
@test Compat.readline(IOBuffer("x\n"), chomp=true) == "x"
17791790
@test Compat.readline(IOBuffer("x\n"), chomp=false) == "x\n"
17801791

17811792
include("to-be-deprecated.jl")
1793+
1794+
nothing

0 commit comments

Comments
 (0)