From 86bd62b86a40bdf243e544a1933518c8c9aea02e Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Thu, 23 Mar 2017 19:13:32 -0500 Subject: [PATCH] Support new zeros/ones methods from #19635 (#330) --- README.md | 3 +++ src/Compat.jl | 11 +++++++++++ test/runtests.jl | 13 +++++++++++++ 3 files changed, 27 insertions(+) diff --git a/README.md b/README.md index 74990b5c4746c..992414d30823e 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,8 @@ Currently, the `@compat` macro supports the following syntaxes: * `broadcast` is supported on tuples of the same lengths on 0.5. ([#16986]) +* `zeros` and `ones` support an interface the same as `similar` ([#19635]) + ## Renamed functions * `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. [#20414]: https://github.com/JuliaLang/julia/issues/20414 [#20418]: https://github.com/JuliaLang/julia/issues/20418 [#20500]: https://github.com/JuliaLang/julia/issues/20500 +[#19635]: https://github.com/JuliaLang/julia/issues/19635 diff --git a/src/Compat.jl b/src/Compat.jl index d4ce559a9e98d..d42e33c694fea 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -1421,6 +1421,17 @@ if VERSION < v"0.6.0-dev.2840" IndexStyle(args...) = Base.linearindexing(args...) end +if VERSION < v"0.6.0-dev.1653" + for (fname, felt) in ((:zeros,:zero), (:ones,:one)) + @eval begin + # allow signature of similar + Base.$fname(a::AbstractArray, T::Type, dims::Tuple) = fill!(similar(a, T, dims), $felt(T)) + Base.$fname(a::AbstractArray, T::Type, dims...) = fill!(similar(a,T,dims...), $felt(T)) + Base.$fname(a::AbstractArray, T::Type=eltype(a)) = fill!(similar(a,T), $felt(T)) + end + end +end + # https://github.com/JuliaLang/julia/pull/20203 if VERSION < v"0.6.0-dev.2283" # not exported diff --git a/test/runtests.jl b/test/runtests.jl index 27f875b95a296..b6b1bd22a6b42 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1773,9 +1773,22 @@ let a = CompatArray.CartesianArray(rand(2,3)), b = CompatArray.LinearArray(rand( @test IndexStyle(b) === IndexLinear() end +for (A,val) in ((zeros(1:5, Float32, 3, 2), 0), + (ones(1:5, Float32, 3, 2), 1), + (zeros(1:5, Float32, (3, 2)), 0), + (ones(1:5, Float32, (3, 2)), 1)) + @test isa(A, Matrix{Float32}) && size(A) == (3,2) && all(x->x==val, A) +end +for (A,val) in ((zeros(1:5, Float32), 0), + (ones(1:5, Float32), 1)) + @test isa(A, Vector{Float32}) && size(A) == (5,) && all(x->x==val, A) +end + # PR 20203 @test Compat.readline(IOBuffer("Hello, World!\n")) == "Hello, World!" @test Compat.readline(IOBuffer("x\n"), chomp=true) == "x" @test Compat.readline(IOBuffer("x\n"), chomp=false) == "x\n" include("to-be-deprecated.jl") + +nothing