JuliaCollections / JuliaCollections/DataStructures.jl

[bug] Type assert of filter fails for empty `CircularBuffer`

Open
#810 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
745
Forks
261
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the failing dispatch shown at Base ./array.jl:2492 and inspect CircularBuffer's filter behavior. Reproduce the empty-buffer cases for Any and Integer element types, then compare them with filtering an empty Vector. Done means filtering an empty CircularBuffer no longer raises the reported type assertion error and matches the documented Vector behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.