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

Implement three-argument similar methods #94

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/structarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,18 @@ end

Base.similar(s::StructArray, sz::Base.DimOrInd...) = similar(s, Base.to_shape(sz))
Base.similar(s::StructArray) = similar(s, Base.to_shape(axes(s)))
function Base.similar(s::StructArray{T}, sz::Tuple) where {T}
function Base.similar(s::StructArray{T,N,C}, ::Type{T}, sz::NTuple{N,Int64}) where {T, N, C<:Union{Tuple, NamedTuple}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this too strict? I'm trying to wrap my head around whether the eltype and the passed type should really match or if it's enough that one is a subtype of the other.

piever marked this conversation as resolved.
Show resolved Hide resolved
StructArray{T}(map(typ -> similar(typ, sz), fieldarrays(s)))
end

function Base.similar(s::StructArray{T,N,C}, S::Type, sz::NTuple{N,Int64}) where {T, N, C<:Union{Tuple, NamedTuple}}
piever marked this conversation as resolved.
Show resolved Hide resolved
# If not specified, we don't really know what kind of array to use for each
# interior type, so we just pick the first one arbitrarily. If users need
# something else, they need to be more specific.
f1 = fieldarrays(s)[1]
buildfromschema(typ -> similar(f1, typ, sz), S)
piever marked this conversation as resolved.
Show resolved Hide resolved
end

"""
`fieldarrays(s::StructArray)`

Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,9 @@ end
str = String(take!(io))
@test str == "StructArray(::Array{Int64,1}, ::Array{Int64,1})"
end

@testset "OffsetArray zero" begin
s = StructArray{ComplexF64}((rand(2), rand(2)))
soff = OffsetArray(s, 0:1)
@test isa(zero(soff).parent, StructArray)
end