skip to main content
article
Free Access

Multiple downcasting techniques

Published:01 May 1999Publication History
Skip Abstract Section

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.

References

  1. {1} Craig Chambers (1995) The Cecil Language: Specification and Rationale, http://www.cs.washington.edu/research/projects/cecil/cecil/ www/cecil-home.html, University of Washington.Google ScholarGoogle Scholar
  2. {2} Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (1995) Design Patterns, Addison Wesley.Google ScholarGoogle Scholar
  3. {3} Bertrand Meyer (1997) Object-Oriented Software Construction, Second Edition, Prentice Hall. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. {4} Scott Meyers (1996) More Effective C++, Addison Wesley.Google ScholarGoogle Scholar
  5. {5} Hans Muller, John Rose, James Kempf, and Tayloe Stansbury (1989) The Use of Multimethods and Method Combination in a CLOS Based Window Interface, in SIGPLAN Notices, Volume 24, Number 10, Pages 239 to 253, October 1989, ACM Press. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. {6} Puppydog P. O. P. and L. B. S. Raccoon (1997) A Middle-Out Concept of Hierarchy, in Software Engineering Notes, Volume 23, Number 3, Pages 111 to 119, May 1998, ACM Press. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. {7} David L. Shang (1996) Are Cows Animals?, http://www.sigs.com/publications/docs/oc/9601/oc9601.c.shang.html, in Object Currents, Volume 1, Issue 1, January 1996, SIGS Publications.Google ScholarGoogle Scholar
  8. {8} Bjarne Stroustrup (1994) The Design and Evolution of C++, Addison Wesley. Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. {9} IBM Systems Journal, Volume 35, Number 2, 1996, IBM.Google ScholarGoogle Scholar

Index Terms

  1. Multiple downcasting techniques

      Recommendations

      Comments

      Login options

      Check if you have access through your login credentials or your institution to get full access on this article.

      Sign in

      Full Access

      • Published in

        cover image ACM SIGSOFT Software Engineering Notes
        ACM SIGSOFT Software Engineering Notes  Volume 24, Issue 3
        May 1999
        79 pages
        ISSN:0163-5948
        DOI:10.1145/311963
        • Editor:
        • Will Tracz
        Issue’s Table of Contents

        Copyright © 1999 Authors

        Publisher

        Association for Computing Machinery

        New York, NY, United States

        Publication History

        • Published: 1 May 1999

        Check for updates

        Qualifiers

        • article

      PDF Format

      View or Download as a PDF file.

      PDF

      eReader

      View online with eReader.

      eReader