Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nsajko opened this issue Oct 15, 2024 · 1 comment

Comments

@nsajko
Copy link
Collaborator

nsajko commented Oct 15, 2024

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
@oscardssmith
Copy link
Collaborator

this seems like the better default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants