JU

Julia Multiple Dispatch Architecture

Structuring Julia packages around multiple dispatch, abstract type hierarchies, and interface conventions

Details

Language / Topic
juliaJulia
Category
Architecture

Rules

balanced
- Define abstract type hierarchies with `abstract type AbstractFoo end` and implement behavior via method specializations — avoid single-dispatch OOP patterns.
- Separate interface (abstract types + method stubs) from implementation (`struct` definitions) into distinct source files within `src/`.
- Use `@interface` conventions or docstring contracts to document which methods must be implemented for a new subtype to be valid.
- Organize packages as `src/types.jl`, `src/interface.jl`, `src/impl.jl` — load them in order from the main module file with `include`.
- Use `Base.show(io::IO, x::MyType)` to provide human-readable representations for all public types.
- Implement `Base.iterate` and `Base.length` on custom collection types to make them work with Julia's generic iteration protocol.
- Avoid deep inheritance chains — prefer composition (a struct holding another struct) and multiple dispatch over deep type hierarchies.