Best way to declare parametric types of parametric types

Suppose I have a type, let’s say

struct MyPoint{T<:AbstractFloat}
    x::T
    y::T
end

My understanding is that this is the best way to declare parametric types as per the Julia performance tips. But suppose you have another type that depends on MyPoint.

struct MyVector{T<:AbstractFloat}
    p1::MyPoint{T}
    p2::MyPoint{T}
    some_parameter::T
end

Is this how that should be done? Or some other way? Should I define abstract type AbstractPoint end , then make MyPoint a subtype of AbstractPoint and then have

struct MyVector{P<:AbstractPoint, T<:AbstractFloat}
    p1::P
    p2::P
    some_parameter::T
end

Is there any reason to do that if MyPoint would be the only subtype of AbstractPoint?

You can do either in this case, yes. They are the same, with the only difference being that P is less restricted in the second case, should some new subtype for AbstractPoint be created.

1 Like

Great, thanks!

Not only AbstractPoint allows for people to create new subtypes and use them, but also does not guarantee that the AbstractPoint stores values of type T inside it. I do not believe they will have similar performance, the first option will probably always have the best performance.