Parametric supertyping using struct field parameters


I’m trying to play with supertypes but I’m encountering an issue so I made this MWE:

abstract type AbstractFoo{T} end

struct Foo{T} <: AbstractFoo{T}

So far so good. Now I would like to define a wrapper of Foo that has the same parametric supertype as its wrapped object. That means I need access to the parametres of the type of the wrapped foo. Ideally I would be able to do this:

struct Bar{F <: AbstractFoo} <: supertype(F)

But Julia does not allow this kind of stuff I recon. So I tried to access T directly like so

struct Bar{F <: AbstractFoo{T}} <: AbstractFoo{T} 

which tells me that T is not defined. How do I define it? Note that I use F <: AbstractFoo{T} because I would like foo to be any <: AbstractFoo even though I defined only one here.

struct Bar{T, F <: AbstractFoo{T}} <: AbstractFoo{T} 

Should work

1 Like

It does thanks!

So it’s mandatory to have the T redundantly appear twice in the Bar parameters?

It’s not redundant. Your Bar struct depends on two type parameters, so those need to be defined as such. And you can’t have a type parameter definition on the right of a <: symbol. You can at best use a type parameter there that has been defined earlier.