# Good way to parametrize over type parameter?

**URL:** <https://discourse.julialang.org/t/good-way-to-parametrize-over-type-parameter/132533>\
**Category:** New to Julia\
**Tags:** parametric-types, parametric-methods, function-parameters\
**Created:** [September 21, 2025, 8:28am UTC](https://discourse.julialang.org/t/good-way-to-parametrize-over-type-parameter/132533 "2025-09-21T08:28:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![loisel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/loisel/32/50626_2.png) [@loisel](https://discourse.julialang.org/u/loisel)\
**Post date:** [September 21, 2025, 8:28am UTC](https://discourse.julialang.org/t/good-way-to-parametrize-over-type-parameter/132533/1 "2025-09-21T08:28:11Z")

</div>

Hi,

I’m writing a numerical code that is parametrized by T, which is typically T=Float64 but could be one of the other number types. It’s a PDE solver so I’ve got a bunch of other types I call “geometries”, say A{T}, B{T}, etc…, to represent various finite element spaces in various dimensions on various domains.

Right now I’ve got functions that look like `f(::Type{T},geometry::G) where {T,G}` but I’d like to get rid of the redundant `::Type{T}` parameter of `f`.

Should I just do `get_T(A{T}) where {T} = T` and then `f(geometry::G) = (T = get_T(G); ...)`? Will that get compiled down to equally as efficient code as if I specify `T` explicitly as a parameter of `f`?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [September 21, 2025, 8:57am UTC](https://discourse.julialang.org/t/good-way-to-parametrize-over-type-parameter/132533/2 "2025-09-21T08:57:06Z")

</div>

> [@loisel](#):
>
> Should I just do `get_T(A{T}) where {T} = T` and then `f(geometry::G) = (T = get_T(G); ...)`?

Yeah, that’s perfect. Except that your `get_T` definition is missing a `::`, it should look like:

```julia
get_T(::A{T}) where {T} = T

```

Then you call it like `get_T(geometry)`, and you do not need `G` as a method static parameter of the `f` method.

If you really need to define `get_T` on the types, instead of on values, you can do it like so:

```julia
get_T(::Type{<:A{T}}) where {T} = T

```

If the geometries all have a field of type `T`, you can even do `fieldtype(typeof(geometry), :field_name)` instead of `get_T(geometry)`. Then you do not need `get_T`.

In general, always avoid unnecessary method static parameters (`where` at the end of the definition of a method). Except as noted here, in the Performance tips:

- [Be aware of when Julia avoids specializing](https://docs.julialang.org/en/v1.12.0-rc2/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing)

---

<div class="post-metadata">

**Author:** ![loisel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/loisel/32/50626_2.png) [@loisel](https://discourse.julialang.org/u/loisel)\
**Post date:** [September 21, 2025, 9:23am UTC](https://discourse.julialang.org/t/good-way-to-parametrize-over-type-parameter/132533/3 "2025-09-21T09:23:19Z")

</div>

OK thanks for the information!

---

<div class="post-metadata">

**Author:** ![loisel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/loisel/32/50626_2.png) [@loisel](https://discourse.julialang.org/u/loisel)\
**Post date:** [September 21, 2025, 12:08pm UTC](https://discourse.julialang.org/t/good-way-to-parametrize-over-type-parameter/132533/4 "2025-09-21T12:08:51Z")

</div>

Actually, what I ended up doing is something like `f(geometry,::Type{T}=get_T(geometry);...) where {T}` so that I can use the type `T` in subsequent keyword arguments, is this kosher for performance?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [September 21, 2025, 12:27pm UTC](https://discourse.julialang.org/t/good-way-to-parametrize-over-type-parameter/132533/5 "2025-09-21T12:27:28Z")

</div>

It is kosher for performance, but it does not seem like a nice style IMO.
