Multiple downcasting techniques

  • Authors:
  • Puppydog P. O. P.;L. B. S. Raccoon

  • Affiliations:
  • -;-

  • Venue:
  • ACM SIGSOFT Software Engineering Notes
  • Year:
  • 1999

Quantified Score

Hi-index 0.00

Visualization

Abstract

In this paper, we describe and compare techniques that implement multiple downcasting in strongly-typed languages. We conclude that multimethods stand out as the single best technique. In the first section, we describe seven commonly used techniques. In the second section, we compare these seven techniques using ten criteria. And, in the third section, we comment on some additional techniques.Multiple downcasting occurs so often that developers use a variety of terms to describe it, and a variety of language constructs and patterns to implement it. "Feed the animals," "driver-vehicle," and "parallel hierarchies" are well-known examples that require multiple downcasting. "Multiple type dispatch" and "covariant subclassing" identify different facets of multiple downcasting. "Dynamic type casting," "typecase statements," and the "visitor pattern" are frequently used to implement multiple downcasting. We believe that multiple downcasting is not a mistake or the result of poor program design, rather multiple downcasting is a specific technique that implements a specific kind of application semantics.In the animal hierarchy shown in Figure 1, cows eat grass, wolves eat meat, and in the superclass, animals eat food. The generalization that animals eat food is imprecise: it ignores the facts that cows only eat grass and wolves only eat meat. So, how should developers write a function to safely "feed the animals" without getting the types mixed up? In Program 1, we show how a developer might like to "feed the animals" in C++. The feed_unsafely function may fail, while the feed_safely function works properly for all combinations of foods and animals. The animal hierarchy will not compile in C++, because the eat method is covariant.In this paper, we elaborate ideas and examples from A Middle-Out Concept of Hierarchy. We adapted the "feed the animals" example from Are Cows Animals? by Shang.