Just as a small addendum to Erik's excellent answer:

One way to think about zero-dimensional arrays is by analogy with geometry. 
 Matrices are like planes, vectors are like lines, and zero-dimensional 
arrays are like points.  Points don't have any dimensions or lengths, but 
they still describe a location.

On Tuesday, June 7, 2016 at 10:01:05 PM UTC-5, Erik Schnetter wrote:
>
> The syntax for constructing arrays is `Array{Type}(size1, size2, ...)`. 
> You are not passing any values for `size`, thus you are constructing an 
> array that has no indices (zero dimensions). The way to access it is `x[]`, 
> i.e. an indexing expression without any indices. All zero-dimensional 
> arrays have one element -- there is no way for it to have zero elements; a 
> zero-dimensional array is very similar to a scalar value.
>
> Zero-dimensional arrays are confusing if you don't expect them; I assume 
> many people expect to obtain a one-dimensional array with zero elements 
> instead. (You can write `Vector{Int64}()` for this, or `Array{Int64}(0)`, 
> or Array{Int64,1}()`, or `Int64[]`.)
>
> To add to the confusion, Julia supports "linear indexing", which allows 
> you to index arrays with fewer or more (!) indices than declared. If you 
> use fewer indices, then the last index can be larger than its declared 
> bound; if there are more, then the additional indices all need to have the 
> value 1:
> ```Julia
> x = Vector{Int}(10)   # one dimension, ten elements
> x[1,1]   # this accesses the first element
> x[1,1,1,1,1]   # so does this
> ```
> This allows you to write `x[1]` in your case, although `x` actually 
> doesn't have any indices.
>
> Supporting zero-dimensional arrays is an important corner case.
>
> Linear indexing is also important, but might use a different syntax in the 
> future to avoid confusion.
>
> -erik
>
>
>
> On Tue, Jun 7, 2016 at 9:47 PM, David Parks <[email protected] 
> <javascript:>> wrote:
>
>> Hi Mauro, thanks for the response!
>> I get the logic behind an uninitialized array, but shouldn't such an 
>> array not return an iterable with 1 element of garbage? Why would it be 
>> initialized to having 1 random element and not 0?
>>
>> I would expect an empty constructor to do something reasonable. Perhaps 
>> it would be logical for these two statements to be equivalent (see bold 
>> lines below). I don't see the value of having a null constructor that 
>> generates an array that's iterable with exactly 1 element of garbage 
>> contents. Seems like a recipe for bugs. My bugs specifically, which is 
>> exactly what brought me to this topic. :)
>>
>> *julia> x = Array{Int64}()*
>> 0-dimensional Array{Int64,0}:
>> 2198389808
>>
>> julia> x[1]
>> 2198389808
>>
>> julia> x[2]
>> ERROR: BoundsError: attempt to access 0-dimensional Array{Int64,0}:
>> 2198389808
>>   at index [2]
>>  in getindex at array.jl:282
>>
>> *julia> x=Array{Int64}(0)*
>> 0-element Array{Int64,1}
>>
>> julia> x[1]
>> ERROR: BoundsError: attempt to access 0-element Array{Int64,1}
>>   at index [1]
>>  in getindex at array.jl:282
>>
>>
>>
>>
>>
>>
>>
>>
>
>
> -- 
> Erik Schnetter <[email protected] <javascript:>> 
> http://www.perimeterinstitute.ca/personal/eschnetter/
>

Reply via email to