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

Failure to reinterpret SMatrix #1201

Open
kbarros opened this issue Sep 22, 2023 · 5 comments
Open

Failure to reinterpret SMatrix #1201

kbarros opened this issue Sep 22, 2023 · 5 comments

Comments

@kbarros
Copy link

kbarros commented Sep 22, 2023

Is this expected behavior? Tested on [email protected].

using StaticArrays

# This gives a surprising error:
A = zeros(SMatrix{3,3,Float64}, 2, 3)
reinterpret(reshape, Float64, A) # ERROR: ArgumentError: cannot reinterpret `SMatrix{3, 3, Float64}` as `Float64`, type `SMatrix{3, 3, Float64}` is not a bits type

# However, it seems like `SMatrix{3, 3, Float64}` is indeed `isbits`
@assert isbits(A[1,1])

# And reinterpreting an `SVector` is fine
B = zeros(SVector{3,Float64}, 4)
reinterpret(reshape, Float64, B)
@mateuszbaran
Copy link
Collaborator

It works if you provide all arguments to SMatrix (which is strongly suggested):

A = zeros(SMatrix{3,3,Float64,9}, 2, 3)

Note that without ,9:

julia> isbitstype(eltype(A))
false

@kbarros
Copy link
Author

kbarros commented Sep 22, 2023

Thanks! I agree this is user error, feel free to close the issue.

I wonder, however, if there is some way to print a more informative error message? Something along the lines of "missing 4th type parameter".

@mateuszbaran
Copy link
Collaborator

I guess we could use Base.Experimental.register_error_hint but I'm not sure if that's the right thing to do. This error, actually, is far from the worst errors that I've seen and I haven't seen that much usage of register_error_hint across the ecosystem.

@kbarros
Copy link
Author

kbarros commented Sep 22, 2023

Thanks. Is there a valid use case for SMatrix{3,3,Float64} without the final fourth type parameter? If not, perhaps Julia could error if an object with such a type is being constructed? Besides this, I have no further comments.

@mateuszbaran
Copy link
Collaborator

You can use the three-parameter variant for constructing SMatrix objects, and the fourth one will be automatically added:

julia> SMatrix{2,2,Float64}(1, 2, 3, 4)
2×2 SMatrix{2, 2, Float64, 4} with indices SOneTo(2)×SOneTo(2):
 1.0  3.0
 2.0  4.0

Here we have an issue of zeros not trying to narrow down given eltype, but that's somewhat intentional (it's useful in different situations).

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

2 participants