Multiple dispatch and type hierarchies

Dear Julia community,
i have been thinking about why multiple dispatch and julias type system works so well (often times) and why it doesnt sometimes. Correct me if i am wrong, but as i see it this system only works well because Julias single-inheritence nominal type hierarchy. Every type/abstract type has one parent at most and also every value has one type. Every function call must dispatch to exactly one method determined by specificity of the input types (position in type hierarchy). If its ambigous it will error.

Contrast that with something like Rust/Haskell where we have traits/type classes and one type can have multiple traits. This wouldnt work well in conjunction with multiple dispatch, because ambiguities would explode exponentially (or combinatorially?). But in general behaviour is not monophyletically inhereted. We might have a Type that is an AbstractArray and also Iterable, but others that are only Iterable and not AbstractArray. We might have 5 traits AbstractArray, AbstractDict, Iterable, Numeric, Serializable and a type “FancyTable” that is all of those.
Julia has traits only in the form of functions that return some value and that have to be awkwardly squeezed into the type hierarchy. Generally Julias system seems to work well in the case of AbstractArray, but i see many frustrated in LinearAlgebra.jl, though i didnt use that so maybe someone could explain why.

I was thinking about how that could be resolved and the best thing i came up with is this:
Seperate abstract types from concrete types. Abstract types live in single-inheretence trees. Only concrete types created with structs live outside of that, but one concrete type can be coupled to one abstract type. Then we could also define traits, which can specify further behaviour that a concrete type must fulfill, just like abstract tpes, only that one concrete type can have many traits. Then multiple dispatch is oriented towards abstract types and traits might serve to make more specific methods.

What are your thoughts on this and the topic in general? Do i understand something wrong/inaccurately?

thanks

Relevant discussion on the issue tracker:

Regarding the question of reconciling multiple dispatch with multiple inheritance, there’s this approach, used in Gerbil Scheme:

3 Likes

The discourse ANN for MultipleInterfaces.jl also discussed the opposition of multiple dispatch and multiple supertypes (or whatever is similar), and it gets more nuanced than just doing one or the other. Note that linearization is not involved there by design, many languages avoid or discourage it because it takes a design detail out of our hands.

3 Likes