Skip to content
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

Closed
AzamatB opened this issue Jan 5, 2019 · 4 comments
Closed

The first argument in Array{T}(undef, dims) is redundant #30603

AzamatB opened this issue Jan 5, 2019 · 4 comments

Comments

@AzamatB
Copy link
Contributor

AzamatB commented Jan 5, 2019

In the uninitialized dense array constructor syntax Array{T}(undef, dims) the undef 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 clear

@KristofferC
Copy link
Member

KristofferC commented Jan 5, 2019

This has already been discussed and it was decided to use undef to be explicit.

@fredrikekre
Copy link
Member

However, undef is the only acceptable value for the first argument in that constructor

You can also use missing and nothing, and there are plans for other things too.

@StefanKarpinski
Copy link
Member

StefanKarpinski commented Jan 5, 2019

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 T(init) where T is the type of the collection to construct and init is an initializer or iterable that provides the values. Examples:

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 speye function to construct a sparse identity matrix—and a corresponding spxxx for whatever other kind of data pattern you might want—the type that you want to construct (Array{Int}, SparseMatrixCSC) and what you want to fill it with (I, -2I) are orthogonal and you can use the same construction for any kind of array.

In 0.6, Array was the only collection in Base that didn't follow this pattern, so it was brought into line with the others in 1.0 and now the pattern is consistent throughout Julia's built-in collections.

@Sacha0
Copy link
Member

Sacha0 commented Jan 6, 2019

As KristofferC would say, you're in for a ride: #24595. Enjoy! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants