From: Mark Wilson Date: 2003-03-24T14:56:31+09:00 Subject: Types (was Re: Ruby lecture slides (was Strong advantages over Python)) On Sunday, March 23, 2003, at 09:14 PM, Greg McIntyre wrote: > Mark Wilson wrote: >> Slide 3, bullet point 1: Ruby is strongly typed (all objects have a >> type) and dynamically typed (type is determined at run time). > > Hrm... let's ask the trusty old FOLDOC... > > http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?strongly+typed > http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?weakly+typed > > It seems Ruby is neither, although there is no definition for "loosely > typed" which I may have invented. :\ > > Perhaps this is a matter of whose definition you use. > > [snip] It's beyond the scope of the excellent slides, but here goes my $0.02 on types: Ruby has types. In Ruby, they are generally called classes. Conceptually, there should not be a distinction between type and class and this is certainly true of Ruby. A type is "a set of values from which a variable, constant, function, or other expression may take its value." In the case of Ruby, only objects have type and all objects have type. This is because every "thing" in Ruby is an object. Ruby includes built-in types and a user-defined types. User-defined types can extend or constrain built-in types and can be entirely different than built-in types. Note that, in Ruby, a class defines a set of values that an object may take. In Ruby, all type rules are strictly enforced with no exceptions at the time that a method is applied to an object. While this does not appear to be "strong" typing according to the foldoc definition, it is certainly stronger typing than would be the case if type rules are not strictly enforced with no exceptions. As you noted, Ruby certainly does not meet the definition of a weakly typed language either. I don't know about how Ruby handles the abstract syntax tree created by the interpreter when a program is run to know for sure whether type rules are enforced at Ruby's analog to compile time (creation of the abstract syntax tree). As noted in the foldoc definition of strong typing, "with variables that can store values of more than one type, incorrect type usage can be detected at run time." This is the principal difference between the treatment of types in Ruby vs. that in Java. In Ruby, it is impossible to create an object that does not have a valid type, because an object is always an instance of a class -- in other words, a "thing" in the programming language is made by a type generator and, by definition, has only values within the domain of that type. I do think this is the functional equivalent of strong typing, although the definitional issue is obviously of far less importance than what Ruby actually does. Because of the absence of (mandatory) type declaration for variables, it is possible in Ruby to create an object and assign it to a variable and then attempt to apply a method (operator) to that variable that is not available to the type (class) of that object. This generates a run time type error (although it is often called a "method missing" error). For me, this is a better approach because it makes it easier to learn how to write programs. As I understand it (not very well yet) this is also useful because it facilitates writing programs that are "dynamic" (can change during run time) and that still fully implement Ruby's object model. I have heard, though do not really understand, that there are some problems that are best solved with a dynamic program. Any comments would be appreciated.