Stream: helpdesk (published)

Topic: Parametrization of Parametric type


view this post on Zulip Max Köhler (Jun 16 2021 at 14:38):

while porting some array wrapper to a GPU, its quite nice to use Adapt.jl to adapt the underlying arrays.
However it feels a bit nasty to parametrize a struct on the full array type, lets say we have

struct Foo
    x::CuArray{Bar,1}
end

in order to use Adapt.jl I'm currently doing

struct Foo{T}
    x::T
end

which would end up in e.g. Foo{CuArray{Bar,1}}

Is there a way to do something like

struct Foo{T<:AbstractArray,arrtype}
    x::T{arrtype,1}
end

In order to get back to Foo{Bar}?

view this post on Zulip Max Köhler (Jun 16 2021 at 14:44):

had the idea, immediately after writing the post, but maybe it's also helpful for others. One possible solution is

struct Foo{arrtype,T<:DenseArray{arrtype,1}}
    x::T
end

it's not Foo{Bar} but with this I can dispatch again on Foo{Bar}

view this post on Zulip Jakob Nybo Nissen (Jun 16 2021 at 14:47):

It is, unfortunately, not possible to do "computation" in type parameters when defining a struct, so you can't ask it to "extract" some type parameters, if that makes sense

view this post on Zulip Jakob Nybo Nissen (Jun 16 2021 at 14:48):

But I'm not clear exactly what you're asking. If you want to obtain a Foo{Bar}, you can simply do Foo(x::Bar), right?

view this post on Zulip Max Köhler (Jun 16 2021 at 14:50):

so, T should be always some type of Array, CuArray or when on device CuDeviceArray. Now I still want the underlying array element type as a parameter of Foo, but you're right I could just get it inside the function. Maybe that's still better than introducing another type parameter


Last updated: Dec 28 2024 at 04:38 UTC