# Why is length(Array{T}()) == 1, rather than 0?

8 messages
Open this post in threaded view
|

## Why is length(Array{T}()) == 1, rather than 0?

 If this was a bug I’m sure it would have been squashed already, so I’m not so worried that this is incorrect behavior per se, but I’m still curious: what’s the motivation for the following behavior? ``````julia> a = Array{Int}() 0-dimensional Array{Int64,0}: 2191825968 julia> length(a) 1 julia> a[1] 2191825968 ``````I would have expected a 0-dimensional array to contain no elements in any dimension, but this one seemingly has one (garbage) element anyway.I would have found the following more intuitive: ``````julia> a = Array{Int}() 0-dimensional Array{Int64,0} julia> length(a) 0 ``````// T ​
Open this post in threaded view
|

## Re: Why is length(Array{T}()) == 1, rather than 0?

 0 dimensional arrays are scalars, hence have one element.On Tue, Nov 24, 2015 at 2:20 AM, Tomas Lycken wrote:If this was a bug I’m sure it would have been squashed already, so I’m not so worried that this is incorrect behavior per se, but I’m still curious: what’s the motivation for the following behavior? ``````julia> a = Array{Int}() 0-dimensional Array{Int64,0}: 2191825968 julia> length(a) 1 julia> a[1] 2191825968 ``````I would have expected a 0-dimensional array to contain no elements in any dimension, but this one seemingly has one (garbage) element anyway.I would have found the following more intuitive: ``````julia> a = Array{Int}() 0-dimensional Array{Int64,0} julia> length(a) 0 ``````// T ​
Open this post in threaded view
|

## Re: Why is length(Array{T}()) == 1, rather than 0?

 Le mardi 24 novembre 2015 à 03:02 -0500, Keno Fischer a écrit : > 0 dimensional arrays are scalars, hence have one element. Yet this difference is a bit disturbing: julia> Array{Int}() 0-dimensional Array{Int64,0}: 139800461198672 julia> Vector{Int}() 0-element Array{Int64,1} Probably impossible to get rid of, though. Another small inconsistency, which could easily be fixed: julia> Array{Int,1}() 0-element Array{Int64,1} julia> Array{Int,0}() ERROR: MethodError: `convert` has no method matching convert(::Type{Array{Int64,0}}) Regards > On Tue, Nov 24, 2015 at 2:20 AM, Tomas Lycken <[hidden email] > > wrote: > > If this was a bug I’m sure it would have been squashed already, so > > I’m not so worried that this is incorrect behavior per se, but I’m > > still curious: what’s the motivation for the following behavior? > > > > julia> a = Array{Int}() > > 0-dimensional Array{Int64,0}: > > 2191825968 > > > > julia> length(a) > > 1 > > > > julia> a[1] > > 2191825968 > > I would have expected a 0-dimensional array to contain no elements > > in any dimension, but this one seemingly has one (garbage) element > > anyway. > > > > I would have found the following more intuitive: > > > > julia> a = Array{Int}() > > 0-dimensional Array{Int64,0} > > > > julia> length(a) > > 0 > > // T > > > >
Open this post in threaded view
|

## Re: Why is length(Array{T}()) == 1, rather than 0?

 It's also only a "garbage element" because you haven't set a value: julia> a = Array(Int) 0-dimensional Array{Int64,0}: 3 julia> a[] = 22 22 julia> a 0-dimensional Array{Int64,0}: 22 On Tuesday, November 24, 2015 09:52:18 AM Milan Bouchet-Valat wrote: > Yet this difference is a bit disturbing: > > julia> Array{Int}() > 0-dimensional Array{Int64,0}: > 139800461198672 > > julia> Vector{Int}() > 0-element Array{Int64,1} > > Probably impossible to get rid of, though. We could delete the fallback (see array.jl)     call{T}(::Type{Vector{T}}) = Array{T}(0) and force people to specify the size. --Tim
Open this post in threaded view
|

## Re: Why is length(Array{T}()) == 1, rather than 0?

 In reply to this post by Keno Fischer It does seem confusing that Array{Int}() implies zero dimensions (did the user forget the size parameter or did they intend a zero length dims tuple?). I think it would be more consistent to require either Array{Int,0}() or Array{Int}( () ), and the first form throws an error.On Tuesday, November 24, 2015 at 3:02:35 AM UTC-5, Keno Fischer wrote:0 dimensional arrays are scalars, hence have one element.On Tue, Nov 24, 2015 at 2:20 AM, Tomas Lycken <tomas....@...> wrote:If this was a bug I’m sure it would have been squashed already, so I’m not so worried that this is incorrect behavior per se, but I’m still curious: what’s the motivation for the following behavior? ``````julia> a = Array{Int}() 0-dimensional Array{Int64,0}: 2191825968 julia> length(a) 1 julia> a[1] 2191825968 ``````I would have expected a 0-dimensional array to contain no elements in any dimension, but this one seemingly has one (garbage) element anyway.I would have found the following more intuitive: ``````julia> a = Array{Int}() 0-dimensional Array{Int64,0} julia> length(a) 0 ``````// T ​
Open this post in threaded view
|

## Re: Why is length(Array{T}()) == 1, rather than 0?

 While I understand that zero-dimensional arrays are a bit confusing, arbitrarily disallowing the zero-argument case of the Array{T}(dims::Integer...) form is definitely not more consistent. On the contrary, it's the opposite of consistency – it's a special case which would need to be handled everywhere anyone writes uses this form to generically constructs n-d arrays.On Tuesday, November 24, 2015, Josh Langsfeld <[hidden email]> wrote:It does seem confusing that Array{Int}() implies zero dimensions (did the user forget the size parameter or did they intend a zero length dims tuple?). I think it would be more consistent to require either Array{Int,0}() or Array{Int}( () ), and the first form throws an error.On Tuesday, November 24, 2015 at 3:02:35 AM UTC-5, Keno Fischer wrote:0 dimensional arrays are scalars, hence have one element.On Tue, Nov 24, 2015 at 2:20 AM, Tomas Lycken wrote:If this was a bug I’m sure it would have been squashed already, so I’m not so worried that this is incorrect behavior per se, but I’m still curious: what’s the motivation for the following behavior? ``````julia> a = Array{Int}() 0-dimensional Array{Int64,0}: 2191825968 julia> length(a) 1 julia> a[1] 2191825968 ``````I would have expected a 0-dimensional array to contain no elements in any dimension, but this one seemingly has one (garbage) element anyway.I would have found the following more intuitive: ``````julia> a = Array{Int}() 0-dimensional Array{Int64,0} julia> length(a) 0 ``````// T ​
 Yeah, it's definitely consistent from the implementation perspective. But from the user's perspective, it's weird because the cases of 'failed to specify a size' and 'specified a dimension of 0' are ambiguous. It would be interesting to see if anyone has deliberately made use of the zero argument 'dims...' functionality.On Tuesday, November 24, 2015 at 4:58:23 PM UTC-5, Stefan Karpinski wrote:While I understand that zero-dimensional arrays are a bit confusing, arbitrarily disallowing the zero-argument case of the Array{T}(dims::Integer...) form is definitely not more consistent. On the contrary, it's the opposite of consistency – it's a special case which would need to be handled everywhere anyone writes uses this form to generically constructs n-d arrays.On Tuesday, November 24, 2015, Josh Langsfeld <jdl...@...> wrote:It does seem confusing that Array{Int}() implies zero dimensions (did the user forget the size parameter or did they intend a zero length dims tuple?). I think it would be more consistent to require either Array{Int,0}() or Array{Int}( () ), and the first form throws an error.On Tuesday, November 24, 2015 at 3:02:35 AM UTC-5, Keno Fischer wrote:0 dimensional arrays are scalars, hence have one element.On Tue, Nov 24, 2015 at 2:20 AM, Tomas Lycken wrote:If this was a bug I’m sure it would have been squashed already, so I’m not so worried that this is incorrect behavior per se, but I’m still curious: what’s the motivation for the following behavior? ``````julia> a = Array{Int}() 0-dimensional Array{Int64,0}: 2191825968 julia> length(a) 1 julia> a[1] 2191825968 ``````I would have expected a 0-dimensional array to contain no elements in any dimension, but this one seemingly has one (garbage) element anyway.I would have found the following more intuitive: ``````julia> a = Array{Int}() 0-dimensional Array{Int64,0} julia> length(a) 0 ``````// T ​
 Here, me. I am writing dimension-independent code to define vector spaces, and my tests explicitly include the zero-dimensional case. A common syntax is necessary. I think I specified the dimensions as tuple instead of a variable number of arguments, though.-erikOn Tue, Nov 24, 2015 at 8:04 PM, Josh Langsfeld wrote:Yeah, it's definitely consistent from the implementation perspective. But from the user's perspective, it's weird because the cases of 'failed to specify a size' and 'specified a dimension of 0' are ambiguous. It would be interesting to see if anyone has deliberately made use of the zero argument 'dims...' functionality.On Tuesday, November 24, 2015 at 4:58:23 PM UTC-5, Stefan Karpinski wrote:While I understand that zero-dimensional arrays are a bit confusing, arbitrarily disallowing the zero-argument case of the Array{T}(dims::Integer...) form is definitely not more consistent. On the contrary, it's the opposite of consistency – it's a special case which would need to be handled everywhere anyone writes uses this form to generically constructs n-d arrays.On Tuesday, November 24, 2015, Josh Langsfeld <[hidden email]> wrote:It does seem confusing that Array{Int}() implies zero dimensions (did the user forget the size parameter or did they intend a zero length dims tuple?). I think it would be more consistent to require either Array{Int,0}() or Array{Int}( () ), and the first form throws an error.On Tuesday, November 24, 2015 at 3:02:35 AM UTC-5, Keno Fischer wrote:0 dimensional arrays are scalars, hence have one element.On Tue, Nov 24, 2015 at 2:20 AM, Tomas Lycken wrote:If this was a bug I’m sure it would have been squashed already, so I’m not so worried that this is incorrect behavior per se, but I’m still curious: what’s the motivation for the following behavior? ``````julia> a = Array{Int}() 0-dimensional Array{Int64,0}: 2191825968 julia> length(a) 1 julia> a[1] 2191825968 ``````I would have expected a 0-dimensional array to contain no elements in any dimension, but this one seemingly has one (garbage) element anyway.I would have found the following more intuitive: ``````julia> a = Array{Int}() 0-dimensional Array{Int64,0} julia> length(a) 0 ``````// T ​ -- Erik Schnetter <[hidden email]> http://www.perimeterinstitute.ca/personal/eschnetter/