liveBook · Manning

Arrows/multimethods

As explained in Data-Oriented Programming: Reduce software complexity

arrows/multimethods is a JavaScript library that provides an implementation of multimethods.

When one operation must vary by data type, arrows/multimethods supplies JavaScript multimethods without forcing every implementation into one module. Create a dispatch function, initialize the multimethod with multi, then attach implementations with method. section 13.2

A dispatch function handles three jobs:

Greeting by animal type

Initialize greet from greetDispatch, then register dog, cat, and cow handlers with method("dog", greetDog)(greet) and equivalent calls. Invoke greet(myDog), greet(myCat), or greet(myCow) like an ordinary function; add a default handler for unmatched types such as horse.

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Explains initialization, validation, dispatch, method registration, default handling, and invocation through a single-animal example.
Examples
Demonstrates dog, cat, cow, and horse greeting handlers selected from an animal type.
What it assumes
Assumes JavaScript functions and objects while teaching multimethod dispatch from initialization through invocation.
What you can do after
Build multimethods with multi and extend them using method-based implementations and defaults.
How it connects
Connects multimethods with polymorphism, single dispatch, dispatch functions, argument validation, and default implementations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming: Reduce software complexity. The section links point to the parts of the book it draws on.