Design extensible Julia code with multiple dispatch

8 steps · about 2 h 2 min · Intermediate

  1. See the expression problem that dispatch solves (article, Wikipedia, 12 min)

    Frames the goal: add new types and new operations without editing old code.

  2. Compare multiple dispatch with single dispatch (article, Wikipedia, 12 min)

    Places Julia's model next to method overloading and visitor patterns.

  3. Build an abstract type hierarchy (tutorial, Wikibooks, 20 min)

    Shows abstract and concrete types and where methods attach.

  4. Write outer and inner constructors (reference, Julia documentation, 20 min)

    Keeps new types valid from the moment they are created.

  5. Implement iteration and indexing interfaces (reference, Julia documentation, 20 min)

    Adding a few methods makes your type work with all of Base.

  6. Add conversion and promotion rules (reference, Julia documentation, 15 min)

    Lets mixed-type arithmetic dispatch to the right method.

  7. Check which method runs with @which and methodswith (reference, Julia documentation, 8 min)

    Confirms that dispatch picked the method you intended.

  8. Avoid type piracy and over-tight signatures (reference, Julia documentation, 15 min)

    Style rules that keep dispatch-based code extensible.