Types vs traits for dispatch

Julia’s type system together with multiple dispatch is already very powerful for generic code and should be able to handle the majority of cases, so it’s more like traits are an overkill for most tasks. For example, instead of defining a new trait for two types to dispatch on, you might as well just rely on multiple dispatch and define a method for each type.
That being said, traits are definitely more general and sometimes necessary when you need to extend the type hierarchy in an orthogonal direction (see the original discussion where THTT first comes up). They’re not just dispatching on groups of types (which the type system is already capable of via Union), they are assigning “types” to existing types in another dimension, think multiple subtyping!
In fact, in an all-powerful trait system, I would say types and traits are pretty much the same thing (see this reimagination of Julia in such a system).

4 Likes