Skip to content

Commit 9ecd842

Browse files
committed
Improve type stability
1 parent 1eac901 commit 9ecd842

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/blocksparsearrayinterface/blocksparsearrayinterface.jl

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,28 @@ end
4444
function blockstype(a::AbstractArray)
4545
return typeof(blocks(a))
4646
end
47+
48+
#=
49+
Ideally this would just be defined as `eltype(blockstype(a))`.
50+
However, BlockArrays.jl doesn't make `eltype(blocks(a))` concrete
51+
even when it could be
52+
(https://github.com/JuliaArrays/BlockArrays.jl/blob/v1.4.0/src/blocks.jl#L71-L74):
53+
```julia
54+
julia> eltype(blocks(BlockArray(randn(2, 2), [1, 1], [1, 1])))
55+
Matrix{Float64} (alias for Array{Float64, 2})
56+
57+
julia> eltype(blocks(BlockedArray(randn(2, 2), [1, 1], [1, 1])))
58+
AbstractMatrix{Float64} (alias for AbstractArray{Float64, 2})
59+
60+
julia> eltype(blocks(randn(2, 2)))
61+
AbstractMatrix{Float64} (alias for AbstractArray{Float64, 2})
62+
```
63+
Also note the current definition doesn't handle the limit
64+
when `blocks(a)` is empty
65+
=#
4766
function blocktype(a::AbstractArray)
4867
if isempty(blocks(a))
49-
# TODO: Unfortunately, this doesn't always give
50-
# a concrete type, even when it could be concrete, i.e.
51-
#=
52-
```julia
53-
julia> eltype(blocks(BlockArray(randn(2, 2), [1, 1], [1, 1])))
54-
Matrix{Float64} (alias for Array{Float64, 2})
55-
56-
julia> eltype(blocks(BlockedArray(randn(2, 2), [1, 1], [1, 1])))
57-
AbstractMatrix{Float64} (alias for AbstractArray{Float64, 2})
58-
59-
julia> eltype(blocks(randn(2, 2)))
60-
AbstractMatrix{Float64} (alias for AbstractArray{Float64, 2})
61-
```
62-
=#
63-
# That is an issue in BlockArrays.jl that we should address.
64-
return eltype(blocks(a))
68+
error("`blocktype` can't be determined if `isempty(blocks(a))`.")
6569
end
6670
return mapreduce(typeof, promote_type, blocks(a))
6771
end

0 commit comments

Comments
 (0)