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

[bug] Type assert of filter fails for empty CircularBuffer #810

Open
kunzaatko opened this issue May 8, 2022 · 1 comment
Open

[bug] Type assert of filter fails for empty CircularBuffer #810

kunzaatko opened this issue May 8, 2022 · 1 comment

Comments

@kunzaatko
Copy link

Replicate

julia> cb = CircularBuffer(1)
> 0-element CircularBuffer{Any}

julia> filter(s -> s < 3, cb)
ERROR: TypeError: in typeassert, expected AbstractArray{Bool}, got a value of type Vector{Any}
Stacktrace:
 [1] filter(f::var"#25#26", a::CircularBuffer{Any})
   @ Base ./array.jl:2492
 [2] top-level scope
   @ REPL[27]:1

Specifying the eltype does not fix the issue

julia> cb = CircularBuffer{Integer}(1)
> 0-element CircularBuffer{Integer}

julia> filter(s -> s < 3, cb)
ERROR: TypeError: in typeassert, expected AbstractArray{Bool}, got a value of type Vector{Any}
Stacktrace:
 [1] filter(f::var"#39#40", a::CircularBuffer{Integer})
   @ Base ./array.jl:2492
 [2] top-level scope
   @ REPL[37]:1

Desired behaviour

It should behave the same way as with a Vector type.

julia> filter(s -> s < 3, [])
> Any[]

Possible solution

Since filter, returns a Vector type for CircularBuffer there are two solutions that both produce an ideologically different ouput:

  1. Specify the filter method for CircularBuffer to use the method with Array on the underlining buffer and not AbstractArray on the struct,
  2. Overwrite the filter method and make it return a new CircularBuffer.

I would think that the second option is the way to go since it would be easy for the user to use the method outputing Vector on demand.

Can you assign me?

@kunzaatko kunzaatko changed the title [bug] Type assert of filter fails for emtpy CircularBuffer [bug] Type assert of filter fails for empty CircularBuffer May 8, 2022
@laborg
Copy link
Collaborator

laborg commented Oct 3, 2023

This can be fixed by defining:

Base.IndexStyle(::Type{<:CircularBuffer}) = Base.IndexLinear()

Are there any downsides to this? If now I can prepare a PR.
Also fixed by #641

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