# Generic type parametrized by supertype

**URL:** https://discourse.julialang.org/t/generic-type-parametrized-by-supertype/56643
**Category:** General Usage
**Tags:** question
**Created:** [March 7, 2021, 1:43am UTC](https://discourse.julialang.org/t/generic-type-parametrized-by-supertype/56643 "2021-03-07T01:43:08Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [March 7, 2021, 1:43am UTC](https://discourse.julialang.org/t/generic-type-parametrized-by-supertype/56643/1 "2021-03-07T01:43:08Z")

</div>

Is it possible to create a generic type whose parameter is its supertype?

I thought this would be it but it returns false.

```julia
julia> struct Foo{T} <: T where T end

julia> Foo{Int64} <: Int64
false

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [March 7, 2021, 1:55am UTC](https://discourse.julialang.org/t/generic-type-parametrized-by-supertype/56643/2 "2021-03-07T01:55:19Z")

</div>

The problem there is that `Int64` is concrete and cannot be subtyped. With `Integer` it should work.

> [@Why is it impossible to subtype a struct?](https://discourse.julialang.org/t/why-is-it-impossible-to-subtype-a-struct/19876/3):
>
> Multiple inheritance is a totally different story and could be allowed. I’ve written about why we don’t allow subtyping of concrete types before if someone wants to dig up one of my earlier posts about it. Edit: here’s a link to an old google groups conversation about it [https://groups.google.com/d/msg/julia-dev/eA4VkFAD-yQ/LNUP\_OT0zy0J](https://groups.google.com/d/msg/julia-dev/eA4VkFAD-yQ/LNUP_OT0zy0J) In particular, this paragraph from the end of my first post on the thread sums up my position fairly well still: While there are a number of practical reaso…

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [March 7, 2021, 2:51am UTC](https://discourse.julialang.org/t/generic-type-parametrized-by-supertype/56643/3 "2021-03-07T02:51:32Z")

</div>

> The problem there is that `Int64` is concrete and cannot be subtyped. With `Integer` it should work.

```julia
julia> Foo{Integer} <: Integer
false

```

That has the same result, unless I missed something.

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [March 7, 2021, 4:18am UTC](https://discourse.julialang.org/t/generic-type-parametrized-by-supertype/56643/4 "2021-03-07T04:18:13Z")

</div>

That struct definition isn’t working the way you want it. Instead, the following three things are equivalent:

```julia
struct Foo{T} <: T where T end
struct (Foo{T} <: (T where T)) end
struct Foo{T} end

```

which you can confirm by running all three and not receiving any errors about struct redefinition. So you’ve actually just defined `Foo{T}` with no supertype, so its supertype is `Any`:

```julia
julia> Foo |> supertype
Any

julia> Foo{Int} |> supertype
Any

```

I don’t think there’s any way to do what you’re after since in Julia you can’t parameterize the supertype like that (only its type arguments, like in `struct Foo{T} <: AbstractVector{T} end`. If you give a little more context what you’re trying to do, someone might be able to suggest a better approach though.

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [March 7, 2021, 5:16am UTC](https://discourse.julialang.org/t/generic-type-parametrized-by-supertype/56643/5 "2021-03-07T05:16:24Z")

</div>

That’s helpful, thanks.

> If you give a little more context what you’re trying to do, someone might be able to suggest a better approach though.

For each of several external abstract types `T`, I want to make a new abstract subtype of `T` whose dispatch I control.

For example, a `const LazyInteger = Lazy{Integer}` that’s a subtype of `Integer` but has custom lazy method implementations.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [March 7, 2021, 6:17am UTC](https://discourse.julialang.org/t/generic-type-parametrized-by-supertype/56643/6 "2021-03-07T06:17:21Z")

</div>

I would use

```julia
abstract type LazyInteger <: Integer end
struct LazyInt64 <: LazyInteger 
   value::Int64
end

```

etc.
