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

Give a meaningful error message for convert(SVector,::Vector) or SVector(::Vector) #259

Closed
dlfivefifty opened this issue Jul 27, 2017 · 2 comments

Comments

@dlfivefifty
Copy link
Member

In the FAQ it is explained why conversion of a Vector to an SVector is not allowed. While I'm not sure I agree with this, if the current behaviour is kept then the error message should be explanatory: instead of

julia> convert(SVector,[1,2,3])
ERROR: MethodError: Cannot `convert` an object of type Type{StaticArrays.SArray{Tuple{S},T,1,S} where T where S} to an object of type StaticArrays.Size
This may have arisen from a call to the constructor StaticArrays.Size(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] convert(::Type{StaticArrays.SArray{Tuple{S},T,1,S} where T where S}, ::Array{Int64,1}) at /Users/solver/.julia/v0.6/StaticArrays/src/convert.jl:17

It should say something like "Converting vectors to static vectors is not allowed to avoid type instability. Instead, use ...".

@andyferris
Copy link
Member

andyferris commented Aug 9, 2017

BTW, this is from a subtyping bug in Julia:

https://github.com/JuliaArrays/StaticArrays.jl/blob/master/src/traits.jl#L35-L49

I guess we could work around this and add messages to SArray, SMatrix, SizedArray, MVector, etc. I kind of expected that one to be fixed before they released v0.6, but oh well.

@c42f
Copy link
Member

c42f commented Jul 31, 2019

We now have the following error which solves this issue:

julia> convert(SVector, [1,2])
ERROR: The size of type `SArray{Tuple{S},T,1,S} where T where S` is not known.

If you were trying to construct (or `convert` to) a `StaticArray` you
may need to add the size explicitly as a type parameter so its size is
inferrable to the Julia compiler (or performance would be terrible). For
example, you might try

    m = zeros(3,3)
    SMatrix(m)      # this error
    SMatrix{3,3}(m) # correct - size is inferrable

@c42f c42f closed this as completed Jul 31, 2019
@c42f c42f mentioned this issue Oct 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants