Stream: helpdesk (published)

Topic: ✔ Subtyping multiple abstract types


view this post on Zulip Brenhin Keller (Feb 13 2022 at 17:13):

Say I want to have a type that looks something like

    struct MyVectorType{T} <: AbstractVector{T}
        pointer::Ptr{T}
        length::Int
    end

but I also want MyVectorType to be a subtype of MyOtherAbstractTypeForDispatch...

Can this be done? I haven't seemed to be able to make it work yet

view this post on Zulip Cédric Belmant (Feb 13 2022 at 17:54):

It cannot, see https://github.com/JuliaLang/julia/issues/5
To achieve the desired behavior for dispatch you would need to use traits. Sadly there's no way to achieve this behavior ad-hoc if the functions to dispatch to weren't implemented with traits in mind.
One possibility may be to define another type which subtypes MyOtherAbstractTypeForDispatch and have conversion methods between MyVectorType and the new subtype so that you can convert back and forth depending on what dispatch behavior you want to achieve.

view this post on Zulip awnmp (Feb 13 2022 at 17:55):

Typing in Julia makes a tree.
And there are always (AFAIK) one and only one direct supertype of a type.

So, I don't think what you're trying to do is possible.

You can use Union though, if you don't mind a bit of unstability for your pointer.

view this post on Zulip Notification Bot (Feb 13 2022 at 17:59):

Brenhin Keller has marked this topic as resolved.

view this post on Zulip Brenhin Keller (Feb 13 2022 at 17:59):

Thanks!


Last updated: Nov 06 2024 at 04:40 UTC