External dispatch in object creation (RG, JOT) generalizes the object-oriented (internal) single dispatch mechanism (dynamic binding) to a (external) dynamic n‑ary dispatch mechanism (n > 0).
(Click here for a step-by-step introduction to this mechanism.)
External dispatch requires two language extensions:
- The introduction of external dispatch classes (extracted from shapes example):
deferred class TWO_SHAPES(s1,s2: SHAPE) -- external dispatch class feature distance: REAL_64 deferred end intersect: BOOLEAN do Result := distance <= 0 end end -- TWO_SHAPES class TWO_CIRCLES(c1,c2: CIRCLE) -- external dispatch class. (c1,c2) descendant of (s1,s2) inherit TWO_SHAPES feature distance: REAL_64 do Result := c1.center.distance(c2.center) - (c1.radius + c2.radius) end end -- TWO_CIRCLES (...)
two_shapes: TWO_SHAPES (...) -- create instruction: create (s1,s2).two_shapes -- external dispatch! s1 and s2 are non-Void references to SHAPE objects -- or, using a create expression: two_shapes := create {TWO_SHAPES}(s1, s2) -- external dispatch! (s1,s2) is the object dispatch tuple (...) print(two_shapes.distance.out + "%N")
This new language mechanism is statically safe, and provides an Object-Oriented modular solution to many problems:
- binary methods (see examples: shapes, point-colorpoint, prefix-calculator);
- covariant redefinition (all examples);
- decorator pattern (see examples: geometry, geometry-once, expression-problem);
- the expression problem (see example: expression-problem);
- visitor pattern (see examples: prefix-calculator, dummy visitor);
- creational patterns;
- some approaches of aspect-oriented programming (see example: prefix-calculator);
- statically ensured global downcast (see example: downcast).
Examples
- Shapes
- Geometry (extend shapes with convexity)
- Geometry with once("object") external dispatch classes (extend shapes with convexity and a mutable position in space)
- Prefix calculator (with numbers and strings)
- The Expression Problem
- Point/color-point
- Downcast
- Dummy visitor
- Dummy examples
Download
The current version of the compiler and example code can be downloaded here:
- compiler-v0.6.zip examples-v0.6.zip (April, 2025)
Syntax
The syntactical modifications to Eiffel are the following:
Semantics
To ensure a static safe mechanism, proper semantic rules are required. In short, it must be ensured sanity in the definition of external dispatch classes, for instance ensuring a covariant redifinition in the class formal arguments in, at least, one of the formal arguments. Also it must ensure non-conflicting descendants, a complete and unambiguous instantiation -- meaning, that it must be statically ensured that all possible combinations of dispatch objects type are dispatched to one, and only one, effective external dispatch class.
See External dispatch in object creation (also here) for a more detailed presentation of the applicable semantic rules.