Skip to content

Undesirable automatic conversion fromVector{Vector{T}} to Vector{T} #56527

Open
@thchr

Description

@thchr

I accidentally stumbled on the following:

julia> v = Union{Vector{Int}, Nothing}[Vector{Int}[] for _ in 1:3]

This returns a Vector{Union{Vector{Int}, Nothing}} but, really, I think it should error. The issue is that the elements in [Vector{Int}[] for _ in 1:3] are not Vector{Int} but Vector{Vector{Int}}, so we have scenario of the kind Union{A, B}[c::C for c in ...] with C not a subtype of A or B.

The reason this works is that each of the Vector{Int}[] elements are passed on to the conversion machinery. In particular, as pointed out by @thofma on Slack, this is probably passed to one of these methods:

julia> convert(Vector{Int}, Vector{Int}[]) # Int64[]
julia> Vector{Int}(Vector{Int}[]) # Int64[]

It's hard to see where this would be desirable though. Additionally, the conversion only works if the to-be-converted-array is empty. That is, the following fails:

julia> convert(Vector{Int}, Vector{Int}[[1,2,3],[4,5]])
julia> Vector{Int}(Vector{Int}[[1,2,3],[4,5]])

It seems strange and error-prone to have conversion methods that only work for empty input.

Metadata

Metadata

Assignees

Labels

arrays[a, r, r, a, y, s]

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions