- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
The first argument in Array{T}(undef, dims) is redundant #30603
Comments
This has already been discussed and it was decided to use |
You can also use |
Here's another set of useful cases that already work: julia> using SparseArrays
julia> Array{Int}(I, 5, 5)
5×5 Array{Int64,2}:
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
julia> SparseMatrixCSC{Int}(I, 5, 5)
5×5 SparseMatrixCSC{Int64,Int64} with 5 stored entries:
[1, 1] = 1
[2, 2] = 1
[3, 3] = 1
[4, 4] = 1
[5, 5] = 1
julia> Array(-2I, 5, 5)
5×5 Array{Int64,2}:
-2 0 0 0 0
0 -2 0 0 0
0 0 -2 0 0
0 0 0 -2 0
0 0 0 0 -2
julia> SparseMatrixCSC(-2I, 5, 5)
5×5 SparseMatrixCSC{Int64,Int64} with 5 stored entries:
[1, 1] = -2
[2, 2] = -2
[3, 3] = -2
[4, 4] = -2
[5, 5] = -2 The general principle is that to construct a collection, you do julia> Set([1, 4, 9, 16])
Set([4, 9, 16, 1])
julia> Set(i^2 for i=1:4)
Set([4, 9, 16, 1])
julia> Dict(i^2 => '*'^i for i=1:4)
Dict{Int64,String} with 4 entries:
4 => "**"
9 => "***"
16 => "****"
1 => "*" This has proved to be a very powerful, flexible and general pattern. Note that in the sparse matrix example, instead of needing a In 0.6, |
As KristofferC would say, you're in for a ride: #24595. Enjoy! :) |
In the uninitialized dense array constructor syntax
Array{T}(undef, dims)
theundef
is passed as the first argument. However,undef
is the only acceptable value for the first argument in that constructor, which suggests that it is redundant and shouldn't really be passed as an argument.Moreover, I would argue that it is misleading since having it as an explicit argument suggests that some other values are possible, whereas there is not really any variation there.
So seems to me that having
Array{T}(dims)
instead would be just as unambiguous and yet less verbose and more clearThe text was updated successfully, but these errors were encountered: