Skip to content

Commit b290391

Browse files
committed
🎉 at-deprecate ones 🎉
1 parent ec36230 commit b290391

File tree

4 files changed

+15
-29
lines changed

4 files changed

+15
-29
lines changed

NEWS.md

+5
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,11 @@ Deprecated or removed
534534
and/or shape depending on `opts...`. For an algebraic multiplicative identity,
535535
consider `one(A)` ([#24656]).
536536

537+
* `ones(dims...)` `and ones(T, dims)` have been deprecated. The replacements are
538+
`fill(1.0, dims...)` and `fill(one(T), dims...)`, but other common patterns, such as
539+
`x*ones(dims)` for some value `x` can, with advantage, instead be rewritten as
540+
`fill(x, dims)` ([#TBD]).
541+
537542
* The `Operators` module is deprecated. Instead, import required operators explicitly
538543
from `Base`, e.g. `import Base: +, -, *, /` ([#22251]).
539544

base/array.jl

+4-28
Original file line numberDiff line numberDiff line change
@@ -353,34 +353,10 @@ julia> zeros(Int8, 2, 3)
353353
"""
354354
function zeros end
355355

356-
"""
357-
ones([T=Float64,] dims...)
358-
359-
Create an `Array`, with element type `T`, of all ones with size specified by `dims`.
360-
See also [`zeros`](@ref), [`similar`](@ref).
361-
362-
# Examples
363-
```jldoctest
364-
julia> ones(1,2)
365-
1×2 Array{Float64,2}:
366-
1.0 1.0
367-
368-
julia> ones(ComplexF64, 2, 3)
369-
2×3 Array{Complex{Float64},2}:
370-
1.0+0.0im 1.0+0.0im 1.0+0.0im
371-
1.0+0.0im 1.0+0.0im 1.0+0.0im
372-
```
373-
"""
374-
function ones end
375-
376-
for (fname, felt) in ((:zeros, :zero), (:ones, :one))
377-
@eval begin
378-
$fname(::Type{T}, dims::NTuple{N, Any}) where {T, N} = fill!(Array{T,N}(uninitialized, Dims(dims)), $felt(T))
379-
$fname(dims::Tuple) = ($fname)(Float64, dims)
380-
$fname(::Type{T}, dims...) where {T} = $fname(T, dims)
381-
$fname(dims...) = $fname(dims)
382-
end
383-
end
356+
zeros(::Type{T}, dims::NTuple{N, Any}) where {T, N} = fill!(Array{T,N}(uninitialized, Dims(dims)), zero(T))
357+
zeros(dims::Tuple) = (zeros)(Float64, dims)
358+
zeros(::Type{T}, dims...) where {T} = zeros(T, dims)
359+
zeros(dims...) = zeros(dims)
384360

385361
function _one(unit::T, x::AbstractMatrix) where T
386362
m,n = size(x)

base/deprecated.jl

+6
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,12 @@ end
17251725
@deprecate zeros(a::AbstractArray, ::Type{T}) where {T} fill!(similar(a, T), 0)
17261726
@deprecate zeros(a::AbstractArray) fill!(similar(a), 0)
17271727

1728+
# deprecate ones
1729+
@deprecate ones(::Type{T}, dims::Tuple) where {T} fill(one(T), dims)
1730+
@deprecate ones(dims::Tuple) fill(1., dims)
1731+
@deprecate ones(::Type{T}, dims...) where {T} fill(one(T), dims...)
1732+
@deprecate ones(dims...) fill(1., dims...)
1733+
17281734
# PR #23711
17291735
@eval LibGit2 begin
17301736
@deprecate get_creds!(cache::CachedCredentials, credid, default) get!(cache, credid, default)

base/exports.jl

-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,6 @@ export
476476
minmax,
477477
ndims,
478478
nonzeros,
479-
ones,
480479
parent,
481480
parentindexes,
482481
partialsort,

0 commit comments

Comments
 (0)