gyro@kestrel.edu (Scott Layson Burson) (06/29/91)
In article <28692A4A.59B7@tct.com> chip@tct.com (Chip Salzenberg) writes: >According to djones@megatest.UUCP (Dave Jones): >>> ... Never design code that requires downcasts. >>> Or, in other words: if the static type is important, why worry >>> about regaining it? Don't lose it in the first place. >> >>Something tells me this has all been hashed over previously. The "static >>type" was not "lost" in the first place. It did not exist in the first place. > >By "static type" I mean "compile-time type". Perhaps an example will >clarify my point. > >If you put a Circle into a ShapeList, you lose the Circle's >compile-time type, because ShapeList.first() returns |Shape*|. >Suppose that you decide to call a Circle-specific function on a Circle >stored in a ShapeList. Some would cast the |Shape*| to a |Circle*| >and carry on. My coding practice would require you to replace the >ShapeList with a CircleList. > >Of course, that change may not be practical, perhaps because there are >other non-Circle objects in the ShapeList. If so, congratulations! >You have just discovered that the function in question is actually a >Shape function in disguise. You should therefore add it to the Shape >interface, presumably as a new virtual function. > >Clearer now? I understand what you are saying, but I don't understand the degree of dogmatism with which you are saying it. As it happens, I have been working for a couple of years in an obscure language that has *only* class tests; it does not have anything corresponding to virtual functions. This leads to a style in which many functions consist of a sequence of class tests on one of the arguments, with each test having an associated piece of code. While this is sometimes inconvenient -- I would sometimes (though not always!) like to organize the code by type, as in an object-oriented system, rather than by operation -- I have simply not noticed that it leads to any particular disaster of the sort that you seem to be convinced lies in wait for code written that way. So I put you the question: what do you expect to happen if I don't follow your advice, i.e., if I implement class tests and type-safe downcasting, and use them in code? What terrible fate do you expect to befall me? -- Scott Gyro@Reasoning.COM