Skip to content

Does collect always return an Array or can it return AbstractArrays? #50051

Open
@FelixBenning

Description

@FelixBenning

The documentation says Array

help?> collect

...
  collect(collection)

  Return an Array of all items in a collection or iterator. For dictionaries, returns Pair{KeyType, ValType}. If the argument is array-like or is an iterator with the HasShape trait, the result will have the same shape and number of dimensions as the argument.

but collect of Iterators.product breaks this promise:

julia> Iterators.product(OffsetArray(rand(3), -1:1), 1:2) |> collect
3×2 OffsetArray(::Matrix{Tuple{Float64, Int64}}, -1:1, 1:2) with eltype Tuple{Float64, Int64} with indices -1:1×1:2:
 (0.892917, 1)  (0.892917, 2)
 (0.398366, 1)  (0.398366, 2)
 (0.550785, 1)  (0.550785, 2)

I think returning an AbstractArray is fine if the documentation is updated. Interestingly collect(collect()) results in a normal Array. So OffsetArrays is following the documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]designDesign of APIs or of the language itselfdocsThis change adds or pertains to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions