Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!rutgers!labrea!decwrl!pyramid!prls!philabs!pwa-b!mmintl!franka From: franka@mmintl.UUCP (Frank Adams) Newsgroups: comp.lang.smalltalk Subject: Re: addition Message-ID: <2349@mmintl.UUCP> Date: Mon, 7-Sep-87 22:42:10 EDT Article-I.D.: mmintl.2349 Posted: Mon Sep 7 22:42:10 1987 Date-Received: Fri, 11-Sep-87 00:38:32 EDT References: <245100011@orstcs> <102@laura.UUCP> <1164@pdn.UUCP> Reply-To: franka@mmintl.UUCP (Frank Adams) Organization: Multimate International, E. Hartford, CT. Lines: 83 In article <1164@pdn.UUCP> colin@pdn.UUCP (Colin Kendall) writes: >What I can't understand is the objection that so many people make to >testing an object's class. It seems to be an ungrounded esthetic >objection, and one which was seemingly not shared by the designers, >inasmuch as there are about 120 such tests in the original Smalltalk-80 >image. I think the objection is for the most part more recent than the original Smalltalk-80 image; certainly more recent than much of the work thereon. People noticed that these class checks caused problems, by reducing later polymorphism. The theory of OOP developed further, and they don't fit according to most versions of it. (Of course, with true multiple inheritance, checking for "isKindOf:" becomes more acceptable.) >Well, perhaps there is a good reason for the objection. I'd like >to know it. I'd also like to know whether there is a similar objection >to testing whether an object understands a given message. Certainly not as strong an objection. But there is one problem in Smalltalk: the method which implements the message may only generate an error. In this case, the object really doesn't "respond to" the message, but "respondsTo:" will return "true". >This is similar to what Martin Mosner is proposing in his example >where he tests if an object understands a sophisticated message, >sends it if so, but if not tests for understanding of a different >(slightly less sophisticated) message, etc. In his example of >an Arc trying to draw itself on a DisplayMedium, if the >DisplayMedium is sophisticated enough to know how to help, it >is asked to do so; else the Arc resorts to more primitive methods. An alternative is make an unconditional message send in the Arc method, and put the "more primitive methods" in the default implementation in DisplayMedium. This is the multiple polymorphism solution, and it seems to me to be better in this case. -------------------------------------------------------------- The approach I currently favor for arithmetic is to use multiple polymorphism, but to try to limit the number of methods it is used for. Thus, I use multiple polymorphism for addition, but not for subtraction. Instead, I have the method: (Number) - aNumber ^self + aNumber negated For speed, I also implement "-" at other levels, with a type check: (Complex) - aNumber ^aNumber class == Complex ifTrue: [real - aNumber real i: imaginary - aNumber imaginary] ifFalse: [aNumber negated plusComplex: self] The last block could be just "[super - aNumber]", but this technique is faster. "plusComplex" is a secondary method for "+": (Complex) + aNumber ^aNumber plusComplex: self I am not entirely happy with this approach, but it represents a reasonable compromise between speed and space considerations. ----------- An issue I have not resolved is typified by the following: I have a class "Modulus" with components "residue" and "modulus". Arithmetic can be performed on two Moduli with the same modulus; this is standard modular arithmetic. Likewise, one can multiply a Modulus by a scalar constant, etc. I also have a class "Polynomial", which has an indexed instance variable with the coefficients of a Polynomial. Again, one perform arithmetic on Polynomials, or with a Polynomial and a scalar. The problem is, when I multiply a Modulus by a Polynomial, how do I decide which is the scalar? The Modulus could have residue and modulus both of class Polynomial, so that I should multiply the residue by the Polynomial, and reduce the result mod the modulus. Alternatively, the Polynomial could have coefficients which are Moduli, with the same modulus as the Modulus, in which case I should multiply each coefficient by the modulus. (Of course, the multiplication could also be illegal.) How can one set things up so that it is quickly and easily decided which method is to be used? -- Frank Adams ihnp4!philabs!pwa-b!mmintl!franka Ashton-Tate 52 Oakland Ave North E. Hartford, CT 06108