Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
goretkin opened this issue Apr 15, 2021 · 2 comments
Open

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

goretkin opened this issue Apr 15, 2021 · 2 comments
Milestone

Comments

@goretkin
Copy link
Contributor

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
@piever piever added this to the 1.0 milestone Apr 18, 2021
@piever
Copy link
Collaborator

piever commented Apr 18, 2021

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?

@goretkin
Copy link
Contributor Author

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants