SparseArrays.jl
SparseArrays.jl copied to clipboard
similar(SparseVector{Float64,Int}, 5) throws error
I would expect similar to work for creating empty sparse vectors:
julia> similar(SparseVector{Float64,Int}, 5)
ERROR: MethodError: Cannot `convert` an object of type Tuple{Int64} to an object of type SparseVector{Float64,Int64}
This may have arisen from a call to the constructor SparseVector{Float64,Int64}(...),
since type constructors fall back to convert methods.
Stacktrace:
[1] SparseVector{Float64,Int64}(::Tuple{Int64}) at ./sysimg.jl:114
[2] similar(::Type, ::Int64) at ./abstractarray.jl:567
[3] top-level scope
julia> similar(Vector{Float64}, 5)
5-element Array{Float64,1}:
2.242449217e-314
2.2405945655e-314
2.240475286e-314
2.2860478697e-314
2.286045435e-314
Leaving a breadcrumb to this thread in JuliaLang/julia#18161 (re. similar redesign). Linking JuliaLang/julia#17124, JuliaLang/julia#17201, and JuliaLang/julia#17137 which led to introduction of the Array-type-accepting similar signatures. Regarding things near term, the sparse array constructors are undergoing a bit of evolution, and soon you should be able to call the constructor itself for this operation. Best!
Thanks for the links, I agree the current behaviour is confusing.
Bump. Perhaps this can be closed or the syntax needs to be updated to be current, if still an issue.