Xref: utzoo comp.object:3018 comp.lang.misc:7253 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!batcomputer!cornell!uw-beaver!fluke!ssc-vax!dmg From: dmg@ssc-vax.uucp (David M Geary) Newsgroups: comp.object,comp.lang.misc Subject: Type Systems and Dynamic Binding Message-ID: <3776@ssc-bee.ssc-vax.UUCP> Date: 2 Apr 91 23:08:41 GMT Sender: news@ssc-vax.UUCP Organization: Boeing Aerospace & Electronics Lines: 114 Originator: dmg@ssc-vax In article <7689:Mar2623:28:5091@kramden.acf.nyu.edu>> (Dan Bernstein) writes: >>In article <48805@nigel.ee.udel.edu> new@ee.udel.edu (Darren New) writes: >> Hmm... How about a window with a hetrogeneous collection of buttons, sliders, >> text displays, etc, all of which respond to "redraw" and "is the mouse >> over you"? >struct {any object; void (*redraw)(); int (*ismouseover)();} *whatsinwindow(); >Here ``any'' is a polymorphic type (e.g., void * in C), and *redraw >takes an ``any'' argument. Naturally, languages like C++ encapsulate the >above struct into a single ``class,'' but that's merely syntactic sugar. A void* is not a "polymorphic type", and C++ provides much more than "merely syntactic sugar". In article <25655:Mar2803:50:2491@kramden.acf.nyu.edu> (Dan Bernstein) writes: >In article <49087@nigel.ee.udel.edu> new@ee.udel.edu (Darren New) writes: >> Am I crazy, or isn't (void *) impossible to indirect? Don't you have >> to type-cast it first? Isn't this dynamic typing implemented on top of ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> C? ^^^ >YES. People are tossing around the term ``dynamic typing'' as if it were >some truly important feature. ``Yeah, Bob, not only does the language >have *loops*, but it has *dynamic typing*!'' ``Wow! *Dynamic typing*? >Really?'' NO. This is *not* an example of dynamic typing implemented on top of C. > On the occasions when I want dynamically typed variables, I >can use them without trouble in C. No you cannot; C has no support whatsover for "dynamically typed variables." >Sure, this requires a void pointer type. SO WHAT? As I pointed out when >I entered this discussion, the language does need *some* type that can >represent *all* values if it's going to support dynamic typing. C has >such a type, and adequate macros, and that's enough for me to get work >done. C has absolutely nothing akin to dynamic typing. One cannot "implement dynamic typing on top of C", without writing some kind of preprocessor, such has been done with Objective-C. I think that there is some confusion over terminology here: 1) Dynamic binding: Dynamic binding is a feature whereby the compiler chooses the correct function to apply to some object, depending upon the *type* of the object. If I have dynamic binding, I can have code like the following (in C++): int list::insert(linkable& lnk, int pos) { .... lnk.setNextNode(NULL); // Compiler chooses appropriate function to call // for the given object. ... } The C compiler will allow *any type* to be passed to list::insert(), as long as the type is a linkable (or is a descendent of class linkable). Therefore, the programmer may send different types to the function list::insert(), and the compiler determines the appropriate function to call, depending upon the type of lnk for lnk.setNextNode(). However, since C++ is statically typed, the first argument to list::insert() *must* be either a linkable, or a descendent of linkable. If not, the COMPILER WILL GENERATE A COMPILE-TIME ERROR. Bertrand Meyer, of Eiffel fame, has already posted an example of this kind in Eiffel, (which, like C++ has multiple inheritance and dynamic binding). 2) Dynamic typing: With "dynamic typing", one may write (in Objective-C): id collection = [Container new]; [collection add:[Dog new]]; [collection add:[Cat new]]; [collection add:[bottleOWine new]]; Here, one is adding arbitrary elements to a collection. There is *no restriction whatsoever* as to the type of things that can be added to the collection. Note that this differs from the C++ example, where all elements added to the list *must* be linkables (or descendents of class linkable). The "pitfall" of dynamic typing, of course, is that it is possible to try to apply a method to an object, which the object is not prepared to deal with, and the error MAY NOT BE CAUGHT UNTIL RUNTIME. In a statically typed language, such as Eiffel and C++ the compiler will simply not allow that to happen. Lastly, note that neither of the two examples given above may be implemented in C, without the programmer *at one time or another* explicitly identifying the types of all objects being operated upon. (In other words, in Dan's example above, it is the programmer's, not the compiler's, responsiblity to ensure that the correct functions are called for the appropriate types). Here, BTW, are some good references. I would suggest that Dan read some of these before providing any more erroneous information in the tradition of his previous postings: Object Oriented Software Construction, Bertrand Meyer. (excellent, BTW) Object Oriented Programming, Brad Cox. An introduction To OOP and C++, Wiener & Pinson