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

Usage of parametrized FieldVector can lead to StackOverflowError #968

Closed
StefanMathis opened this issue Oct 22, 2021 · 3 comments
Closed

Comments

@StefanMathis
Copy link

StefanMathis commented Oct 22, 2021

Hello,

I found quite some ways to produce StackOverflowError when dealing with a parametrized FieldVector type:

using StaticArrays

struct Point2D{T} <: FieldVector{2,T}
    x::T
    y::T
    Point2D(x,y) = new{eltype(x)}(x,y)
end

point1 = Point2D(0.0, 0.0)
point2 = Point2D(1.0, 0.0)

vec = [point1, point2]

point3 = Point2D(2.0, 0.0)

# This works
vec[2] = Point2D(2.0, 0.0)

# ==============================================================================
# Any of those one-liners lead to StackOverflow

# vec[2] ≈ point3

# test = point1 .+ point3

# test = vec[2] .+ point3

# vec[2] = Point2D(2, 0)

I kind of expected the last use case to fail, since I'm trying to add a Point2D{Int} to a vector of type Point2D{Float64}. However, the other use cases I expected to work. Is this a bug or should a user define corresponding functions for any of those use cases?

I should add that all of the shown use cases work fine when using a non-parametrized FieldVector.

@mateuszbaran
Copy link
Collaborator

You could just define Point2D{T}(x::T,y) where T = Point2D(x,y) and your example will work (except the last thing which really isn't supposed to work I think). I wouldn't call the need to define that constructor a bug, though it's a side-effect of a questionable part of static array construction code in StaticArrays.jl. It's a very central part of the design though so I'm afraid no one wants to touch that.

@andyferris
Copy link
Member

Hmm, interesting. That inner constructor seems wrong to me. I don’t have Julia to check what it should be right now, but type selection and conversion should happen separately. (There’s the tuple constructor and similar_type and so-on that also need to work.)

@StefanMathis
Copy link
Author

Thank you very much and sorry for the long delay, I didn't get around to test your suggestion. It works perfectly fine now, except for the last line (but as you said, this isn't supposed to work anyway). Just for reference, here is the code again with your suggestion included.

using StaticArrays

struct Point2D{T} <: FieldVector{2,T}
    x::T
    y::T
    Point2D(x,y) = new{eltype(x)}(x,y)
end
Point2D{T}(x::T,y) where T = Point2D(x,y)

point1 = Point2D(0.0, 0.0)
point2 = Point2D(1.0, 0.0)

vec = [point1, point2]

point3 = Point2D(2.0, 0.0)

# This works
vec[2] = Point2D(2.0, 0.0)

# ==============================================================================
# Those examples now all work

vec[2]  point3

test = point1 .+ point3

test = vec[2] .+ point3

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