SparseArrays.jl icon indicating copy to clipboard operation
SparseArrays.jl copied to clipboard

similar(SparseVector{Float64,Int}, 5) throws error

Open dlfivefifty opened this issue 7 years ago • 3 comments

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 

dlfivefifty avatar Nov 26 '17 11:11 dlfivefifty

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!

Sacha0 avatar Nov 26 '17 17:11 Sacha0

Thanks for the links, I agree the current behaviour is confusing.

dlfivefifty avatar Nov 26 '17 17:11 dlfivefifty

Bump. Perhaps this can be closed or the syntax needs to be updated to be current, if still an issue.

ViralBShah avatar May 30 '18 03:05 ViralBShah