tombre@crin.crin.fr (Karl Tombre) (02/25/88)
I have a question about Object Pascal, the object extension to Pascal written by Apple for the MAcintosh. I have read Kurt Schmucker's book (Object-Oriented Programming for the Macintosh) without finding an answer. Many object languages (Smalltalk, Objective-C for instance) use dynamic typing. On the contrary, Simula-67, C++, Eiffel and some others use static typing, and achieve dynamic binding by means of the virtual function mechanism. Schmucker says several times that Object Pascal also uses static typing (more precisely compile-time type checking), but does not explain how dynamic binding is obtained. Some excerpts from his book (I don't copy the integral text, as americans seem to be crazy about copyrights ;-) : p. 67 : he speaks of examples of what Object Pascal cannot determine until run-time and says that this gives the impression that a great deal of decisions must be delayed until then. But according to him, Object Pascal makes very few decisions at run-time... and so on. But no word on how dynamic binding is made possible. p. 323 : in the chapter on Clascal, he notes that Object Pascal has no ABSTRACT and DEFAULT keywords and that Apple found a "better method"... My question : is this method a trade secret, or is it the good old virtual mechanism of Simula ? Said in another way : does Object Pascal detect which functions must be dynamically bound (as they are overriden in several classes) and implement this by a pointer to a function associated with the instance, or something like that ? Or is there another mechanism for achieving this ? Or is there really a method lookup function searching at run-time for the right procedure to call ? Can somebody give an answer to this ? Thanks in advance, --- Karl Tombre @ CRIN (Centre de Recherche en Informatique de Nancy) EMAIL : tombre@crin.crin.fr -- tombre@crin.UUCP POST : Karl Tombre, CRIN, B.P. 239, 54506 VANDOEUVRE CEDEX, France PHONE : +33 83.91.21.25
lsr@Apple.COM (Larry Rosenstein) (03/01/88)
In article <416@crin.crin.fr> tombre@crin.crin.fr (Karl Tombre) writes: > >mechanism. Schmucker says several times that Object Pascal also uses static >typing (more precisely compile-time type checking), but does not explain how >dynamic binding is obtained. Object Pascal is much like Simula in this respect. It uses static type checking and dynamic binding. >p. 67 : he speaks of examples of what Object Pascal cannot determine until >run-time and says that this gives the impression that a great deal of >decisions must be delayed until then. But according to him, Object Pascal >makes very few decisions at run-time... and so on. In that particular example, the variable anAnimal refers to different types of objects. The static type checking requires that each object be a kind of TAnimal (since that is how anAnimal is declared on the prvious page). In Smalltalk (which has no static typechecking), anAnimal could refer to an integer, which might cause problems if code later assumes that it is an animal. The comment in the book about Object Pascal making very few run-time decisions I think refers to the fact that ordinary arithmetic and ordinary procedure calls in Object Pascal work exactly as those in non-Object Pascal. This is different from Smalltalk, in which all routine invocations are method calls, and even numbers are defined by classes. >But no word on how dynamic binding is made possible. The book is not intended to go into the implementation details. For that, you should look at Ken Doyle's article in the December 1986 MacTutor. >p. 323 : in the chapter on Clascal, he notes that Object Pascal has no >ABSTRACT and DEFAULT keywords and that Apple found a "better method"... In Clascal, the ABSTRACT and DEFAULT keywords were both used to indicate methods in a class that were expected to be overridden in subclasses. (ABSTRACT required such an override). The implementation of Clascal used this information to group methods into ones that were not expect to be overridden and ones that were expected to be overridden. The table storage for the first group of methods could be shared between different classes, if in fact the methods weren't overridden. In Object Pascal we achieved the same goal of reducing the memory space for tables with a different mechanism, and so the ABSTRACT and DEFAULT keywords were not needed. (Although there are times when you would like to define abstract methods and not have to implement them.) >Said in another way : does Object Pascal detect which functions must be >dynamically bound (as they are overriden in several classes) and implement >this by a pointer to a function associated with the instance, or something >like that ? Or is there another mechanism for achieving this ? Or is there >really a method lookup function searching at run-time for the right >procedure to call ? The answer to the question is YES, although this is not the reason Clascal had the keywords. Again, I would look at the article mentioned above, but I will try to summarize. The default implementation of methods in Object Pascal is very straightforward. Each class has a table of the methods implemented in that class. An instance of that class points to the table. When you call a method, the method dispatcher finds the table and scans it linearly until it finds the method. If if doesn't, then it follows a pointer to the superclass table and continues. The static type checking of Object Pascal guarantees that the method will be found eventually. (In other words, you cannot get the methodNotUnderstood exception of Smalltalk.) When you link your Object Pascal program you have the option of "optimizing" the method calls. The main thing this does is to take the method tables (1 per class) and create a lot of smaller tables, one for each method. These tables contain a list of all the classes that implement this method. In the course of constructing these tables, the linker detects methods that have only one implementation, and redirects all calls to those methods to the actual code. This makes those method calls into direct procedure calls with no overhead for method lookup. (There are also other situations in which the linker can statically bind method calls.) For these methods, there is no table space used. If a method has more than one implementation, then there will be a table of all the implementors of that method. A call to that method will result in a linear search of that table. The table also has a 1-entry cache. I once measured the overhead for method calls in Object Pascal (this was on a Mac Plus, which is a 68000 machine). The usual overhead for a method call (comapred to a standard procedure call) was about 60 microseconds. If the method was found in the cache, this was cut to about 30 uSec. If a method call could be statically bound, then the runtime overhead was 0. If you have any other questions about Object Pascal, let me know.
lsr@Apple.COM (Larry Rosenstein) (03/01/88)
Our new news software failed to include my .signature. -- Larry Rosenstein, Object Specialist Apple Computer, Inc. 20525 Mariani Ave, MS 32E Cupertino, CA 95014 AppleLink:Rosenstein1 domain:lsr@Apple.COM UUCP:{sun,voder,nsc,decwrl}!apple!lsr