3.3 Objects
In Dylan, everything is an object. Characters, strings, numbers, arrays, and vectors are all objects. The canonical true and false values, #t, and #f, are objects. Methods, generic functions, and classes are objects. What does it mean to be an object?
Most important, an object has a unique identity. You can use the == predicate to test whether two operands are the same object. See Section 3.3.1.
An object is a direct instance of a particular class. You can use the object-class predicate to determine the direct class of an object.
You can give an object a name. For example, if you define a variable or constant to contain an object, you have given that object a name. See Section 3.3.2.
You can pass an object as an argument or return value — because generic functions and methods are objects, you can manipulate them just as you can any other object. See Section 12.3, page 180.
Comparison to C++ and Smalltalk: In Dylan and Smalltalk, everything is an object (an instance of a class); we say that Dylan and Smalltalk have "objects all the way down." In contrast, in C++, some values are not objects; they have primitive types that are not classes. For example, in Dylan, 7 is an instance of <integer>. In C++, 7 is not an instance; it has the type int. This design decision enables Dylan users to define methods on built-in classes in the same way that they define methods on user-defined classes — a technique that cannot be done in C++.
|
Comparison to Java: Java recognizes the need for object representation of all classes with the Number class and its subclasses. However, Java still requires the programmer to work with nonobjects when writing mathematical statements. The Number classes can be used to "wrap" an object cloak around the primitive integer, float, and other numeric types, to allow object-based programming. Dylan does not separate the mathematical manipulation of numbers from their other object properties. Programmers need only to think in terms of numerical objects, and can rely on the compiler to implement mathematical operations efficiently. Similarly, the Boolean class is used to encapsulate primitive boolean values as objects, and programmers must convert back and forth, depending on the context.
|
N Feinberg/S E Keene/R Mathews/P Tucker Withington, DYLAN PROGRAMMING, (c) 1997 Harlequin Inc. Reproduced by permission of Addison-Wesley Longman Publishing Company, Inc. All rights reserved. No further copying, downloading or transmitting of this material is allowed without the prior written permission of the publisher.