Skip to content

construct empty vector with StructVector{Foo1}() #183

Open

Description

@goretkin
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

Activity

  1. added this to the 1.0 milestone on Apr 18, 2021
  2. piever commented on Apr 18, 2021

    @piever
    Collaborator

    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?

  3. goretkin commented on Apr 23, 2021

    @goretkin
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions