Skip to content

reconsider behavior of collect_as for empty input, regarding output element type and use of return type inference #72

@nsajko

Description

@nsajko

Current behavior:

julia> using FixedSizeArrays

julia> FixedSizeArrays.collect_as(FixedSizeVector, (n*n for n  1:0))
0-element FixedSizeVector{Any, Memory{Any}}

julia> collect((n*n for n  1:0))
Int64[]

We return an empty array with eltype of Any, while collect from Base returns an empty array with an element type of Int. Neither of these is ideal:

  • Any is obviously suboptimal
  • Int seems nice at first, but necessitates the use of return type inference. This is quite undesirable:
    • It inhibits compiler optimization AFAIK
      • in particular, the :foldable effect requires :nortcall
    • It makes the behavior depend on type inference, which makes for a terrible programming and debugging experience.

Unlike Base, we're not yet constrained by compatibility considerations, so I think we should:

  • avoid making our behavior depend on type inference (in particular, I think this means not doing things like map(identity, some_array) in the implementation of collect_as)
  • for empty input, we could do something like this:
    • set the output element type to Union{}, for an input element type equal to Any
    • set the output element type to the input element type, when it's not equal to Any

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions