Arrays with no size and/or non-sequential integers

I just stumbled on two phrases in the manual that puzzle me:

In Arrays · The Julia Language it says

Note that size may not be defined for arrays with non-standard indices, in which case axes may be useful. See the manual chapter on arrays with custom indices.

What kind of array doesn’t have a size defined? I can’t find an example, and the “chapter on arrays with custom indices” only says:

In some cases it may also be helpful to temporarily disable size and length for your new array type, since code that makes incorrect assumptions frequently uses these functions.

which sounds more like a temporary debug thing than a real array without size.

The other phrase is at Arrays with custom indices · The Julia Language :

For this reason, your best option may be to iterate over the array with eachindex(A) , or, if you require the indices to be sequential integers, to get the index range by calling LinearIndices(A) .

In which case would eachindex(A) give values that are not sequential integers?

The most common case I’m aware of involves arrays whose axes do not start at one (see GitHub - JuliaArrays/OffsetArrays.jl: Fortran-like arrays with arbitrary, zero or negative starting indices. ). Disabling size and length can help avoid users accidentally trying to do:

for i in 1:size(offset_array, 1)
  do_something(offset_array[i, ...])
end

which wrongly assumes that 1:size(...) is the correct set of indices.

Instead, users can be encouraged to do:

for i in axes(might_be_an_offset_array_or_not, 1)
  ...
end

which will work whether or not the array indices start at 1.

2 Likes