# Subtyping parametric composite types

**URL:** <https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944>\
**Category:** General Usage\
**Tags:** parametric-types\
**Created:** [March 24, 2018, 4:45pm UTC](https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944 "2018-03-24T16:45:28Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdacosta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdacosta/32/3716_2.png) [@cdacosta](https://discourse.julialang.org/u/cdacosta)\
**Post date:** [March 24, 2018, 4:45pm UTC](https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944/1 "2018-03-24T16:45:28Z")

</div>

I am struggling to understand how I can reuse methods which have been defined as composite parametric types. I am working with the [`LinearMaps.jl` package](https://github.com/Jutho/LinearMaps.jl/) which defines a `FunctionMap` as

```julia
struct FunctionMap{T,F1,F2} <: LinearMap{T}
    f::F1
    fc::F2
    M::Int
    N::Int
    _ismutating::Bool
    _issymmetric::Bool
    _ishermitian::Bool
    _isposdef::Bool
end

```

What I would like to do is create my own map which shares the same fields and methods as a `FunctionMap`, but to which I can add other methods (e.g. an inverse). For example, I would like to do something like this

```julia
import Base: \
struct MyMap{T,F1,F2} <: FunctionMap{T,F1,F2}
    f::F1
    fc::F2
    M::Int
    N::Int
    _ismutating::Bool
    _issymmetric::Bool
    _ishermitian::Bool
    _isposdef::Bool
end
\(A::MyMap, b::AbstractVector) = myinverse(A, b)

```

where `MyMap` inherits all methods from `FunctionMap` except `\`, which I define myself. The above code does not work, and I get a complaint of `LoadError: LoadError: invalid subtyping in definition of MyMap`.

Any suggestions?

---

<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:** [March 24, 2018, 7:35pm UTC](https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944/2 "2018-03-24T19:35:56Z")

</div>

You can only subtype from abstract types. Besides reading the manual, maybe these posts can give some insights [Search results for 'composition inheritance' - JuliaLang](https://discourse.julialang.org/search?q=composition%20inheritance).

---

<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:** [March 24, 2018, 7:47pm UTC](https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944/3 "2018-03-24T19:47:56Z")

</div>

For some more relevant information on why this is intentionally disallowed, see the [Types](https://docs.julialang.org/en/stable/manual/types/) section in the manual:

> One particularly distinctive feature of Julia’s type system is that concrete types may not subtype each other: all concrete types are final and may only have abstract types as their supertypes. While this might at first seem unduly restrictive, it has many beneficial consequences with surprisingly few drawbacks. It turns out that being able to inherit behavior is much more important than being able to inherit structure, and inheriting both causes significant difficulties in traditional object-oriented languages.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 25, 2018, 6:36am UTC](https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944/4 "2018-03-25T06:36:44Z")

</div>

> [@cdacosta](#):
>
> What I would like to do is create my own map which shares the same fields and methods as a FunctionMap, but to which I can add other methods (e.g. an inverse)

Yes it’s a good idea and the problem is that the library should be dispatching on an abstract type. I didn’t want to keep asking for it, but please revive the issue. Or just PR the solution.

> <https://github.com/JuliaLinearAlgebra/LinearMaps.jl/issues/13>
>
> Can FunctionMap and CompositeMap be changed to be concrete implementations of an… abstract interfaces with their functions all written for the abstract type? I would like to create extended versions of FunctionMap and CompositeMap specific to Diffeq, and without this change I would have to duplicate the entire codes for these.

---

<div class="post-metadata">

**Author:** ![cdacosta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdacosta/32/3716_2.png) [@cdacosta](https://discourse.julialang.org/u/cdacosta)\
**Post date:** [March 25, 2018, 8:59pm UTC](https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944/5 "2018-03-25T20:59:07Z")

</div>

Thanks both of you. For now I have just copied the methods, but I will take this up with the developer or `LinearMaps.jl`

---

<div class="post-metadata">

**Author:** ![ksamtsak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksamtsak/32/10585_2.png) [@ksamtsak](https://discourse.julialang.org/u/ksamtsak)\
**Post date:** [June 24, 2018, 4:50pm UTC](https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944/6 "2018-06-24T16:50:20Z")

</div>

I missed that paragraph in the manual and was puzzling over the “invalid subtyping” error, because I tried to subtype from a concrete type. Can the compiler message be made more specific?

---

<div class="post-metadata">

**Author:** ![astrobc1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/astrobc1/32/45632_2.png) [@astrobc1](https://discourse.julialang.org/u/astrobc1)\
**Post date:** [September 17, 2020, 8:34pm UTC](https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944/7 "2020-09-17T20:34:31Z")

</div>

It’s been over 2 years since this thread, and “invalid subtyping” is still about the most useless Julia error I’ve seen. The stracktrace has come a LONG way since pre 1.0 releases in terms of info and speed, but this error seriously needs addressed, considering it’s at the root of OOP practices.

---

<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:** [September 18, 2020, 6:11am UTC](https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944/8 "2020-09-18T06:11:57Z")

</div>

> [@astrobc1](#):
>
> “invalid subtyping” is still about the most useless Julia error I’ve seen

I am not sure why you think this. The error message is telling you that you are trying to define a subtype relationship that is not allowed in Julia (so it is invalid). I find it concise and informative.

What would you prefer instead?

(also, please don’t necro very old topics in the future)

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [September 18, 2020, 8:40am UTC](https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944/9 "2020-09-18T08:40:47Z")

</div>

Such as: “Subtyping from a concrete type is strictly forbidden. Please see (doc link).”

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 18, 2020, 9:20am UTC](https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944/10 "2020-09-18T09:20:51Z")

</div>

`Invalid subtyping` seems like it could mean any number of things, such as “subtyping concrete types is perfectly ok, but you did it the wrong way, somehow.” Or that there’s something special or weird about that particular parent type that prevented subtyping, and that could be fixed. I would think that I had to tweak things a bit and try again, look through examples somewhere, and scratch my head a lot.

“Subtyping from concrete types is not possible” makes it really clear what’s wrong.

---

<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:** [September 18, 2020, 9:23am UTC](https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944/11 "2020-09-18T09:23:46Z")

</div>

@liuyxpp, @DNF: excellent points. Please consider making a PR.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 18, 2020, 11:21am UTC](https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944/12 "2020-09-18T11:21:04Z")

</div>

I don’t really know much (barely any) C, but I suspect that this is where it happens

[https://github.com/JuliaLang/julia/blob/e84fec4d67945e50a31b8a15cc23e42c4e124a41/src/builtins.c#L1265](https://github.com/JuliaLang/julia/blob/e84fec4d67945e50a31b8a15cc23e42c4e124a41/src/builtins.c#L1265)

This error message seems to trigger on several different criteria, such as if it isn’t a type at all, if it’s a vararg type, a tuple type, and some other things.

This function needs some re-writing in that case. C is a foreign language, but it still looks like it shouldn’t be too hard.

Something like:  
“Invalid subtyping in definition of %s” + one of

- “Parent is not a type”
- “Subtyping concrete types is not possible.”
- “Subtyping NULL is not possible?”
- “Parent and child types cannot have the same name”
- “Subtyping a vararg type is not possible”
- “Subtyping a tuple type is not possible”
- “Subtyping a named tuple type is not possible”
- “Subtyping a type type(?!) is not possible” Is this something like `Type{T}`?
- “Subtyping a built-in type is not possible” (okay?)

So it needs an extra string variable that is assigned one of these strings somewhere inside the function. So re-writing is needed, indeed.

---

<div class="post-metadata">

**Author:** ![astrobc1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/astrobc1/32/45632_2.png) [@astrobc1](https://discourse.julialang.org/u/astrobc1)\
**Post date:** [September 18, 2020, 7:35pm UTC](https://discourse.julialang.org/t/subtyping-parametric-composite-types/9944/13 "2020-09-18T19:35:00Z")

</div>

I think that because it is. I’m not saying there’s an easy fix, I but I don’t see how you can just say

> The error message is telling you that you are trying to define a subtype relationship that is not allowed in Julia (so it is invalid).

You may as well have just read off the error statement. By that logic, a seg fault is just as informative. There are plenty of ways to “invalidly” subtype an abstract type. It would be helpful to see a more descriptive stack trace in this case, where possible.

I could start a new topic, sure, but why? It’s the same error, and it hasn’t been addressed AFAIC. Creating a new thread would create two unique pages for others to find eventual answers / solutions / etc.
