# Invalid subtyping: can only subtype data types (related to symbolics too)

**URL:** <https://discourse.julialang.org/t/invalid-subtyping-can-only-subtype-data-types-related-to-symbolics-too/88683>\
**Category:** General Usage\
**Tags:** type, parametric-types, symbolics\
**Created:** [October 13, 2022, 6:20pm UTC](https://discourse.julialang.org/t/invalid-subtyping-can-only-subtype-data-types-related-to-symbolics-too/88683 "2022-10-13T18:20:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [October 13, 2022, 6:20pm UTC](https://discourse.julialang.org/t/invalid-subtyping-can-only-subtype-data-types-related-to-symbolics-too/88683/1 "2022-10-13T18:20:08Z")

</div>

Edit: it was just a syntax error, that is being reported a bit misleadingly and led me astray

I was playing with defining convoluted types and stumbled upon this behavior:

```julia
julia> struct S{T} <: AbstractVector{T} where {T<:Integer} end
ERROR: invalid subtyping in definition of S: can only subtype data types.

```

I think it is happening because `typeof(AbstractVector) == UnionAll` instead of `DataType`. Could someone hand-hold me through an explanation of why this is forbidden? Not just the dry rule “can not subtype UnionAll”, but focusing more on the “why”, what becomes unwieldy and unpleasant for the dispatcher and compiler?

Edit: Another way to ask the question is **what is the difference between the following two attempts** that I initially thought are the same, but it seems only one of them works:

```julia
julia> struct S1{T <: Integer} <: AbstractVector{T} end
# works fine

julia> struct S2{T} <: AbstractVector{T} where {T <: Integer} end
ERROR: invalid subtyping in definition of S2: can only subtype data types.

```

As to why I was trying to do this, I describe it in the block below, but I suspect it is not important - I will figure out some other way to do it. It had to do with defining new Symbolic operators with Symbolics.jl. I am describing it here, because maybe the creators of Symbolics have faced similar issues.

> **Click to expand the initial reason I stumbled upon this**
>
> I wanted to define a bunch of symbolic objects that have mostly the same behaviors and interfaces and internal structure, but which print slightly differently. For example  
> `struct SymbolicTensorProductOfKets <: Symbolic{Ket} terms end`  
> and  
> `struct SymbolicTensorProductOfOperators <: Symbolic{Operator} terms end`  
> So I thought it would make sense to define  
> `struct SymbolicTensorProduct{T} <: Symbolic{T} where {T <: Union{Ket,Operator}} terms end`  
> but that does not work.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 13, 2022, 6:28pm UTC](https://discourse.julialang.org/t/invalid-subtyping-can-only-subtype-data-types-related-to-symbolics-too/88683/2 "2022-10-13T18:28:17Z")

</div>

Perhaps you want:

```julia
struct S{T <: Integer} <: AbstractVector{T}
  x::Int
end

```

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [October 13, 2022, 6:31pm UTC](https://discourse.julialang.org/t/invalid-subtyping-can-only-subtype-data-types-related-to-symbolics-too/88683/3 "2022-10-13T18:31:07Z")

</div>

I think you are right! It is not immediately clear to me why the two syntaxes are interpreted differently.

---

<div class="post-metadata">

**Author:** ![palday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palday/32/12640_2.png) [@palday](https://discourse.julialang.org/u/palday)\
**Post date:** [October 13, 2022, 6:36pm UTC](https://discourse.julialang.org/t/invalid-subtyping-can-only-subtype-data-types-related-to-symbolics-too/88683/4 "2022-10-13T18:36:14Z")

</div>

There is another critical difference `AbstractVector` vs the concrete type `Vector`

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [October 13, 2022, 7:41pm UTC](https://discourse.julialang.org/t/invalid-subtyping-can-only-subtype-data-types-related-to-symbolics-too/88683/5 "2022-10-13T19:41:42Z")

</div>

You are right, it needs to be `AbstractVector`. I edited the initial question. However, I do not think there is a clear answer yet. I still do not have much understanding of why the following two are different:

```julia
julia> struct S1{T <: Integer} <: AbstractVector{T} end
# works fine

julia> struct S2{T} <: AbstractVector{T} where {T <: Integer} end
ERROR: invalid subtyping in definition of S2: can only subtype data types.

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 13, 2022, 8:04pm UTC](https://discourse.julialang.org/t/invalid-subtyping-can-only-subtype-data-types-related-to-symbolics-too/88683/6 "2022-10-13T20:04:54Z")

</div>

There’s no where clause in struct definition

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [October 13, 2022, 8:17pm UTC](https://discourse.julialang.org/t/invalid-subtyping-can-only-subtype-data-types-related-to-symbolics-too/88683/7 "2022-10-13T20:17:55Z")

</div>

Oh, so it is just a syntax error!? That is fair, but the error message should have been one about a syntax error (using `where` with struct definitions), not an error about subtyping, right?

```julia
julia> struct S2{T} <: AbstractVector{T} where {T <: Integer} end
ERROR: invalid subtyping in definition of S2: can only subtype data types.

julia> struct S2{T} <: AbstractVector{T} end
# works fine

julia> struct S2{T} where {T<:Integer} end
ERROR: syntax: invalid type signature around ...

```
