What is the reason for a zero-dimensional array?

array

#1

Consider:

foo(v) = [x + 5 for x in v]

I think that in most cases if foo is called with a scalar, one wants a scalar value back, equivalent to foo(v) = v + 5. However, this function results in

julia> foo(5)
0-dimensional Array{Int64,0}:
10

which is type stable when v is an array, but not exactly what we want.

Is there a reason that a zero-dimensional array exists? How is it different from a scalar?


#2

0-dimensional arrays can be used to pass scalars to a function via “call-by-reference.” E.g.

   a = zeros()
   a[] = 7
   b = 7
   modify!(a,b)  # changes a but not b

   function modify!(a,b)
     a[] = 9
     b = 9
   end

#3

True, but perhaps Ref communicates this intent more clearly.