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

Array{Int,3}() should create an empty 3d array #14201

Closed
eschnett opened this issue Dec 1, 2015 · 14 comments
Closed

Array{Int,3}() should create an empty 3d array #14201

eschnett opened this issue Dec 1, 2015 · 14 comments

Comments

@eschnett
Copy link
Contributor

eschnett commented Dec 1, 2015

Array{Int,1}() and Array{Int,2}() create empty 1d- and 2d-arrays, respectively. This could also be implemented for all higher dimensions.

@rofinn
Copy link
Contributor

rofinn commented Dec 1, 2015

+1 I ran into this last week and had to do Array(Int, 2, 3, 4) instead.

@yuyichao
Copy link
Contributor

yuyichao commented Dec 1, 2015

Note that high dimension arrays cannot be resized so it's unclear what is Array{T,N}() with N >= 2 good for. On the other hand, I think it's useful to allow Array{Int,3}(2, 3, 4) in additional to Array{Int}(2, 3, 4) and Array(Int, 2, 3, 4).

@Rory-Finnegan If you want Array(Int, 2, 3, 4) an empty 3-d array won't be useful for you.

@rofinn
Copy link
Contributor

rofinn commented Dec 1, 2015

@yuyichao Sorry, my initial use case was an empty 3-d array, but when I couldn't figure out how to do that I just preallocated a large one and used sub once I had filled it in (not the best solution, but meh). In either case, it did surprise me that Array{Int,3}() and Array{Int,3}(2,3,4) weren't valid constructors.

@JeffBezanson
Copy link
Member

Honestly I'm not a fan of Array{Int,2}() making a 0x0 array. I find that both non-obvious and useless. +1 to the versions that accept size arguments though.

@GunnarFarneback
Copy link
Contributor

Note that high dimension arrays cannot be resized so it's unclear what is Array{T,N}() with N >= 2 good for.

Type-stable placeholders.

@nalimilan
Copy link
Member

+1, at the very least to ensure consistency.

@stevengj
Copy link
Member

Consistency with what?

@KristofferC
Copy link
Member

With N=1 and N=2?

@StefanKarpinski
Copy link
Member

StefanKarpinski commented Jan 30, 2017

I'm not sure why this works for N=1 and N=2 in the first place. That seems like a misfeature to me. It's as if we've decided that the default size of dimensions is zero but only if we don't specify any dimensions. What?!?! That strikes me as absurd and I'm somewhat tempted to find out when we started supporting this. (It's probably my fault, 😬 .) For N=0 it makes sense since there are no dimensions to specify.

@StefanKarpinski
Copy link
Member

StefanKarpinski commented Jan 30, 2017

The constructors with specified dimensions already all work:

Array{Int,3}((2,3,4))
Array{Int}((2,3,4))
Array{Int,3}(2,3,4)
Array{Int}(2,3,4)

@StefanKarpinski
Copy link
Member

The N=1 case actually kind of makes sense since we can at least grow those.

@martinholters
Copy link
Member

The problem I have with the N==1 case is that it makes one think that if Vector{Int}() is equivalent to Int[], then Vector{Int}(42) is equivalent to Int[42].

I agree that as a beginner, Vector{Int}() is one of the first things to try to create an empty vector. But you better learn using [] quickly anyway, as you'll need it for non-empty vectors of all kinds (concatenation, comprehension).

@StefanKarpinski
Copy link
Member

I'm more ok with Vector{Int}() and Vector() because zero seems like a sensible default for the length of a growable vector, but I agree that there's a solid argument to be made for its removal.

@KristofferC
Copy link
Member

Closed by #20330

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

10 participants