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!