Multiple Dispatch As Dispatch on Tuples Gary T
Total Page:16
File Type:pdf, Size:1020Kb
Computer Science Technical Reports Computer Science 7-1998 Multiple Dispatch as Dispatch on Tuples Gary T. Leavens Iowa State University Todd Millstein Iowa State University Follow this and additional works at: http://lib.dr.iastate.edu/cs_techreports Part of the Systems Architecture Commons, and the Theory and Algorithms Commons Recommended Citation Leavens, Gary T. and Millstein, Todd, "Multiple Dispatch as Dispatch on Tuples" (1998). Computer Science Technical Reports. 131. http://lib.dr.iastate.edu/cs_techreports/131 This Article is brought to you for free and open access by the Computer Science at Iowa State University Digital Repository. It has been accepted for inclusion in Computer Science Technical Reports by an authorized administrator of Iowa State University Digital Repository. For more information, please contact [email protected]. Multiple Dispatch as Dispatch on Tuples Abstract Many popular object-oriented programming languages, such as C++, Smalltalk-80, Java, and Eiffel, do not support multiple dispatch. Yet without multiple dispatch, programmers find it difficult to express binary methods and design patterns such as the ``visitor'' pattern. We describe a new, simple, and orthogonal way to add multimethods to single-dispatch object-oriented languages, without affecting existing code. The new mechanism also clarifies many differences between single and multiple dispatch. Keywords Multimethods, generic functions, object-oriented programming languages, single dispatch, multiple dispatch, tuples, product types, encapsulation, modularity, static typechecking, subtyping, inheritance, Tuple language Disciplines Systems Architecture | Theory and Algorithms Comments Copyright © 1998 by the Association for Computer Machinery, Inc., 1998. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or ommec rcial advantage, the copyright notice, the title of this publication, and its date appear, and notice is given that copying is by permission of ACM, Inc. To copy otherwise, to republish, to post on servers, or to redistribute to lists, requires prior specific permission and/or a fee. This article is available at Iowa State University Digital Repository: http://lib.dr.iastate.edu/cs_techreports/131 Multiple Dispatch as Dispatch on Tuples Gary T. Leavens and Todd D. Millstein TR #98-03b April 1998, Revised May, July 1998 To appear in OOPSLA ‘98 Object-Oriented Programming, Systems, Langauges, and Applications, Vancouver, British Columbia, October 18-22, 1998. Keywords: Multimethods, generic functions, object-oriented programming languages, single dispatch, multiple dispatch, tuples, product types, encapsulation, modularity, static typechecking, subtyping, inheritance, Tuple language. 1994 CR Categories: D.3.1 [Programming Languages] Formal Definitions and Theory — semantics; D.3.2 [Programming Languages] Language Classifications — object-oriented languages; D.3.3 [Programming Languages] Language Constructs and Features — abstract data types, control structures, procedures, functions, and subroutines; D.3.m [Programming Languages] Miscellaneous — multimethods, generic functions, single dispatch, multiple dispatch, type systems; F.3.3 [Logics and Meanings of Programs] Studies of Program Constructs — control primitives, type structure. Copyright © 1998 by the Association for Computing Machinery, Inc., 1998. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commerical advantage, the copyright notice, the title of this publication, and its date appear, and notice is given that copying is by permission of ACM, Inc. To copy otherwise, to republish, to post on servers, or to redistribute to lists, requires prior specific permission and/or a fee. Department of Computer Science 226 Atanasoff Hall Iowa State University Ames, Iowa 50011-1040, USA Multiple Dispatch as Dispatch on Tuples Gary T. Leavens Todd D. Millstein Department of Computer Science, Department of Computer Science and Engineering, Iowa State University, University of Washington, 229 Atanasoff Hall, Ames, Iowa, 50011 USA Box 352350, Seattle, WA 98195-2350 USA [email protected] [email protected] +1 515 294-1580 +1 206 543-5118 ABSTRACT tuple gives multiple dispatch. To illustrate the idea, we have * Many popular object-oriented programming languages, such designed a simple class-based OO language called Tuple . as C++, Smalltalk-80, Java, and Eiffel, do not support While perhaps not as elegant as a language built directly with multiple dispatch. Yet without multiple dispatch, multiple dispatch, we claim the following advantages for our programmers find it difficult to express binary methods and mechanism: design patterns such as the “visitor” pattern. We describe a 1. It can be added to existing single dispatch languages, new, simple, and orthogonal way to add multimethods to such as C++ and Java, without affecting either (a) the single-dispatch object-oriented languages, without affecting semantics or (b) the typing of existing programs written existing code. The new mechanism also clarifies many in these languages. differences between single and multiple dispatch. 2. It retains the encapsulation mechanisms of single- dispatch languages. Keywords 3. It is simple enough to be easily understood and Multiple dispatch, multimethods, generic functions, typing, remembered, we believe, by programmers familiar with single dispatch, binary methods, semantics, language design, standard single dispatch. Tuple. 4. It is more uniform than previous approaches of 1 INTRODUCTION incorporating multiple dispatch within a single- dispatching framework. Single dispatch, as found in C++ [Stroustrup 97], Java [Arnold & Gosling 98, Gosling et al. 96], Smalltalk-80 To argue for the first two claims, we present the semantics of [Goldberg & Robson 83], and Eiffel [Meyer 92, Meyer 97], the language in two layers. The first layer is a small, class- selects a method using the dynamic class of one object, the based single-dispatching language, SDCore, of no interest in message’s receiver. Multiple dispatch, as found in CLOS itself. The second layer, Tuple proper, includes SDCore and [Chapter 28, Steele 90] [Paepcke 93], Dylan [Shalit 97, adds multiple dispatch by allowing messages to be sent to Feinberg et al. 97], and Cecil [Chambers 92, Chambers 95], tuples. generalizes this idea, selecting a method based on the Our support for the third claim is the simplicity of the dynamic class of any subset of the message’s arguments. mechanism itself. If, even now, you think the idea of sending Multiple dispatch is in many ways more expressive and messages to tuples is clearly equivalent to multiple dispatch, flexible than single dispatch in object-oriented (OO) then you agree. To support the fourth claim, we argue below programming [Bobrow et al. 86, Chambers 92, Castagna 97, that our mechanism has advantages over others that solve the Moon 86]. same problem of incorporating multiple dispatch into In this paper we propose a new, simple, and orthogonal way existing single-dispatching languages. of adding multiple dispatch to existing languages with single The rest of this paper is organized as follows. In Section 2 we dispatch. The idea is to add tuples as primitive expressions describe the single-dispatching core of Tuple; this is needed and to allow messages to be sent to tuples. Selecting a only to show how our mechanism can be added to such a method based on the dynamic classes of the elements of the language. In Section 3 we describe the additions to the single- dispatching core that make up our mechanism and support our first three claims. Section 4 supports the fourth claim by comparison with related work. In Section 5 we offer * With apologies to Bruce et al., whose TOOPLE language [Bruce et al. 93] is pronounced the same way. 1 class Point { inheritance causes no problems with respect to the new ideas fields (xval:int, yval:int) we are advocating.) method x():int { xval } method y():int { yval } We use a standard syntax for message sends. For example, if method distanceFrom(l:Line):int { ... } p1 is an instance of Point or a subclass, then the } expression p1.distanceFrom(ln2) invokes the instance’s distanceFrom method with the argument class ColorPoint inherits Point { ln2. Within the method body, the keyword self refers to fields (colorval:Color) method color():Color { colorval } the receiver of the message, in this case p1. } Dynamic dispatching is used to determine which method to Figure 1: The classes Point and ColorPoint. invoke. In particular, the receiver’s class is first checked for a method implementation of the right name and number of arguments. If no such method exists, then that class’s some conclusions. In Appendix A we give the concrete immediate superclass is checked, and so on up the syntax of Tuple, and in Appendix B we give the language’s inheritance hierarchy until a valid implementation is found formal typing rules. (or none is found and a message-not-understood error occurs). 2 SDCORE, THE SINGLE-DISPATCHING CORE OF TUPLE For simplicity, and to ease theoretical analysis, we have not In this section, we introduce the syntax and semantics of included assignment and mutation in SDCore. Again, these SDCore by example. could be easily added. 2.1 Syntax and Semantics 2.2 Type Checking for SDCore The single-dispatching