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

Hi all - I wanted to share a small experimental package that I have started working on called ExtensibleUnions.jl.

As discussed in these comments, “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 Unions 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:

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 Iterables with:

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:

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:

addtounion!(Iterable, AbstractArray, AbstractSet, AbstractDict)

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

addtounion!(Iterable, MyStruct)

See the README for a full working example.

Issues and pull requests are welcome!

8 Likes

x-ref: https://github.com/rofinn/Interfaces.jl

1 Like

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.

1 Like