Subtype a struct in package extension, but defining the struct in the Main Package code

While trying to transfer a package’s @requires into extensions, I wanted to take the structs into the Main package. However, one of the structs is defined as a subtype of a weakdep. So something like:

In Foo/ext/FooBarExt.jl:

using Bar: BarType
struct ExtType{T} <: BarType
    a::T
end

My thoughts for a solution go something like:

In Foo/src/Foo.jl:

struct ExtType{T} <: SuperType
    a::T
    SuperType::DataType
end

Where SuperType will then be somehow redefined in FooBarExt as BarType. Otherwise perhaps an “assignment” like:

ExtType <: BarType

In the extension.
Any clues on how to possibly solve this?

I don’t think you can do that, as it would lead to two different implementations of the same type. If you want to subtype a struct, the parent (abstract) type must be known at compilation time. It can’t be loaded from an extension later.

Many packages provide base packages with minimal interfaces and base classes exactly for that reason: to enable package developers to define compatible interfaces without having to depend on the full implementation. Perhaps this could work for you.

Alternatively, you can define a non-subtyped struct in the main package, and wrap it in a subtyped wrapper struct in the extension.

# in the main module
struct MyType
    a::T
end

# in the extention module
struct MyTypeExt <: BarType
    v::MyType
end

Depending on how you use it, this could work, too. You might need to adjust your function signatures a bit, though.