From: Ryan Pavlik Date: 2003-04-12T16:23:00+09:00 Subject: Re: Does dynamic typing make it easier to place an object in a container? On Sat, 12 Apr 2003 13:40:07 +0900 Joel VanderWerf wrote: > Mark Wilson wrote: > > The following is from > > http://www.wikipedia.org/wiki/Objective_C_programming_language > > > > Is this a correct statement about the value of dynamic typing? And what > > exactly does it mean? > > > > "However when one does need dynamic typing, it becomes tremendously > > powerful. Consider a simple example, placing an object in a container. > > In strongly-typed [sic] languages like C++ and Java, the programmer is > > forced to write a container class for a generic type of object, and then > > cast back and forth between the abstract generic type and the real type > > (C++ can also use templates, but many find these somewhat cumbersome and > > obscure). Sadly, casting breaks the discipline of strong typing -- if > > you put in an Integer and read out a String, you get an error. This is > > annoying when you consider that it was a string when you put it in there > > three lines earlier, and now you have to tell the compiler that it's a > > string coming back out just to call toUpperCase(). Dynamic typing > > thankfully removes this problem." This is a very confused explanation. The writer has confused strong and static typing. Strong typing is giving things a particular type, which lets you tell what a particular thing _is_. Weak typing is the opposite of strong typing. A good example of this is Perl. The string "1" and the number 1 are interchangeable. Ruby is strongly-typed; a String is not an Integer, and (basically) everything is of a particular class. Static and dynamic are different. They're like another axis. Static typing means a variable has a given type, and can only refer to that type, requiring casting. Dynamic typing is the opposite. C++ is statically typed; Ruby is dynamically typed. > For a great explanation, see Pragmatic Dave's article at: > > http://www.pragmaticprogrammer.com/cgi-local/pragprog?JavaIsUntyped This is even worse. Java is strongly, statically typed. The fact you can say Object o = new OtherThing is neither "untyped" (weak typing) or dynamically typed. Given class OtherThing inherits from Object, this assignment is correct: OtherThing _is_ an Object. Static typing rules are unbroken, and since we're dealing with types, we have, by definition, strong typing. An analogy to this page would be looking at C++ and saying, "well, until you refer to an object, it's just a big blotch of memory, so Neo, There Is No Type." (Of course, at some point, everything is just memory, but reducing things to the lowest common denominator isn't really correct and certainly isn't useful.) Another way would be looking at C (or C++) and saying you can refer to everything as a void*, so it's weakly dynamically typed. These language features all let you do some things similar to dynamic typing or weak typing, but that doesn't make the language dyamically or weakly typed. I think the biggest problem is still the confusion between static/dynamic and strong/weak typing (we had this discussion on this list a month or so ago, look through the archives). This message has gone on far too long already, but I'll address the question in the subject line shortly. With OOP, as you can refer to an object by a generic parent class, it's really no more difficult in a static language to make a container than in a dynamic one. Either way you can filter your classes to make homogenous containers, or allow heterogenous containers. hth, -- Ryan Pavlik "My plans are *always* practical! It's the laws of physics that get in the way of my success." - 8BT