|
| 1 | +% $Author$ |
| 2 | +% $Date$ |
| 3 | +% $Revision$ |
| 4 | +%================================================================= |
| 5 | +\ifx\wholebook\relax\else |
| 6 | +% -------------------------------------------- |
| 7 | +% Lulu: |
| 8 | + \documentclass[a4paper,10pt,twoside]{book} |
| 9 | + \usepackage[ |
| 10 | + papersize={6in,9in}, |
| 11 | + hmargin={.75in,.75in}, |
| 12 | + vmargin={.75in,1in}, |
| 13 | + ignoreheadfoot |
| 14 | + ]{geometry} |
| 15 | + \input{../common.tex} |
| 16 | + \pagestyle{headings} |
| 17 | + \setboolean{lulu}{true} |
| 18 | +% -------------------------------------------- |
| 19 | +% A4: |
| 20 | +% \documentclass[a4paper,11pt,twoside]{book} |
| 21 | +% \input{../common.tex} |
| 22 | +% \usepackage{a4wide} |
| 23 | +% -------------------------------------------- |
| 24 | + \graphicspath{{figures/} {../figures/}} |
| 25 | + \begin{document} |
| 26 | + \renewcommand{\nnbb}[2]{} % Disable editorial comments |
| 27 | + \sloppy |
| 28 | +\fi |
| 29 | +%================================================================= |
| 30 | + |
| 31 | +%----------------------------------------------------------------- |
| 32 | +\subsection{Associating} |
| 33 | +\sd{I would remove that section too} |
| 34 | +The \emph{associating} method category contains a single method \ct{->}, which is used to create an \ct{Association} between a key (the receiver) and a value (the argument). An \ct{Array} of \ct{Associations} can be used to conveniently initialize a \ct{Dictionary}: |
| 35 | + |
| 36 | +\begin{code}{@TEST} |
| 37 | +Dictionary newFrom: { #start -> (1@1) . #end -> (3@4) } --> a Dictionary(#end->3@4 #start->1@1 ) |
| 38 | +\end{code} |
| 39 | + |
| 40 | +\ct{Object>>>-->} is an example of a \emph{Shortcut Constructor Method}\footnote{See: Kent Beck, \emph{Smalltalk Best Practice Patterns}, Prentice-Hall, 1997.}, which allows you to construct an object by sending a message to one of its constructor arguments. Shortcut Constructor Methods have the great advantage that they make object creation much more concise, but if abused they can clutter the receiver class with many methods unrelated to the key responsibilities of the class. |
| 41 | + |
| 42 | +%----------------------------------------------------------------- |
| 43 | +\subsection{Converting} |
| 44 | + |
| 45 | +\sd{here} |
| 46 | +\sd{I do not like to talk about that here. I would remove that section. Add a note on the asString and printString difference} |
| 47 | +Whenever it is possible in Smalltalk to convert an object from one class \ct{A} to be an instance of another class \ct{B}, this is usually accomplished by sending it a message of the form \ct{asB}. Of course, there is no magic, so this must be implemented at the appropriate level. There are a few such messages provided already by the class \ct{Object}. The most important are \ct{asString} and \ct{asOrderedCollection}. |
| 48 | + |
| 49 | +Every object should have a \ct{String} representation. The default implementation is to return the \ct{printString} representation of the object. We will see this in more detail when we look at \ct{Object}'s \emph{printing} method category below. |
| 50 | + |
| 51 | +\begin{code}{} |
| 52 | +Object>>>asString |
| 53 | + "Answer a string that represents the receiver." |
| 54 | + ^ self printString |
| 55 | +\end{code} |
| 56 | + |
| 57 | +\begin{code}{@TEST} |
| 58 | +Object new --> an Object |
| 59 | +\end{code} |
| 60 | + |
| 61 | +The other standard conversion method is \ct{asOrderedCollection}: |
| 62 | + |
| 63 | +\begin{code}{} |
| 64 | +Object>>>asOrderedCollection |
| 65 | + "Answer an OrderedCollection with the receiver as its only element." |
| 66 | + ^ OrderedCollection with: self |
| 67 | +\end{code} |
| 68 | + |
| 69 | +\begin{code}{@TEST} |
| 70 | +1 asOrderedCollection --> an OrderedCollection(1) |
| 71 | +\end{code} |
| 72 | + |
| 73 | + |
| 74 | +%================================================================= |
| 75 | +\ifx\wholebook\relax\else |
| 76 | +\end{document}\fi |
| 77 | +%================================================================= |
0 commit comments