Context:

multi-method in a type family that features type promotion (with strictly linear precision order. E.g. a sequence of Numbers, by precision.)

Challenge:

Given two objects - to convert the lesser to the type of the higher. 

Skill:

Participants:

Quantity:

The capability to be sequenced by precision. Specifically: The capabilities for comparison with another quantity and for upward conversion.

Used by:

Metamorphic Bridge (Generic Number).

Signature:

Quantity responds to messages to compare its precision with another Quantity, implemented by Concrete Quantity (e.g. forwarded from template method in quantity).

Comments:

  • The alternative Solution is double dispatch. Algebraic hierarchy is less efficient, but easier to write and maintain.
  • While conversion is possible but up and down the precision ladder, both up conversion is more natural, as long as it does not create a cyclic namespace dependency.

Source:

Coplien.

Scope:

General.