Parametrized types

I am looking at the source code to Lux.jl layers, and see the following struct for the Dense layer:

struct Dense{use_bias, F1, F2, F3} <: AbstractExplicitLayer

with four parametrized types. This is followed by the function

function (d::Dense{false})(x::AbstractVecOrMat, ps, st::NamedTuple)
    return d.activation.(ps.weight * x), st

I notice that in this function, Dense has only a single slot filled among the four: the first one. I have not seen a mention in the documentation that a struct could be referred to using only a subset of types. How should this be interpreted? I can assume that the other types remain arbitrary, so this is a specialization.

In that case, what if I’d like to specialize only the second type?



IIRC this is equivalent to

function (d::Dense{false,U,V,W})(x::AbstractVecOrMat, ps, st::NamedTuple) where {U,V,W}
    return d.activation.(ps.weight * x), st

If you would want to specify only the second one, I think you need to write

function (d::Dense{U,some_type})(x::AbstractVecOrMat, ps, st::NamedTuple) where U
   # ...

which again is short for

function (d::Dense{U,some_type,V,W})(x::AbstractVecOrMat, ps, st::NamedTuple) where {U,V,W}
   # ...

I wrote a macro a while ago that lets you avoid having to repeat the first type: GitHub - fatteneder/NamedTypeParameters.jl: Macro for naming type parameters in signatures
This is just a proof-of-principle and not well tested, not even registered, so use at your own risk.
Btw. I am not even sure if it work in the above case.

1 Like

Also: There is an issue on the 2.0 milestone about named parameter type support: Feature request: Named type parameters · Issue #32632 · JuliaLang/julia · GitHub

Thanks! No need for the macro, but I was wondering about the shortcut, since I had not read about it. Cheers!