# Parametric type with value of custom type as type parameter

**URL:** <https://discourse.julialang.org/t/parametric-type-with-value-of-custom-type-as-type-parameter/89202>\
**Category:** General Usage\
**Tags:** parametric-types\
**Created:** [October 24, 2022, 5:59pm UTC](https://discourse.julialang.org/t/parametric-type-with-value-of-custom-type-as-type-parameter/89202 "2022-10-24T17:59:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GoodDayToYouAll](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@GoodDayToYouAll](https://discourse.julialang.org/u/GoodDayToYouAll)\
**Post date:** [October 24, 2022, 5:59pm UTC](https://discourse.julialang.org/t/parametric-type-with-value-of-custom-type-as-type-parameter/89202/1 "2022-10-24T17:59:36Z")

</div>

Hi everyone,

as the title suggests, I want to create a parametric type with the value of a custom type as type parameter. So for example

```julia
struct MyType
x::Int
end

struct MyParametricType{T}
x
MyParametricType(a) = new{a}(a)
end

```

This constructor works for “simple” inputs like `MyParametricType(5)` returns `MyParametricType{5}(5)`. However this does not work for any instance of `MyType` (even though it is a bitstype) or a `String`. I also noticed that it does work for a singleton type defined like `struct MySingletonType end`.

I guess it has something to do with whether `Val` is defined for my input to `MyParametricType`.

So is it possible to attain this? And when does it work and when doesn’t it?

I am grateful for any input.

---

<div class="post-metadata">

**Author:** ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)\
**Post date:** [October 24, 2022, 6:30pm UTC](https://discourse.julialang.org/t/parametric-type-with-value-of-custom-type-as-type-parameter/89202/2 "2022-10-24T18:30:46Z")

</div>

From the [manual](https://docs.julialang.org/en/v1/manual/types/#Parametric-Types):

` Both abstract and concrete types can be parameterized by other types. They can also be parameterized by symbols, by values of any type for which [`isbits`](https://docs.julialang.org/en/v1/base/base/#Base.isbits) returns true (essentially, things like numbers and bools that are stored like C types or `struct`s with no pointers to other objects), and also by tuples thereof.`

It works:

```julia
julia> isbits(MyType(3))
true

julia> MyParametricType(MyType(3))
MyParametricType{MyType(3)}(MyType(3))

```

---

<div class="post-metadata">

**Author:** ![GoodDayToYouAll](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@GoodDayToYouAll](https://discourse.julialang.org/u/GoodDayToYouAll)\
**Post date:** [October 24, 2022, 8:29pm UTC](https://discourse.julialang.org/t/parametric-type-with-value-of-custom-type-as-type-parameter/89202/3 "2022-10-24T20:29:40Z")

</div>

First thank you for your answer and sorry for asking a question which is answered by the manual and for posting a wrong MWE.

It seems that the example I actually look at was (I don’t know why I changed this for my MWE)

```julia
struct MySymbolType
       x::Symbol
end

```

And then it doesn’t work. I assume because `Symbol` is not a bits type. However, I am still confused as to why

```julia
julia> MyParametricType(:a)
MyParametricType{:a}(:a)

julia> isbits(:a)
false

```

Works. I guess there is some special treatment for `Symbol`s, but then I had hoped that `MyParametricType(MySymbolType(:a))` also works.  
But apparently that is not the case.

Thanks again for the answer

---

<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:** [October 24, 2022, 8:35pm UTC](https://discourse.julialang.org/t/parametric-type-with-value-of-custom-type-as-type-parameter/89202/4 "2022-10-24T20:35:15Z")

</div>

`Symbols` are also special cased here, yes, as mentioned in the quoted section above:

> They can also be parameterized by _symbols_, by values of any type for which _`isbits`_ returns true (essentially, things like numbers and bools that are stored like C types or structs with no pointers to other objects), and also by _tuples thereof_.

(emphasis mine)

---

<div class="post-metadata">

**Author:** ![GoodDayToYouAll](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@GoodDayToYouAll](https://discourse.julialang.org/u/GoodDayToYouAll)\
**Post date:** [October 24, 2022, 8:42pm UTC](https://discourse.julialang.org/t/parametric-type-with-value-of-custom-type-as-type-parameter/89202/5 "2022-10-24T20:42:22Z")

</div>

Yeah I see, thank you
