home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 116.lha / SmallTalk / Manual / EXPLORE.MS < prev    next >
Encoding:
Text File  |  1986-11-20  |  13.3 KB  |  426 lines

  1. .SH
  2. Exploring and Creating
  3. .PP
  4. This document describes how to discover information about existing objects 
  5. and create new objects using the Unix interface to the Little Smalltalk
  6. system (version two).  The Little Smalltalk system running 
  7. under different operating
  8. systems may have a slightly different interface, and the reader should be
  9. forewarned.
  10. .PP
  11. When you start version two Little Smalltalk under Unix, you will be given a 
  12. prompt.
  13. You can enter expressions in response to the prompt, and the system will 
  14. evaluate them (although it will not print the result unless you request 
  15. it\s-2\u*\d\s+2).
  16. .FS
  17. * Note that this is a change from version one of Little Smalltalk, where
  18. expressions were automatically printed.
  19. The reason has to do with now expressions are compiled and executed
  20. now, using more Smalltalk code, and less C code.
  21. .FE
  22. .DS I
  23. >    (5 + 7) print
  24. 12
  25. >
  26. .DE
  27. In Smalltalk one communicates with objects by passing messages to them.
  28. Even the addition sign shown above is treated as a message passed to the
  29. object 5, with argument 7.  Other messages can be used to discover
  30. information about various objects.
  31. The most basic fact you can discover about an object is its class.
  32. This is given by the message \fBclass\fP, as in the following examples:
  33. .DS I
  34. >    7 class print
  35. Integer
  36. >    nil class print
  37. UndefinedObject
  38. .DE
  39. .PP
  40. Occasionally, especially when programming, one would like to ask whether
  41. the class of an object matches some known class.  One way to do this would
  42. be to use the message \fB=\!=\fP, which tells whether two expressions 
  43. represent the same object:
  44. .DS I
  45. >    ( 7 class =\!= Integer) print
  46. True
  47. >    nil class == Object ; print
  48. False
  49. .DE
  50. .LP
  51. (Notice that second example uses cascades in place of parenthesis.
  52. The only difference between these two is that in the first example the
  53. result of the expression is the value returned by the print, whereas in the
  54. second the result of the expression is the value returned by =\!=.  But
  55. since in any case the value is thrown away, it makes no difference.)
  56. .PP
  57. An easier way is to use the message \fBisMemberOf:\fP;
  58. .DS I
  59. >    7 isMemberOf: Integer ; print
  60. True
  61. >    nil isMemberOf: Integer ; print
  62. False
  63. .DE
  64. .PP
  65. Sometimes you want to know if an object is an instance of a particular
  66. class or one if its subclasses; in this case the appropriate message is
  67. \fBisKindOf:\fP.
  68. .DS I
  69. >    7 isMemberOf: Number ; print
  70. False
  71. >    7 isKindOf: Number ; print
  72. True
  73. .DE
  74. .PP
  75. All objects will respond to the message \fBdisplay\fP by telling a little
  76. about themselves.  Many just give their class and their printable
  77. representation:
  78. .DS I
  79. >    7 display
  80. (Class Integer) 7
  81. >    nil display
  82. (Class UndefinedObject) nil
  83. .DE
  84. .LP
  85. Others, such as classes, are a little more verbose:
  86. .DS I
  87. >    Integer display
  88. Class Name: Integer
  89. SuperClass: Number
  90. Instance Variables:
  91. no instance variables
  92. Subclasses:
  93. .DE
  94. .LP
  95. The display shows that the class \fBInteger\fP is a subclass of class
  96. \fBNumber\fP (that is, class \fBNumber\fP is the superclass of
  97. \fBInteger\fP).  There are no instance variables for this class, and it
  98. currently has no subclasses.  
  99. All of this information could be obtained by means of other messages,
  100. although the \fBdisplay\fP form is the easiest.
  101. .DS I
  102. >    List variables display
  103. links
  104. >    Integer superClass print
  105. Number
  106. >    Collection subClasses display
  107. IndexedCollection
  108. Interval
  109. List
  110. .DE
  111. About the only bit of information that is not provided when one passes the
  112. message \fBdisplay\fP to a class
  113. is a list of methods the class responds to.  There are two
  114. reasons for this omission; the first is that this list can often be quite
  115. long, and we don't want to scroll the other information off the screen
  116. before the user has seen it.  The second reason is that there are really
  117. two different questions the user could be asking.  The first is what
  118. methods are actually implemented in a given class.  A dictionary containing
  119. the set of methods implemented in a class can be found by passing the
  120. message \fBmethods\fP to a class.  Since we are only interested in the set
  121. of keys for this dictionary (that is, the message selectors), we can use
  122. the message \fBkeys\fP.  Finally, as we saw with the message
  123. \fBsubClasses\fP shown above, our old friend \fBdisplay\fP prints this
  124. information out one method to a line:
  125. .DS I
  126. >    True methods keys display
  127. #ifTrue:ifFalse:
  128. #not
  129. .DE
  130. .PP
  131. A second question that one could ask is what message selectors an instance of a
  132. given class will respond to, whether they are inherited from superclasses
  133. or are defined in the given class.  This set is given in response to the
  134. message \fBrespondsTo\fP.
  135. .DS I
  136. >    True respondsTo display
  137. #class
  138. #==
  139. #hash
  140. #isNil
  141. #display
  142. #=
  143. #basicSize
  144. #isMemberOf:
  145. #notNil
  146. #print
  147. #basicAt:put:
  148. #isKindOf:
  149. #basicAt:
  150. #printString
  151. #or:
  152. #and:
  153. #ifFalse:ifTrue:
  154. #ifTrue:
  155. #ifFalse:
  156. #not
  157. #ifTrue:ifFalse:
  158. .DE
  159. .PP
  160. Alternatively, one can ask whether instances of a given class will respond
  161. to a specific message by writing the message selector as a symbol:
  162. .DS I
  163. >    ( String respondsTo: #print ) print
  164. True
  165. >    String respondsTo: #+ ; print
  166. False
  167. .DE
  168. .PP
  169. The inverse of this would be to ask what classes contain methods for a
  170. given message selector.  Class \fBSymbol\fP defines a method to yield just
  171. this information:
  172. .DS I
  173. >    #+ respondsTo display
  174. Integer
  175. Number
  176. Float
  177. .DE
  178. .PP
  179. The method that will be executed in response to a given message selector
  180. can be displayed by means of the message \fBviewMethod:\fP
  181. .DS I
  182. >    Integer viewMethod: #gcd:
  183. gcd: value
  184.     (value = 0) ifTrue: [ \(ua self ].
  185.     (self negative) ifTrue: [ \(ua self negated gcd: value ].
  186.     (value negative) ifTrue: [ \(ua self gcd: value negated ].
  187.     (value > self) ifTrue: [ \(ua value gcd: self ].
  188.     \(ua value gcd: (self rem: value)
  189. .DE
  190. .PP
  191. New functionality can be added using the message \fBaddMethod\fP.
  192. When passed to an instance of \fBClass\fP, this message drops the user into
  193. a standard Unix Editor.  A body for a new method can then be entered.
  194. When the user exists the editor, the method body is compiled.  If it is
  195. syntactically correct, it is added to the methods for the class.  If it is
  196. incorrect, the user is given the option of re-editing the method.
  197. .DS I
  198. >    Integer addMethod
  199. \& ... drop into editor and enter the following text
  200. % x
  201.     \(ua ( x + )
  202. \& ... exit editor
  203. compiler error: invalid expression start )
  204. edit again (yn) ?
  205. \& ...
  206. .DE
  207. .PP
  208. In a similar manner, existing methods can be editing by passing their
  209. selectors, as symbols to the message \fBeditMethod:\fP.
  210. .DS I
  211. >    Integer editMethod: #gcd:
  212. \& ... drop into editor working on the body of gcd:
  213. .DE
  214. .PP
  215. The name of the editor used by these methods is taken from a string
  216. pointed to by the global variable \fIeditor\fP.  Different editors can be
  217. selected merely by redefining this value:
  218. .DS I
  219. globalNames at: #editor put: 'emacs'
  220. .DE
  221. .PP
  222. Some Smalltalk systems make it very difficult for you to discover the
  223. bytecodes that a method gets translated into.  Since the primary goal of
  224. Little Smalltalk is to help the student to discover how a modern very high
  225. leval language is implemented, it makes sense that the system should help
  226. you as much as possible discover everything about its internal structure.
  227. Thus a method, when presented with the message \fBdisplay\fP, will print
  228. out its bytecode representation.
  229. .DS I
  230. >    Char methods at: #isAlphabetic ; display
  231. Method #isAlphabetic
  232.     isAlphabetic
  233.         ^ (self isLowercase) or: [ self isUppercase ]
  234.  
  235. literals
  236. Array ( #isLowercase #isUppercase )
  237. bytecodes
  238. 32 2 0
  239. 144 9 0
  240. 0 0 0
  241. 250 15 10
  242. 8 0 8
  243. 32 2 0
  244. 144 9 0
  245. 1 0 1
  246. 242 15 2
  247. 241 15 1
  248. .DE
  249. .PP
  250. Bytecodes are represented by four bit opcodes and four bit operands, with
  251. occasional bytes representing data (more detail can be found in the book).
  252. The three numbers written on each line for the bytecodes represent the 
  253. byte value followed by the upper four bits and the lower four bits.
  254. .PP
  255. New objects are created using the message \fBnew\fP.  
  256. Within a method
  257. these can be assigned to instance varibles using the assignment arrow.
  258. .DS I
  259. \fBaMethod\fP
  260.     x \(<- Set new.
  261.     \&...
  262. .DE
  263. .PP
  264. The assignment arrow is not recognized at the topmost level.  Instead,
  265. global variables (variables recognized in any context), are created by
  266. passing messages to \fBglobalNames\fP (below).
  267. .PP
  268. New classes, on the
  269. other hand, are created by sending a message \fBaddSubClass\fP to the class
  270. that will be the superclass of the new class.  The user will then be
  271. interrogated for information to be associated with the new class:
  272. .DS I
  273. >    Object addSubClass
  274. Class Name? Foo
  275. Instance Variables? x y z
  276. Add a method (yn) ? y
  277. \&...
  278. >    Foo display
  279. Class Name: Foo
  280. Superclass: Object
  281. Instance Variables: 
  282. x
  283. y
  284. z
  285. Subclasses:
  286. .DE
  287. .PP
  288. Classes created using \fBaddSubClass\fP will be automatically added to the
  289. list of global variables.  Other global variables can be created merely by
  290. placing their name and value into the 
  291. dictionary \fBglobalNames\fP\s-2\u*\d\s+2.
  292. .DS I
  293. >    globalNames at: #version put: 2.1
  294.  
  295. >    version print
  296. 2.1
  297. .DE
  298. .FS
  299. * This is a change from version 1 of Little Smalltalk, where it was
  300. possible to create global variables merely by assiging a value to them at
  301. the command level.  The change is an unfortunate consequence of the
  302. fact that more is done now
  303. is Smalltalk, and less in C.  The bytecode interpreter now knows little
  304. about the object globalNames, in particular, the bytecode interpreter
  305. doesn't know how to add a new object; this is done entirely in Smalltalk
  306. code.  One possiblity would be to automatically have the parser change an
  307. assignment at the command level into an at:put:, but this would seem to
  308. complicate the parser unnecessarily.
  309. .FE
  310. .PP
  311. If you have written a new class and want to print the class methods on a
  312. file you can use the message \fBfileOut:\fP, after first creating a file to
  313. write to.  Both classes and individual methods can be filed out, and
  314. several classes and/or methods can be placed in one file.
  315. .DS I
  316. >    globalNames at: #f put: File new
  317. >    f name: 'foo.st'
  318. >    f open: 'w'
  319. >    Foo fileOut: f
  320. >    Bar fileOut: f
  321. >    Object fileOutMethod: #isFoo to: f
  322. >    f close
  323. .DE
  324. .LP
  325. The file ``newfile'' will now have a printable representation of the
  326. methods for the class Foo.
  327. These can subsequently be filed back into a different smalltalk image.
  328. .DS I
  329. >    globalNames at: #f put: File new
  330. >    f name: 'foo.st'
  331. >    f open: 'r'
  332. >    f fileIn
  333. >    2 isFoo print
  334. False
  335. .DE
  336. .PP
  337. Finally, once the user has added classes and variables and made whatever other
  338. changes they want, the message \fBsaveImage\fP, passed to the pseudo
  339. variable \fBsmalltalk\fP, can be used to save an entire object image on a file.
  340. If the writing of the image is successful, a message will be displayed.
  341. .DS I
  342. >    smalltalk saveImage
  343. Image name? newimage
  344. image newimage created
  345. >    
  346. .DE
  347. .PP
  348. Typing control-D causes the interpreter to exit.
  349. .PP
  350. When the smalltalk system is restarted, an alternative image, such as the
  351. image just created, can be specified by giving its name on the argument
  352. line:
  353. .DS I
  354. st newimage
  355. .DE
  356. .PP
  357. Further information on Little Smalltalk can be found in the book.
  358. .SH
  359. Incompatabilities with the Book
  360. .PP
  361. It is unfortunately the case that during the transition from version 1 (the
  362. version described in the book) and version 2 (the new version that is one
  363. third the size and three times faster), certain changes to the user
  364. interface were required.  I will describe these here.
  365. .PP
  366. The first incompatability comes at the very beginning.  In version 1 there
  367. were a great number of command line options.  These have all been
  368. eliminated in version two.  In version two the only command line option is
  369. the file name of an image file.
  370. .PP
  371. In version 1 it is possible to create global variables simply by assigning
  372. to them.  That is, a statement such as
  373. .DS I
  374. xx \(<- 27
  375. .DE
  376. when issued at the command level would create a new global variable.
  377. Since it is not possible to assign to an unknown name within a method, this
  378. in effect required the version one system to keep around two parsers, one
  379. for methods and another for command lines.  These were replaced with a
  380. single parser in version two, which necessitated a change.  Now to create a
  381. global variable one must first establish it in the dictionary, using the
  382. command 
  383. .DS I
  384. globalNames at: #xx put: 27
  385. .DE
  386. It is not possible to use assignment to create a global variable in version
  387. two.
  388. .PP
  389. The interface to the editor has been changed.  In version one this was
  390. handled by the system, and not by Smalltalk code.  This required a command
  391. format that was clearly not a Smalltalk command, so that they could be
  392. distinguished.  The convention adoped was to use an APL style system
  393. command:
  394. .DS I
  395. )e filename
  396. .DE
  397. In version two we have moved these functions into Smalltalk code.  Now
  398. the problem is just the reverse, we need a command that is a Smalltalk
  399. command.  In addition, in version one entire classes were edited at once,
  400. whereas in version two only individual methods are edited.  As we have
  401. already noted, the new commands to add or edit methods are as follows:
  402. .DS I
  403. \fIclassname\fP addMethod
  404. \fIclassname\fP editMethod: \fImethodname\fP
  405. .DE
  406. .PP
  407. The only other significant syntactic change is the way primitive methods
  408. are invoked.  In version one these were either named or numbered, 
  409. something like the following:
  410. .DS I
  411. <primitive 37 a b>
  412. <IntegerAdd a b>
  413. .DE
  414. In version two we have simply eliminated the keyword \fBprimitive\fP, so
  415. primitives now look like:
  416. .DS I
  417. <37 a b>
  418. .DE
  419. .PP
  420. There are far fewer primitives in version two, and much more of the system
  421. is now performed using Smalltalk code.
  422. .PP
  423. In addition to these syntactic changes, there are various small changes in
  424. the class structure.  I hope to have a document describing these changes at
  425. some point, but as of right now the code itself is the best description.
  426.