Skip to content

push!! accepts non-collections, wraps them in Vector #41

@mhauru

Description

@mhauru
julia> BangBang.push!!(:a, 0, identity, "haha")
4-element Vector{Any}:
 :a
 Symbol("[0]")
 identity (generic function with 5 methods)
 "haha"

I think the above should result in a MethodError saying a Symbol is not a collection, you can't push!! to it. The current behaviour caused us some trouble in https://github.com/TuringLang/DynamicPPL.jl/ by resulting in a hard to understand downstream error, when we were accidentally calling push!! with the wrong arguments.

The cause of the current behaviour is that push!! falls back on push, which has this method:

push(xs, x) = vcat(xs, singletonof(xs, x))

and it turns out you can vcat all sorts of nonsense like this:

julia> vcat(:a, [0.2])
2-element Vector{Any}:
  :a
 0.2

I think it's questionable whether the above behaviour by vcat is desirable, but it's a bit more understandable since at least one expects an array from a vcat call, where as getting a Vector of stuff when calling push!! on a bunch of arguments none of which is a Vector is quite surprising.

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