Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!husc6!bloom-beacon!mit-hermes!iuvax!pur-ee!uiucdcs!uiucdcsp!johnson From: johnson@uiucdcsp.cs.uiuc.edu Newsgroups: comp.lang.smalltalk Subject: Re: A question... Message-ID: <80500012@uiucdcsp> Date: Thu, 6-Aug-87 16:40:00 EDT Article-I.D.: uiucdcsp.80500012 Posted: Thu Aug 6 16:40:00 1987 Date-Received: Sat, 8-Aug-87 16:54:02 EDT References: <4357@felix.UUCP> Lines: 28 Nf-ID: #R:felix.UUCP:4357:uiucdcsp:80500012:000:1144 Nf-From: uiucdcsp.cs.uiuc.edu!johnson Aug 6 15:40:00 1987 The + method for Point in Smalltalk-80 converts the argument to a point and then adds it. It looks something like + anArg | aPoint | aPoint <- anArg asPoint. ^(x + aPoint x) @ (y + aPoint y) The asPoint message to a Point returns the receiver, while the asPoint message to a number returns (self @ self). This seems to me a kludge. A better solution is to use "double dispatching" to pick the method based on the classes of both the receiver and the argument. Dan Ingals had a paper describing this in OOPSLA'86. The basic idea is that the + method for Point would look something like + anArg ^anArg addToPoint: self The class of anything that might ever be added to a point would then need an addToPoint: method. You can do this with all the number classes and the arithmetic operators. It results in a nicer system than the one in Smalltalk-80 based on "coercion", especially if you want to have matrices, polynomials, functions, etc. as part of your arithmetic. I teach my students that good Smalltalk code should NEVER explicitly test the class of an object. Double dispatching is usually a better alternative.