PS: `Array{Int64}()` is an array with 0 dimensions, "each" of which has
size 0 (so to say), leading to an overall length of 0^0 = 1 elements...

-erik

On Tue, Jun 7, 2016 at 11:01 PM, Erik Schnetter <[email protected]> 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]>
> 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]>
> http://www.perimeterinstitute.ca/personal/eschnetter/
>



-- 
Erik Schnetter <[email protected]>
http://www.perimeterinstitute.ca/personal/eschnetter/

Reply via email to