# Can a struct have a variable supertype?

**URL:** <https://discourse.julialang.org/t/can-a-struct-have-a-variable-supertype/17942>\
**Category:** General Usage\
**Created:** [November 24, 2018, 11:22am UTC](https://discourse.julialang.org/t/can-a-struct-have-a-variable-supertype/17942 "2018-11-24T11:22:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zot/32/5817_2.png) [@zot](https://discourse.julialang.org/u/zot)\
**Post date:** [November 24, 2018, 11:22am UTC](https://discourse.julialang.org/t/can-a-struct-have-a-variable-supertype/17942/1 "2018-11-24T11:22:51Z")

</div>

I’m working on beefing up JavaCall.jl and I have a proxy struct (currently called JProxy) that represents a Java object, along with an abstract type hierarchy with one type for each Java type. It would be convenient if the JProxy struct had a variable supertype. Julia lets me do that but doesn’t behave in the way I expect. Here is an example:

```julia
abstract type A end
abstract type B <: A end

struct S{T} <: T where {T <: A} end

@show(S{A} <: A)
@show(S{B} <: A)
@show(S{B} <: B)

```

The @show calls output this:

```julia
S{A} <: A = true
S{B} <: A = true
S{B} <: B = false

```

I expected the last expression to return true but it returned false.

The struct declaration’s AST seems to tell a different story from what I expected:

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

```

outputs

```julia
:(struct S{T} <: (T where T <: A)
      #= REPL[22]:1 =#
  end)

```

Which makes it look like there is actually a separate T variable for the supertype. Is this true? It seems like if it is then the variable is unbindable and is actually equivalent to A.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 24, 2018, 12:57pm UTC](https://discourse.julialang.org/t/can-a-struct-have-a-variable-supertype/17942/2 "2018-11-24T12:57:47Z")

</div>

Yes, your analysis is correct. You can also check this be re-writing it

```julia
struct S{T} <: U where {U <: A} end

```

which behaves identically to your example. You’d want something like:

```julia
julia> struct (MT{T} <: T) where {T <: A} end
ERROR: syntax: invalid type signature

```

but that does not work, not this

```julia
julia> struct S{T<:A} <: T end
ERROR: invalid subtyping in definition of S

```

Anyway, bottom line is that you can’t do this. But you can just dispatch on the type parameter, so I don’t think `S` needs to have the same supertype as its type parameter.
