# Get the type parameters of a parametric type

**URL:** <https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308>\
**Category:** New to Julia\
**Tags:** type, parametric-types\
**Created:** [April 21, 2024, 6:25pm UTC](https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308 "2024-04-21T18:25:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [April 21, 2024, 6:25pm UTC](https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308/1 "2024-04-21T18:25:44Z")

</div>

I have an instance of a parametric type. Is it possible to somehow retrieve the type parameters for this instance?

E.g. for ```julia  
struct MyStruct{T}  
x::T

```
function MyStruct(x)
    return new{typeof(x)}(x)
end

```

end

ms = MyStruct(1.0)  
typeof(ms)

```julia
I'd like to know the type parameter `Float64`.
```

---

<div class="post-metadata">

**Author:** ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)\
**Post date:** [April 21, 2024, 7:10pm UTC](https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308/2 "2024-04-21T19:10:48Z")

</div>

I believe you have to define the `eltype` for MyStruct. In this case:

```julia
Base.eltype(::Type{MyStruct{T}}) where T = T

```

This will allow you to get the parameter type:

```julia
julia> ms1 = MyStruct(1.0)
MyStruct{Float64}(1.0)

julia> eltype(ms1)
Float64

julia> ms2 = MyStruct(1)
MyStruct{Int64}(1)

julia> eltype(ms2)
Int64

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 21, 2024, 7:52pm UTC](https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308/3 "2024-04-21T19:52:23Z")

</div>

`eltype` should only be defined for containers/collections:

```julia
help?> eltype
search: eltype valtype keytype fieldtype oftype fieldtypes supertype subtypes code_typed

  eltype(type)

  Determine the type of the elements generated by iterating a collection of the given
  type. [...]

```

It’s not a generic “get a type parameter” function.

That kind of functions doesn’t (and can’t, in general) exist, because what exactly a type parameter means to the type depends on that type. It doesn’t generalize.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [April 21, 2024, 8:44pm UTC](https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308/4 "2024-04-21T20:44:59Z")

</div>

Perhaps:

```julia
get_parameter_type(::MyStruct{T}) where T = T

```

---

<div class="post-metadata">

**Author:** ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)\
**Post date:** [April 21, 2024, 9:19pm UTC](https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308/5 "2024-04-21T21:19:12Z")

</div>

Ah good point. I guess I’m really use to working with collections. @sylvaticus’s suggestion makes more sense to avoid type piracy then.

I guess the bigger question to @Torkel is why do you need the parametric type?

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [April 22, 2024, 3:39am UTC](https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308/6 "2024-04-22T03:39:55Z")

</div>

If `MyStruct` is a type that you own, then you could just get the type of the field `x`:

```julia
ms = MyStruct(1.0)
typeof(ms.x)

```

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [April 22, 2024, 8:56am UTC](https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308/7 "2024-04-22T08:56:27Z")

</div>

> [@CameronBieganek](#):
>
> ```julia
> ms = MyStruct(1.0)
> typeof(ms.x)
> 
> ```

Consider instead the following:

```julia
julia> struct MyStruct{T}
       x::T
       end

julia> ms=MyStruct{Union{Float64, Missing}}(1.0);

julia> typeof(ms.x)
Float64

julia> typeof(ms).parameters[1]
Union{Missing, Float64}

julia> extract_the_T(::MyStruct{T}) where T = T
extract_the_T (generic function with 1 method)

julia> extract_the_T(ms)
Union{Missing, Float64}

```

PS. On current master, `extract_the_T2(ms) = typeof(ms).parameters[1]` does constant propagate. This might not const-prop on earlier julia versions – test it in order to avoid inference failures.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 22, 2024, 10:51am UTC](https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308/8 "2024-04-22T10:51:31Z")

</div>

> [@foobar\_lv2](#):
>
> ```julia
> julia> typeof(ms).parameters[1]
> Union{Missing, Float64}
> 
> ```

I’m pretty sure that `.parameters` is an internal field, so not realiable. Extracting the type parameter in a dedicated function through the parameters there is IMO better, because it’s more stable & doesn’t rely on internals.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [April 22, 2024, 12:08pm UTC](https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308/9 "2024-04-22T12:08:04Z")

</div>

> [@foobar\_lv2](#):
>
> ```julia
> julia> ms=MyStruct{Union{Float64, Missing}}(1.0);
> 
> julia> typeof(ms.x)
> Float64
> 
> ```

Oops, good point.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 22, 2024, 1:43pm UTC](https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308/10 "2024-04-22T13:43:11Z")

</div>

> [@RobertGregg](#):
>
> I guess the bigger question to @Gaussia is why do you need the parametric type?

Indeed this is the most relevant question. Specifically, if the user needs to query a value or a type for something, which may be a type (eg `eltype`), **this should be exposed via a function** , and the fact that this may be encoded in a parametric type is an implementation detail.

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [April 22, 2024, 3:13pm UTC](https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308/11 "2024-04-22T15:13:48Z")

</div>

So, in this case `MyStruct` was just an example I added to make what I want to do more clear, figured there probably was an easy way for this.

I want to apply this to a structure I do not actually own. Basically, there’s this DiffEq composite structure which typeof is reaaaaaaly long. To try and figure out what it actually is, I wanted to look at the parametric types on by one.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 23, 2024, 8:01am UTC](https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308/12 "2024-04-23T08:01:00Z")

</div>

> [@Torkel](#):
>
> To try and figure out what it actually is, I wanted to look at the parametric types on by one.

I see — this is for code exploration only. In that case, I would recommend `dump`, and of course reading the source code, but `typeof(T).parameters` is fine too.

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [April 23, 2024, 3:43pm UTC](https://discourse.julialang.org/t/get-the-type-parameters-of-a-parametric-type/113308/13 "2024-04-23T15:43:35Z")

</div>

That worked great, thanks a lot!
