StructArrays.jl icon indicating copy to clipboard operation
StructArrays.jl copied to clipboard

construct empty vector with `StructVector{Foo1}()`

Open goretkin opened this issue 4 years ago • 2 comments

julia> struct Foo1
           x::Int
           y::Int
       end

julia> Vector{Foo1}()
Foo1[]

julia> StructVector{Foo1}()
ERROR: type NamedTuple has no field x
Stacktrace:
 [1] (::Base.var"#164#167"{(:x, :y), NamedTuple{(), Tuple{}}})(n::Int64)
   @ Base ./none:0
 [2] iterate
   @ ./generator.jl:47 [inlined]
 [3] (NamedTuple{(:x, :y), T} where T<:Tuple)(nt::NamedTuple{(), Tuple{}})
   @ Base ./namedtuple.jl:99
 [4] (StructArray{Foo1, N, C, I} where {N, C<:Union{Tuple, NamedTuple}, I})(c::NamedTuple{(), Tuple{}})
   @ StructArrays ~/repos/StructArrays.jl/src/structarray.jl:92
 [5] #_#5
   @ ~/repos/StructArrays.jl/src/structarray.jl:100 [inlined]
 [6] StructArray
   @ ~/repos/StructArrays.jl/src/structarray.jl:100 [inlined]
 [7] #_#7
   @ ~/repos/StructArrays.jl/src/structarray.jl:109 [inlined]
 [8] (StructVector{Foo1, C, I} where {C<:Union{Tuple, NamedTuple}, I})()
   @ StructArrays ~/repos/StructArrays.jl/src/structarray.jl:109
 [9] top-level scope
   @ REPL[56]:1

goretkin avatar Apr 15 '21 05:04 goretkin

Adding the 1.0 milestone here because this would be a breaking change. There is a keyword constructor StructArray{T}(; kwargs...) to pass the named tuple of columns via kwargs (otherwise the extra parentheses get a bit annoying).

One reasonable (but still breaking) solution could be to only have the keyword constructor when no eltype parameter is passed, and create the empty StructArray otherwise.

Btw, I was actually not aware of the Vector{T}() constructor, is it also documented somewhere?

piever avatar Apr 18 '21 15:04 piever

Btw, I was actually not aware of the Vector{T}() constructor, is it also documented somewhere?

The punny constructors like Int64[] are defined in terms of e.g. Vector{Int64}() https://github.com/JuliaLang/julia/blob/fc02458492c60f6527245c6991f729c2a986f666/base/array.jl#L403-L411

It seems like the constructors don't have docstring: https://github.com/JuliaLang/julia/blob/fc02458492c60f6527245c6991f729c2a986f666/base/boot.jl#L472

Not sure if they're documented elsewhere.

goretkin avatar Apr 23 '21 00:04 goretkin