# Generic Associated Types

**URL:** <https://discourse.julialang.org/t/generic-associated-types/105829>\
**Category:** General Usage\
**Tags:** parametric-types, type-stability\
**Created:** [November 5, 2023, 6:23pm UTC](https://discourse.julialang.org/t/generic-associated-types/105829 "2023-11-05T18:23:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HashBrown](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hashbrown/32/43601_2.png) [@HashBrown](https://discourse.julialang.org/u/HashBrown)\
**Post date:** [November 5, 2023, 6:23pm UTC](https://discourse.julialang.org/t/generic-associated-types/105829/1 "2023-11-05T18:23:08Z")

</div>

Hi, I am looking to define a type as below:

```julia
gat1(::Type{Float64}) = Int64
gat2(::Type{Float64}) = String

struct Foo{T}
    a::gat1(T)
    b::gat2(T)
end

f = Foo{Float64}(10, "Hello")

```

Is there any way to get generic assoicated types in Julia?

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [November 5, 2023, 6:29pm UTC](https://discourse.julialang.org/t/generic-associated-types/105829/2 "2023-11-05T18:29:08Z")

</div>

I am unsure if it is still maintained but you may find [GitHub - jonniedie/ConcreteStructs.jl: 🏩🏠🌆🏨🌇🏦](https://github.com/jonniedie/ConcreteStructs.jl) useful (+ using whatever inner constructor you want to pass values into the struct)

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [November 5, 2023, 6:31pm UTC](https://discourse.julialang.org/t/generic-associated-types/105829/3 "2023-11-05T18:31:54Z")

</div>

You want [ComputedFieldTypes.jl](https://github.com/vtjnash/ComputedFieldTypes.jl):

```julia
julia> using ComputedFieldTypes

julia> gat1(::Type{Float64}) = Int64
gat1 (generic function with 1 method)

julia> gat2(::Type{Float64}) = String
gat2 (generic function with 1 method)

julia> @computed struct Foo{T}
           a::gat1(T)
           b::gat2(T)
       end

julia> Foo{Float64}(10, "Hello")
Foo{Float64, Int64, String}(10, "Hello")

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 5, 2023, 6:33pm UTC](https://discourse.julialang.org/t/generic-associated-types/105829/4 "2023-11-05T18:33:05Z")

</div>

There’s no way around aliasing an underlying `Foo{T, U, V}`, otherwise associating types like that makes unstable concrete types. Say after the code example, you redefine `gat1(::Type{Float64}) = Ref{Int32}`. Now `Foo{Float64}` refers to a different underlying type.
