# On what versions and OS's does ConstantArray[0, {}] work?

I've been using the combination

ConstantArray[0, {}]

(* 0 *)


in my code for some time, as a way for returning zero (i.e. a zero-dimensional array of zeroes) inside scalable code that can be made to produce empty arrays of arbitrary dimensions but also zeros. On my system (currently v11 over linux) it's always worked well since I introduced it.

However, when I tried to run the same code on other systems, you sometimes get the following:

ConstantArray[0, {}]
ConstantArray::ilsmn : Single or list of non-negative machine-sized integers
expected at position 2 of ConstantArray[0, {}]

(*Out[]:= ConstantArray[0, {}]*)


The above screenshot was taken over version 10 on OSX; a separate user also observed this in v8 over Windows 7.

I find this confusing, and I'm pretty sure it's a bug. In particular, the documentation for ConstantArray claims that its history is

Introduced in 2007 (6.0) | Updated in 2008 (7.0)

which means that there should be no change between v8 and v11. I will file a bug report shortly, but mostly I would like to build a workaround (since I want my code to be usable on systems like e.g. v10/OSX). At present, thus, my main question is: on which systems does this behaviour show up, and on which ones does it return a plain 0 without complaints? I suspect this depends both on the specific version and the operating system, but it'd be nice to know for sure.

• Seems to be OK in 11.01, Mac os – bill s Dec 8 '16 at 15:33
• Works for me on 11.0.1, Windows 10 – lowriniak Dec 8 '16 at 15:37
• I tried it on 10.4.1 running on OS X. No problem. – m_goldberg Dec 8 '16 at 15:43
• This fails in version 7.0.1 but works in 10.1.0 under Windows 7 x64. – Mr.Wizard Dec 8 '16 at 15:47
• I don't think you can call it a bug since there is nothing in the docs that indicates the zero should be expected behavior. The docs do say ConstantArray[c,dims] has Dimensions[dims] which fails here as Dimensions[{}] != Dimensions[0] – george2079 Dec 8 '16 at 15:57

This was a change introduced in version 10.1.0 (on all platforms).

The new behavior is more consistent with Array, e.g.

Array[f, {}]

(* f[] *)

Array[Function[0], {}]

(* 0 *)
`
• Thanks for this. So do all <10.1.0 versions produce the same error and the unevaluated return? – Emilio Pisanty Dec 8 '16 at 16:52
• (Either way, it still strikes me as a potential documentation bug - if this behaviour is settled, shouldn't it be reported in the documentation, and marked as the last change in the function?) – Emilio Pisanty Dec 8 '16 at 16:53
• Yes (starting with 6.0 of course). – ilian Dec 8 '16 at 16:53
• In some cases it would be safer to interpret the 'last updated in' markup as referring to the documentation page content, not necessarily the function implementation. – ilian Dec 8 '16 at 16:56