Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!spool.mu.edu!uunet!mcsun!corton!ilog!barbes!davis From: davis@barbes.ilog.fr (Harley Davis) Newsgroups: comp.object Subject: Re: Readability of Ada Message-ID: Date: 24 Apr 91 12:19:56 GMT Article-I.D.: barbes.DAVIS.91Apr24141956 References: <3878@ssc-bee.ssc-vax.UUCP> <20245@alice.att.com> Sender: davis@ilog.fr Followup-To: comp.object Organization: ILOG S.A., Gentilly, France Lines: 112 In-reply-to: jls@rutabaga.Rational.COM's message of 23 Apr 91 01:38:37 GMT In article jls@rutabaga.Rational.COM (Jim Showalter) writes: %So, how about comparing the languages on the example originally used %to introduce the technique we now commonly refer to as object-oriented %programming in the Simula textbook SIMULA BEGIN? The problem is to %write a little program that define a set of shapes and manipulates %them (I'll rotate a list of shapes). Then we add another shape and %see what effort that takes and whether the original code manipulates %shapes still works. There are other `tests' for object orientation %(both harder and easier on the expressiveness of programming languages), %but this one has the property of demonstrating a realistic problem %of program organization in a small problem and has been around for %about 20 years. I've been wanting to ask someone who purports to know the answer this question for over a year. Consider the shapes example. Suppose that the given example goes out in binary form, and arrives at my site, and I want to add a new shape. Suppose that the initial shapes were limited to triangles and squares. Suppose that I now add a Circle, which has a new method defined for it that does NOT apply to triangles or squares and which was never previously defined in the base class for shapes--radius. This method returns the radius as some floating point number from 0 to whatever. Now, I want to take a heterogeneous list of shapes, including triangles, circles, and squares, and I want to iterate over the list and print out all of the radii. How do I do this? I can't get elements out of the list and call the Radius method on all of them, because not all of them HAVE such a method defined. I can't add the new method to the base class (with a null implementation as the default for those shapes for which it is a meaningless operation) because the base class is in binary. I can't ask the shapes to tell me their Kind because there is no such operation defined on them in C++. It is my claim that solving this problem in C++ results in a solution that is every bit as messy as simply using a discriminated record and an enumeration type in Ada--and Ada doesn't HAVE inheritance. I don't know about C++, but in several Lisp-based OO languages this is cake. Here's a solution in EuLisp: (defmodule shape (defclass shape () () predicate shapep) (defclass triangle (shape) (...some triangle slots...) constructor make-triangle predicate trianglep) (defclass rectangle (shape) (...some rectangle slots...) ...) (defgeneric rotate (object theta)) (defmethod rotate ((object triangle) (theta real)) ...triangle rotation code...) ... other methods for rotate ... (export shape triangle rectangle rotate ...) ) This module is delivered in binary form to the client. The fact that ROTATE is generic is documented. The client then does: (defmodule circle-module (expose shape) ;;; New subclass of SHAPE (defclass circle (shape) ((radius accessor circle-radius initarg radius)) constructor (make-circle (radius)) predicate circlep) ;;; Adding a new method for ROTATE. (defmethod rotate ((object circle) (theta real)) ;; do nothing for circles ()) ;;; Printing out the radii of circles in a list. (defun print-radii (shape-list) ;; Prints the radii of all the circles in SHAPE-LIST. (mapc shape-list (generic-lambda (shape) method (((shape circle)) (print (circle-radius shape)))))) ) There you go. Nearly identical solutions will work in CommonLisp and Le-Lisp, and probably every other Lisp with even the simplest object oriented framework. -- Harley Davis -- ------------------------------------------------------------------------------ nom: Harley Davis ILOG S.A. net: davis@ilog.fr 2 Avenue Gallie'ni, BP 85 tel: (33 1) 46 63 66 66 94253 Gentilly Cedex, France