# Parametrized types

**URL:** <https://discourse.julialang.org/t/parametrized-types/92090>\
**Category:** General Usage\
**Created:** [December 24, 2022, 2:50pm UTC](https://discourse.julialang.org/t/parametrized-types/92090 "2022-12-24T14:50:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [December 24, 2022, 2:50pm UTC](https://discourse.julialang.org/t/parametrized-types/92090/1 "2022-12-24T14:50:53Z")

</div>

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

```julia
struct Dense{use_bias, F1, F2, F3} <: AbstractExplicitLayer
    activation::F1
    in_dims::Int
    out_dims::Int
    init_weight::F2
    init_bias::F3
end

```

with four parametrized types. This is followed by the function

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

```

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?

Thanks,

Gordon

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [December 24, 2022, 3:41pm UTC](https://discourse.julialang.org/t/parametrized-types/92090/2 "2022-12-24T15:41:49Z")

</div>

> [@erlebach](#):
>
> ```julia
> function (d::Dense{false})(x::AbstractVecOrMat, ps, st::NamedTuple)
> return d.activation.(ps.weight * x), st
> end
> 
> ```

IIRC this is equivalent to

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

```

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

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

```

which again is short for

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

```

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](https://github.com/fatteneder/NamedTypeParameters.jl)  
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.

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [December 24, 2022, 3:45pm UTC](https://discourse.julialang.org/t/parametrized-types/92090/3 "2022-12-24T15:45:18Z")

</div>

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](https://github.com/JuliaLang/julia/issues/32632)

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [December 24, 2022, 3:56pm UTC](https://discourse.julialang.org/t/parametrized-types/92090/4 "2022-12-24T15:56:07Z")

</div>

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