From: Chris Pine Date: 2003-03-24T23:21:40+09:00 Subject: Re: Types (was Re: Ruby lecture slides (was Strong advantages over Python)) ----- Original Message ----- From: "Jim Freeze" > Take this example: > > class Fred > def shout > puts "wilma" > end > end > > f = Fred.new > > o = Object.new > def o.shout > puts "wilma" > end > > 'f' and 'o' have different classes (Fred and Object respectively) but > they have indistinguishable types (for all intents and purposes). In Thanks for the fine example, but for those of us who are a little slow, what exactly are the types of 'f' and 'o'? --------------------------- They are the same (unnamed) type. I think the point behind duck typing is that you never have to say "this is a Duck". You never even use the word "Duck". The idea is that if you have already used the words "quack", "waddle", "swim", etc..., then you have done enough already. If I understand it correctly, it is also a bit incorrect to ask what THE types of 'f' and 'o' are, because they could have many. But in this case, since they respond to the exact same messages in the exact same way, they are the same. (Well, they don't respond to the `class' method in the same way, but nevermind that...) So my question (which proceeds on the assumption that what I have said so far is true): If an object's type(s) are determined by what methods it responds to, and those can be added and removed dynamically, then an object can be "cast" in some sense. Then is it correct to say that Ruby is strongly typed? It seems like Ruby is "strongly classed", but weakly typed. (Or is this simply a different usage of the word "type"?) As I understand it, static/dynamic typing refers to the variables having types (so Ruby is dynamically typed), and strong/weak refers to objects' types being immutable. Or is a language strongly typed if you can always determine its type(s) *at any one time*? If so, then Ruby is still strongly typed. (However, when people say Ruby is strongly typed, they almost always say, "Because you can't change an object's class." This seems to have nothing to do with it.) Assuming I got that right, it seems that Ruby is more than just strongly/dynamically typed. It is also multi-typed (objects can have many types at once), and mutably-typed (objects' types can change). Though, I guess languages which equate (confuse?) class and type, they are also multi-typed in that an object's types are its class and all of its class's superclasses. So, was that all wrong? Chris