Self: the Power of Simplicity David Ungar and Randall B
Total Page:16
File Type:pdf, Size:1020Kb
Self: The Power of Simplicity David Ungar and Randall B. Smith David Ungar Randall B. Smith CIS, Room 209 Xerox Palo Alto ResearchCenter Stanford University 3333 Coyote Hill Rd. Stanford, CA 94305 Palo Alto, CA 94304 (415) 725-3713 (415) 494-4947 [email protected] [email protected] To thine own self be true. supportexploratory programming Me831, and there- -William Shakespeare fore includes runtime typing (i.e. no type declarations) and automatic storage reclamation. But Abstract unlike Smalltalk, Self includes neither classes nor variables. Instead, Self has adopted a prototype Self is a new object-oriented language for explor- metaphor for object creation EBor79, Bor81 Bor86, atory programming based on a small number of Lie86, LTP86]. Furthermore, while Smalltalk and simple and concrete ideas: prototypes, slots, and most other object-oriented languages support vari- behavior. Prototypes combine inheritance and instan- able access as well as message passing, Self objects tiation to provide a framework that is simpler and access their state information by sending messagesto more flexible than most object-oriented languages. L‘self,” the receiver of the current message. Slots unite variables and procedures into a single con- Naturally this results in many messages sent to struct. This permits the inheritance hierarchy to take “SeIf,” and the language is named in honor of these over the function of lexical scoping in conventional messages. One of strengths of object-oriented pro- languages. Finally, because Self does not distinguish gramming lies in the uniform access to different state from behavior, it narrows the gaps between kinds of stored and computed data, and the ideas in ordinary objects, procedures, and closures. Self”s Self result in even more uniformity, which results in simplicity and expressivenessoffer new insights into greater expressivepower. We believe that these object-oriented computation. ideas offer a new and useful view of object-oriented computation. Introduction Object-oriented programming languages are gaining Severalprincipals have guided the design of Self: acceptance,partly because they offer a useful perspec- tive for designing computer programs. However, Messages-at-the-bottom. Self features message they do not all offer exactly the same perspective; passing as the fundamental operation, providing there are many different ideas about the nature of access to stored state solely via messages. There are object-oriented computation. In this paper we pre- no variables, merely slots containing objects that sent Self, a programming language with a new return themselves. Since Self objects access state perspective on objects and messagepassing. Like the solely by sending messages,message passing is more Smalltalk-80* language [GoR83], Self is designed to fundamental to Self than to languageswith variables. *Smalltalk-gO is a trademark of ParcPlaceSystems. In this paper, the term “Smalltalk” will be used to refer to the Occam’s razor. Throughout the design, we have SmaUtalk-gO programming language. aimed for conceptual economy: Permission to copy without fee all or part of this material is granted provided . As described above, Self’s design omits that the copies are not made or distributed for direct commerical advantage, classes and variables. Any object can per- the ACM copyright notice and the title of the publication and its date appear, form the role of an instance or serve as a and notice is given that copying is by permission of the Association for repository of sharedinformation. Computing Machinery. To copy otherwise, or to republish, requires a fee and/ or specific permission. l There is no distinction between accessing a 0 1987 ACM 0-89791-247-O/87/0010-0227 $1.50 variable and sending a message. October443,1987 OOPStA ‘87 Proceedings 227 class-based systems Self: no classes inheritance relationships instance of inherits from I I subclass of I 1 creation metaphor 1 build according to plan 1 clone an object initialization executing a “plan” cloning an example on-f-a-kind need extra object for class no extra object needed infinite regress class of class of class of . none required As in Smalhalk, the language kernel has no which may store either state or behavior. If an control structures. Instead, closures and object receives a messageand it has no matching slot, polymorphism support arbitrary control the search continues via its parent pointer. This is structures within the language. how Self implements inheritance. Inheritance in Self allows objects to share behavior, which in turn Unlike Smalltalk, Self objects, procedures, allows the programmer to alter the behavior of many and closures are all woven from the same objects with a single change. For instance as shown yarn by representing procedures and closures in Figure 1, a point* object would have slots for its as prototypes of activation records. This non-shared characteristics: x and y. Its parent would technique allows activation records to be be an object that held the behavior shared among all created the same way as other objects, by points: +, -, etc. cloning prototypes. In addition to sharing the same model of creation, procedures and closures also store their variables and main- Comparing Prototypes and Classes tain their environment information the same way as ordinary objects, as described below. One of .Sers most interesting aspects is the way it combines inheritance, prototypes, and object creation, Concreteness. Our tastes have led us to a metaphor eliminating the need for classes. whose elements are as concrete as possible [Smi87]. So, in the matter of classes versus prototypes, we Simpler relationships. Prototypes can simplify the have chosen to try prototypes. This makes a basic dif- relationships between objects. To visualiz.e the way ference in the way that new objects are created. In a objects behave in a class-based language, one must class-based language an object would be created by grasp two relationships: the “is a” relationship, that instuntiuting a plan in its class. In a prototypebased indicates that an object is an instance of some class, language like Self, an object would be created by and the “kind of * relationship, that indicates that an cloning (copying) a prototype. In Self, any object object’s class is a subclass of some other object’s can be cloned. class. In a system with prototypes instead of classes such as Self, there is only one relationship, “inherits The remainder of the paper describes Self in more from”, that describes how objects share behavior and detail, and concludes with an example. We use State. This structural simplification makes it easier Smalltalk as our yardstick, as it is the most widely to understand the language and easier to formulate an known language in which everything is an object. inheritance hierarchy. Familiarity with Smalltalk will therefore be helpful to the reader. A working system will provide-the chance to discov- er whether class-like objects would be so useful that Prototypes: Blending Classesand programmers will create them without encourage- Instances ment from the language. The absence of the class-instance distinction may make it too hard to understand which objects exist solely to provide In Smalltalk, unlike C++, Simula, Loops, or ADA, shared information for other objects. Perhaps Self everything is an object and every object contains a pointer to its class, an object that describes its format and holds its behavior. (See Figure 1.) In * Throughout this paper we appeal to point objects in exam- ples. A Smalltalk pint represents a point in Self too, everything is an object. But, instead of a two-dimensional Cartesian coordinates. It contains two class pointer, a Self object contains named slots instance variables, an x anda y coordinate. 228 OOPSLA ‘87 Proceedings October 4-8,1987 Smalltalk instances and classes Self objects print how to 1print 1 prir?!b;&ts ( * print objects A (class) (name) Object (superclass) nil (inst vars) I (methods) parent I x: t (class) ... Y: t (name) Point + (superclass) FTJk I (inst vars) Iclass,x, Y 1 I i (methods) L-A-J (class) parent parent \ w 3 X 3 X 7 (Y)j 5 Y 5 Y 9 Each Smalltalk point contains a class pointer, x Each Self point intrinsically describes its own for- and y coordinates. The class Point supplies both mat, but appeals to another object for any behavior format and behavior information for points. Addi- that is shared among points. In this example, the tional format and behavior information is points appeal to an object containing shared behavior inherited from Object via Point’s superclass link. for points. That object in turn appeals to another Each of the two classes in turn must appeal to (on top) for behavior that is shared by all objects. other classes (not shown) for their format and The “root” object fully describes its own format behavior. and behavior, so it has no parent. L A Figure 1. A comparison of Smalltalk instances and classes with Self objects: At the bottom of each figure are two point objects that have been createdby a user program. Odober 4-8,1937 OOPSIA ‘87 Proceedings 229 programmers will create entirely new organizational between computers and the real word, the ability to structures. In any case, Self’s flexibility poses a chal- make sweeping changes by changing shared behavior. lenge to the programming environment; it will have Once inheritance is introduced into the language, the to include navigational and descriptive aids. natural tendency is to make the prototype the same object that contains the behavior for that kind of Creation by copying. Creating new objects from object. For instance, the behavior of all points could prototypes is accomplished by a simple operation, be changed by changing the behavior of the prototypi- with a simple biological metaphor, copying, cal point. Unfortunately, such a system must supply cloning.