There is a problem with the type based similar method, applied to a view (SubArray), a reshaped array (Base.ReshapedArray) or a reinterpreted array (Base.ReinterpretArray), since these array modifications doe not have constructors, which can directly be called.
Here is an example (Julia 1.10.4):
julia> A = @view ones(10,10)[1:20];
julia> similar(typeof(A), axes(A));
ERROR: MethodError: no method matching SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}(::UndefInitializer, ::Tuple{Int64})
Closest candidates are:
SubArray{T, N, P, I, L}(::Any, ::Any, ::Any, ::Any) where {T, N, P, I, L}
@ Base subarray.jl:19
Stacktrace:
[1] similar(::Type{SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}}, dims::Tuple{Int64})
@ Base .\abstractarray.jl:877
There is a problem with the type based
similar
method, applied to a view (SubArray
), a reshaped array (Base.ReshapedArray
) or a reinterpreted array (Base.ReinterpretArray
), since these array modifications doe not have constructors, which can directly be called. Here is an example (Julia 1.10.4):I suggest the following fix in base/abstractarray.jl:
This should then unravel nested version of these array views. The exact Version details: