-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add empty
for AbstractArray
s and their types
#33482
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -672,9 +672,10 @@ similar(::Type{T}, shape::Tuple{Union{Integer, OneTo}, Vararg{Union{Integer, One | |||||
similar(::Type{T}, dims::Dims) where {T<:AbstractArray} = T(undef, dims) | ||||||
|
||||||
""" | ||||||
empty(v::AbstractVector, [eltype]) | ||||||
empty(a::AbstractArray, [eltype]) | ||||||
empty(a::Type{AbstractArray}, [eltype]) | ||||||
|
||||||
Create an empty vector similar to `v`, optionally changing the `eltype`. | ||||||
Create an empty array similar to `a`, optionally changing the `eltype`. | ||||||
|
||||||
# Examples | ||||||
|
||||||
|
@@ -687,6 +688,10 @@ julia> empty([1.0, 2.0, 3.0], String) | |||||
``` | ||||||
""" | ||||||
empty(a::AbstractVector{T}, ::Type{U}=T) where {T,U} = Vector{U}() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change this method also then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left that in for inlining purposes due to all the existing code depending on it. If that is not necessary, then I'll change it as well. |
||||||
function empty(a::Type{<:AbstractArray{T,N}}, ::Type{U}=T) where {T,N,U} | ||||||
convert(AbstractArray{U}, a(undef, Tuple(Iterators.repeated(0,N)))) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately I'm not sure Also this should be faster:
Suggested change
BTW, better use the short function syntax as for other definitions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the About the
Do you mean I should always use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
As I noted, the problem with
Yes it's fine to break after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That was the problem with it, I remember... So first we will have to keep the constructor by type, if I'm seeing this correctly? Until #20815 is merged.
I was thinking more in an aesthetic/readability sense. Only because it has been done in the past does not imply it has to be done again. But sure, I will change it! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I'm not even sure calling the constructor like that is guaranteed to work. Somebody else will have to confirm. |
||||||
end | ||||||
empty(a::AbstractArray{T}, ::Type{U}=T) where {T,U} = empty(typeof(a), U) | ||||||
|
||||||
# like empty, but should return a mutable collection, a Vector by default | ||||||
emptymutable(a::AbstractVector{T}, ::Type{U}=T) where {T,U} = Vector{U}() | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.