_E_x_p_l_o_r_i_n_g _a_n_d _C_r_e_a_t_i_n_g This document describes how to discover information about existing objects and create new objects using the Unix interface to the Little Smalltalk system (version three). The Little Smalltalk system running under different operating systems may have a slightly different interface, and the reader should be forewarned. When you start version three Little Smalltalk under Unix, you will be given a prompt. You can enter expressions in response to the prompt, and the system will evaluate them > 5 + 7 12 > In Smalltalk one communicates with objects by passing mes- sages to them. Even the addition sign shown above is treated as a message passed to the object 5, with argument 7. Other messages can be used to discover information about various objects. The most basic fact you can discover about an object is its class. This is given by the message ccccllllaaaassssssss, as in the following examples: > 7 class Integer > nil class UndefinedObject Occasionally, especially when programming, one would like to ask whether the class of an object matches some known class. One way to do this would be to use the message ========, which tells whether two expressions represent the same object: > 7 class == Integer True > nil class == Object False (Notice that second example uses cascades in place of parenthesis. The only difference between these two is that May 7, 1992 - 2 - in the first example the result of the expression is the value returned, whereas in the second the result of the expression is the value returned by ==. But since in any case the value is thrown away, it makes no difference.) An easier way is to use the message iiiissssMMMMeeeemmmmbbbbeeeerrrrOOOOffff::::; > 7 isMemberOf: Integer True > nil isMemberOf: Integer False Sometimes you want to know if an object is an instance of a particular class or one if its subclasses; in this case the appropriate message is iiiissssKKKKiiiinnnnddddOOOOffff::::. > 7 isMemberOf: Number False > 7 isKindOf: Number True All objects will respond to the message ddddiiiissssppppllllaaaayyyy by tel- ling a little about themselves. Many just give their class and their printable representation: > 7 display (Class Integer) 7 > nil display (Class UndefinedObject) nil Others, such as classes, are a little more verbose: > Integer display Class Name: Integer SuperClass: Number Instance Variables: no instance variables Subclasses: The display shows that the class IIIInnnntttteeeeggggeeeerrrr is a subclass of class NNNNuuuummmmbbbbeeeerrrr (that is, class NNNNuuuummmmbbbbeeeerrrr is the superclass of IIIInnnntttteeeeggggeeeerrrr). There are no instance variables for this class, and it currently has no subclasses. All of this information could be obtained by means of other messages, although the ddddiiiissssppppllllaaaayyyy form is the easiest. May 7, 1992 - 3 - > List variables display links > Integer superClass Number > Collection subClasses display IndexedCollection Interval List About the only bit of information that is not provided when one passes the message ddddiiiissssppppllllaaaayyyy to a class is a list of methods the class responds to. There are two reasons for this omission; the first is that this list can often be quite long, and we don't want to scroll the other informa- tion off the screen before the user has seen it. The second reason is that there are really two different questions the user could be asking. The first is what methods are actu- ally implemented in a given class. A dictionary containing the set of methods implemented in a class can be found by passing the message mmmmeeeetttthhhhooooddddssss to a class. Since we are only interested in the set of keys for this dictionary (that is, the message selectors), we can use the message kkkkeeeeyyyyssss. Finally, as we saw with the message ssssuuuubbbbCCCCllllaaaasssssssseeeessss shown above, our old friend ddddiiiissssppppllllaaaayyyy prints this information out one method to a line: > True methods keys display #ifTrue:ifFalse: #not A second question that one could ask is what message selectors an instance of a given class will respond to, whether they are inherited from superclasses or are defined in the given class. This set is given in response to the message rrrreeeessssppppoooonnnnddddssssTTTToooo. May 7, 1992 - 4 - > True respondsTo display #class #== #hash #isNil #display #= #basicSize #isMemberOf: #notNil #print #basicAt:put: #isKindOf: #basicAt: #printString #or: #and: #ifFalse:ifTrue: #ifTrue: #ifFalse: #not #ifTrue:ifFalse: Alternatively, one can ask whether instances of a given class will respond to a specific message by writing the mes- sage selector as a symbol: > String respondsTo: #print True > String respondsTo: #+ False The inverse of this would be to ask what classes con- tain methods for a given message selector. Class SSSSyyyymmmmbbbboooollll defines a method to yield just this information: > #+ respondsTo display Integer Number Float The method that will be executed in response to a given message selector can be displayed by means of the message vvvviiiieeeewwwwMMMMeeeetttthhhhoooodddd:::: May 7, 1992 - 5 - > Integer viewMethod: #gcd: gcd: value (value = 0) ifTrue: [ |^ self ]. (self negative) ifTrue: [ |^ self negated gcd: value ]. (value negative) ifTrue: [ |^ self gcd: value negated ]. (value > self) ifTrue: [ |^ value gcd: self ]. |^ value gcd: (self rem: value) New functionality can be added using the message aaaaddddddddMMMMeeeetttthhhhoooodddd. When passed to an instance of CCCCllllaaaassssssss, this mes- sage drops the user into a standard Unix Editor. A body for a new method can then be entered. When the user exists the editor, the method body is compiled. If it is syntactically correct, it is added to the methods for the class. If it is incorrect, the user is given the option of re-editing the method. > Integer addMethod ... drop into editor and enter the following text % x |^ ( x + ) ... exit editor compiler error: invalid expression start ) edit again (yn) ? ... In a similar manner, existing methods can be editing by passing their selectors, as symbols to the message eeeeddddiiiitttt---- MMMMeeeetttthhhhoooodddd::::. > Integer editMethod: #gcd: ... drop into editor working on the body of gcd: The name of the editor used by these methods is taken from a string pointed to by the global variable _e_d_i_t_o_r. Different editors can be selected merely by redefining this value: globalNames at: #editor put: 'emacs' Some Smalltalk systems make it very difficult for you to discover the bytecodes that a method gets translated into. Since the primary goal of Little Smalltalk is to help the student to discover how a modern very high leval language is implemented, it makes sense that the system should help you as much as possible discover everything about its internal structure. Thus a method, when presented with the message ddddiiiissssppppllllaaaayyyy, will print out its bytecode representation. May 7, 1992 - 6 - > Char methods at: #isAlphabetic ; display Method #isAlphabetic isAlphabetic ^ (self isLowercase) or: [ self isUppercase ] literals Array ( #isLowercase #isUppercase ) bytecodes 32 2 0 144 9 0 0 0 0 250 15 10 8 0 8 32 2 0 144 9 0 1 0 1 242 15 2 241 15 1 Bytecodes are represented by four bit opcodes and four bit operands, with occasional bytes representing data (more detail can be found in the book). The three numbers written on each line for the bytecodes represent the byte value fol- lowed by the upper four bits and the lower four bits. New objects are created using the message nnnneeeewwww. Within a method these can be assigned to instance varibles using the assignment arrow. aaaaMMMMeeeetttthhhhoooodddd x <- Set new. ... The assignment arrow is not recognized at the topmost level. Instead, global variables (variables recognized in any context), are created by passing messages to gggglllloooobbbbaaaallllNNNNaaaammmmeeeessss (below). New classes, on the other hand, are created by sending a message aaaaddddddddSSSSuuuubbbbCCCCllllaaaassssssss to the class that will be the super- class of the new class. The user will then be interrogated for information to be associated with the new class: May 7, 1992 - 7 - > Object addSubClass Class Name? Foo Instance Variables? x y z Add a method (yn) ? y ... > Foo display Class Name: Foo Superclass: Object Instance Variables: x y z Subclasses: Classes created using aaaaddddddddSSSSuuuubbbbCCCCllllaaaassssssss will be automatically added to the list of global variables. Other global vari- ables can be created merely by assigning an object to them: > version <- 3.0 > version 3.0 If you have written a new class and want to print the class methods on a file you can use the message ffffiiiilllleeeeOOOOuuuutttt::::, after first creating a file to write to. Both classes and individual methods can be filed out, and several classes and/or methods can be placed in one file. > globalNames at: #f put: File new > f name: 'foo.st' > f open: 'w' > Foo fileOut: f > Bar fileOut: f > Object fileOutMethod: #isFoo to: f > f close To simply save a class 'Foo' in a file 'Foo.st', at CSUSB use > Foo fileOut which creates or overwrites file 'Foo.st' with a loadable description of the class. May 7, 1992 - 8 - The file ``newfile'' will now have a printable representa- tion of the methods for the class Foo. These can subse- quently be filed back into a different smalltalk image. > globalNames at: #f put: File new > f name: 'foo.st' > f open: 'r' > f fileIn > f close At CSUSB this is shortened to: > 'foo.st' load Finally, once the user has added classes and variables and made whatever other changes they want, the message ssssaaaavvvveeeeIIIImmmmaaaaggggeeee, passed to the pseudo variable ssssmmmmaaaallllllllttttaaaallllkkkk, can be used to save an entire object image on a file. If the writ- ing of the image is successful, a message will be displayed. > smalltalk saveImage Image name? newimage image newimage created > Typing control-D causes the interpreter to exit. When the smalltalk system is restarted, an alternative image, such as the image just created, can be specified by giving its name on the argument line: st newimage Further information on Little Smalltalk can be found in Budd's book.