# ExtensibleUnions.jl - experiments in abstract multiple inheritance via extensible type unions

**URL:** <https://discourse.julialang.org/t/extensibleunions-jl-experiments-in-abstract-multiple-inheritance-via-extensible-type-unions/27920>\
**Category:** Package Announcements\
**Tags:** inheritance\
**Created:** [August 24, 2019, 1:51am UTC](https://discourse.julialang.org/t/extensibleunions-jl-experiments-in-abstract-multiple-inheritance-via-extensible-type-unions/27920 "2019-08-24T01:51:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [August 24, 2019, 1:51am UTC](https://discourse.julialang.org/t/extensibleunions-jl-experiments-in-abstract-multiple-inheritance-via-extensible-type-unions/27920/1 "2019-08-24T01:51:39Z")

</div>

Hi all - I wanted to share a small experimental package that I have started working on called [ExtensibleUnions.jl](https://github.com/bcbi/ExtensibleUnions.jl).

As discussed in [these comments](https://github.com/JuliaLang/julia/issues/5#issuecomment-33010882), “extensible type unions are largely equivalent to multiple inheritance”. Of course, in base Julia, you cannot currently extend a `Union`. Therefore, this package provides a way of simulating extensible type unions.

Using `Union`s is a natural way of providing abstract multiple inheritance. For example, if I want to dispatch on things that are iterable, I could try to define `Iterable` as such:

```julia
const Iterable = Union{AbstractArray, AbstractSet, AbstractDict}

```

Here, I have made a `Union` that contains some of the things that I know are iterable.

Then I can dispatch on `Iterable`s with:

```julia
function foo(x::Iterable)
    ...
end

```

However, after I create the union, I can’t add new members to it. So if I later load a package that defines some new types:

```julia
abstract MyAbstract end
struct MyStruct <: MyAbstract end

```

There’s no way for me to add `MyStruct` to the `Union` referred to by `Iterable`.

ExtensibleUnions.jl adds the ability to dispatch on unions that you are able to extend (i.e. add members to) after creation. For example, you can add some members to an extensible union:

```julia
addtounion!(Iterable, AbstractArray, AbstractSet, AbstractDict)

```

And at a later time, you can add some more members:

```julia
addtounion!(Iterable, MyStruct)

```

See the README for a full working example.

Issues and pull requests are welcome!

---

<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:** [August 24, 2019, 11:36am UTC](https://discourse.julialang.org/t/extensibleunions-jl-experiments-in-abstract-multiple-inheritance-via-extensible-type-unions/27920/2 "2019-08-24T11:36:30Z")

</div>

x-ref: [https://github.com/rofinn/Interfaces.jl](https://github.com/rofinn/Interfaces.jl)

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [December 21, 2020, 11:44am UTC](https://discourse.julialang.org/t/extensibleunions-jl-experiments-in-abstract-multiple-inheritance-via-extensible-type-unions/27920/3 "2020-12-21T11:44:46Z")

</div>

I just found out about this upon pondering about possible forms of interfaces in a future Julia version. After a year and a half, what’s your impression about this approach? What are the key shortcomings here? Is there a better way towards interfaces floating around the collective mind?

(I apologize if this is considered inappropriate necroposting)

EDIT: Let me say that, from a usability point of view, I find this concept _very_ appealing. It is powerful, yet simple to understand. My worry is how it can be made to work well at a compiler/inference/invalidations level.
