Tutorial Squeak An OpenSource Smalltalk Environment Oscar Nierstrasz










































![Change Sets 'From Squeak 3. 9 alpha of 4 July 2005 [latest update: #7006] Change Sets 'From Squeak 3. 9 alpha of 4 July 2005 [latest update: #7006]](https://slidetodoc.com/presentation_image_h/ba5d1ea4c63f78ef7f31a84355409b5b/image-43.jpg)

























![Various kinds of Loops |n| n: = 10. [n>0] while. True: [ Transcript show: Various kinds of Loops |n| n: = 10. [n>0] while. True: [ Transcript show:](https://slidetodoc.com/presentation_image_h/ba5d1ea4c63f78ef7f31a84355409b5b/image-69.jpg)





















































- Slides: 122
Tutorial: Squeak — An Open-Source Smalltalk Environment Oscar Nierstrasz scg. unibe. ch TOOLS Europe 2008 Zürich, July 1, 2008
Roadmap > What is Smalltalk? > Getting started > Syntax in a nutshell > Test-driven development > Version control > Standard classes — Object, Number, Boolean … > Common idioms > Debugging practices > Metaclasses in 7 points > Seaside demo © O. Nierstrasz 2
Roadmap > What is Smalltalk? > Getting started > Syntax in a nutshell > Test-driven development > Version control > Standard classes — Object, Number, Boolean … > Common idioms > Debugging practices > Metaclasses in 7 points > Seaside demo © O. Nierstrasz 3
The origins of Smalltalk Alan Kay’s Dynabook project (1968) Alto — Xerox PARC (1973) © O. Nierstrasz 4
What is Smalltalk? Image Virtual machine + Changes © O. Nierstrasz + Sources 5
Smalltalk-80 and Squeak • Everything is an object • Everything is there, all the time • First windowing system with mouse • First graphical IDE Squeak: a modern, portable, fast, opensource Smalltalk © O. Nierstrasz 6
Squeak resources www. squeak. org www. seaside. st Downloads and links One-click image Squeak. By. Example. org Free download — Print-on-demand © O. Nierstrasz 7
Don’t panic! New Smalltalkers often think they need to understand all the details of a thing before they can use it. Try to answer the question “How does this work? ” with “I don’t care”. —Alan Knight. Smalltalk Guru © O. Nierstrasz 8
Roadmap > What is Smalltalk? > Getting started > Syntax in a nutshell > Test-driven development > Version control > Standard classes — Object, Number, Boolean … > Common idioms > Debugging practices > Metaclasses in 7 points > Seaside demo © O. Nierstrasz 9
Everything is an object © O. Nierstrasz 10
Everything happens by sending messages © O. Nierstrasz 11
Running Squeak © O. Nierstrasz 12
Three-button mouse Operate Select Window © O. Nierstrasz 13
World Menu and Open Menu © O. Nierstrasz 14
Standard development tools © O. Nierstrasz 15
Debuggers, Inspectors, Explorers © O. Nierstrasz 16
Do it, Print it, … You can evaluate any expression anywhere in Smalltalk. © O. Nierstrasz 17
Roadmap > What is Smalltalk? > Getting started > Syntax in a nutshell > Test-driven development > Version control > Standard classes — Object, Number, Boolean … > Common idioms > Debugging practices > Metaclasses in 7 points > Seaside demo © O. Nierstrasz 18
Three kinds of messages > Unary messages > Binary messages 5 factorial Transcript cr 3 + 4 > Keyword messages 3 raised. To: 10 modulo: 5 Transcript show: 'hello world' © O. Nierstrasz 19
Precedence First unary, then binary, then keyword: 2 raised. To: 1 + 3 factorial Same as: 128 2 raised. To: (1 + (3 factorial)) Use parentheses to force order: 1 + 2 * 3 1 + (2 * 3) © O. Nierstrasz 9 (!) 7 20
A typical method in the class Point Method name Argument Comment <= a. Point "Answer whether the receiver is neither below nor to the right of a. Point. " ^ x <= a. Point x and: [y <= a. Point y] Return Block Binary message Instance variable Keyword message (2@3) <= (5@6) © O. Nierstrasz true 21
Statements and cascades Temporary variables Statement | p pen | p : = 100@100. pen : = Pen new. pen up. pen goto: p; down; goto: p+p Cascade © O. Nierstrasz 22
Literals and constants Strings & Characters Numbers Symbols Arrays Pseudo-variables Constants © O. Nierstrasz 'hello' $a 1 3. 14159 #yada #(1 2 3) self super true false 23
Method Return > Use a caret to return a value from a method or a block max: a. Number ^ self < a. Number if. True: [a. Number] if. False: [self] 1 max: 2 2 > By default, methods return self © O. Nierstrasz 24
Blocks > Use square brackets to delay evaluation of expressions ^ 1 < 2 if. True: ['smaller'] if. False: ['bigger'] 'smaller' © O. Nierstrasz 25
Variables > Local variables are delimited by |var| Block variables by : var| Ordered. Collection>>collect: a. Block "Evaluate a. Block with each of my elements as the argument. " | new. Collection : = self species new: self size. first. Index to: last. Index do: [ : index | new. Collection add. Last: (a. Block value: (array at: index))]. ^ new. Collection (Ordered. Collection with: 10 with: 5) collect: [: each| each factorial ] an Ordered. Collection(3628800 120) © O. Nierstrasz 26
Control Structures > Every control structure is realized by message sends max: a. Number ^ self < a. Number if. True: [a. Number] if. False: [self] 4 times. Repeat: [Beeper beep] © O. Nierstrasz 27
Creating objects > Class methods Ordered. Collection new Array with: 1 with: 2 > Factory methods 1@2 1/2 © O. Nierstrasz a Point a Fraction 28
Creating classes > Send a message to a class (!) Number subclass: #Complex instance. Variable. Names: 'real imaginary' class. Variable. Names: '' pool. Dictionaries: '' category: 'Complex. Numbers' © O. Nierstrasz 29
Roadmap > What is Smalltalk? > Getting started > Syntax in a nutshell > Test-driven development > Version control > Standard classes — Object, Number, Boolean … > Common idioms > Debugging practices > Metaclasses in 7 points > Seaside demo © O. Nierstrasz 30
Change sets © O. Nierstrasz 31
SUnit © O. Nierstrasz 32
Money > We will implement the Money example in Smalltalk — First, we develop a test case for a single currency Test. Case subclass: #Money. Test instance. Variable. Names: 'chf 2 chf 8 chf 10' class. Variable. Names: '' pool. Dictionaries: '' category: 'Money' NB: This is just a message sent to the Test. Case class object (!) © O. Nierstrasz 33
Set. Up > We will need setters for the private Money state Money. Test>>set. Up chf 2 : = Money new currency: 'CHF'; amount: 2. chf 8 : = Money new currency: 'CHF'; amount: 8. chf 10 : = Money new currency: 'CHF'; amount: 10. © O. Nierstrasz 34
Money. Test>>test. Equals > Some obvious tests Money. Test>>test. Equals self assert: chf 2 = chf 2. self assert: chf 2 = (Money new currency: 'CHF'; amount: 2). self assert: chf 2 ~= chf 8. © O. Nierstrasz 35
Money > We define Money as a subclass of Object, with getters and setters Object subclass: #Money instance. Variable. Names: 'currency amount' class. Variable. Names: '' pool. Dictionaries: '' category: 'Money' Money>>currency: a. String currency : = a. String. Money>>currency ^ currency © O. Nierstrasz 36
Failing tests © O. Nierstrasz 37
Comparisons Money>>= a. Money ^ self currency = a. Money currency and: [ self amount = a. Money amount ] © O. Nierstrasz 38
Constructors Money. Test>>test. Equals self assert: chf 2 = chf 2. self assert: chf 2 = (Money currency: 'CHF' amount: 2). self assert: chf 2 != chf 8. We need a constructor on the class side of Money © O. Nierstrasz 39
Class methods NB: Which “self” is referred to in the method body? © O. Nierstrasz 40
Addition Money. Test>>test. Add self assert: chf 2 + chf 8 = chf 10 Money>>+ a. Money ^ Money currency: self currency amount: self amount + a. Money amount And so on … © O. Nierstrasz 41
Filing out your changes > You can “file out” all your changes so they can be loaded into another image © O. Nierstrasz 42
Change Sets 'From Squeak 3. 9 alpha of 4 July 2005 [latest update: #7006] on 7 March 2006 at 12: 52: 26 pm'! Object subclass: #Money instance. Variable. Names: 'currency amount' class. Variable. Names: '' pool. Dictionaries: '' category: 'Money'! !Money comment. Stamp: 'on 3/7/2006 12: 45' prior: 0! SUnit demo! Test. Case subclass: #Money. Test instance. Variable. Names: 'chf 2 chf 8 chf 10' class. Variable. Names: '' pool. Dictionaries: '' category: 'Money'! !Money. Test comment. Stamp: 'on 3/7/2006 12: 44' prior: 0! SUnit demo! !Money methods. For: 'arithmetic' stamp: 'on 3/7/2006 12: 52'! + a. Money ^ Money currency: self currency amount: self amount + a. Money amount! ! … © O. Nierstrasz 43
Extensions > Creating new instances of Money is still cumbersome — We would like to be able to write instead code like this: Test. Money>>set. Up chf 2 : = 2 chf 8 : = 8 chf 10 : = 10 chf. © O. Nierstrasz 44
Extension protocols > To extend an existing class for a category My. Project, define the method category *My. Project for that class. © O. Nierstrasz 45
Roadmap > What is Smalltalk? > Getting started > Syntax in a nutshell > Test-driven development > Version control > Standard classes — Object, Number, Boolean … > Common idioms > Debugging practices > Metaclasses in 7 points > Seaside demo © O. Nierstrasz 46
Squeak. Source. com © O. Nierstrasz 47
Categories, Projects and Packages > A system category My. Project (and possibly My. Project-*) contains the classes of your application > A Monticello package My. Project contains the categories My. Project and My. Project-* > A Squeak. Source project My. Project stores everything in the Monticello package My. Project © O. Nierstrasz 48
Loading a project > To load a project from Squeak. Source. com — Find the project in Squeak. Source — Open the Monticello Browser — Add an HTTP Repository – Accept the code fragment from the Squeak. Source page — Open the repository and load the latest version © O. Nierstrasz 49
Loading a project © O. Nierstrasz 50
Creating a project > To create a project on Squeak. Source. com — Define your categories My. Project or My. Project-* — Create a Squeak. Source project My. Project — Define a Monticello package My. Project — Add an HTTP Repository for My. Project – Accept the code fragment from the Squeak. Source page — Save the package © O. Nierstrasz 51
Roadmap > What is Smalltalk? > Getting started > Syntax in a nutshell > Test-driven development > Version control > Standard classes — Object, Number, Boolean … > Common idioms > Debugging practices > Metaclasses in 7 points > Seaside demo © O. Nierstrasz 52
Object Defines common behavior for all the objects in the system. © O. Nierstrasz 53
Identity vs. Equality > = tests Object value — Should normally be overridden – Default implementation is == ! — You should override hash too! > == tests Object identity — Should never be overridden 'foo', 'bar' = 'foobar' 'foo', 'bar' == 'foobar' © O. Nierstrasz true false 54
Printing > Override print. On: to give your objects a sensible textual representation Fraction>>print. On: a. Stream next. Put: $(. numerator print. On: a. Stream next. Put: $/. denominator print. On: a. Stream next. Put: $). © O. Nierstrasz 55
Object methods to support the programmer error: a. String Signal an error does. Not. Understand: a. Message Handle unimplemented message halt, halt: a. String Invoke the debugger subclass. Responsibility The sending method is abstract should. Not. Implement Disable an inherited method deprecated: an. Explanation. String Warn that the sending method is deprecated. © O. Nierstrasz 56
Numbers © O. Nierstrasz 57
Abstract methods in Smalltalk Number>>+ a. Number "Answer the sum of the receiver and a. Number. " self subclass. Responsibility Object>>subclass. Responsibility "This message sets up a framework for the behavior of the class' subclasses. Announce that the subclass should have implemented this message. " self error: 'My subclass should have overridden ', this. Context sender selector print. String © O. Nierstrasz 58
Strings © O. Nierstrasz 59
Strings #mac as. String 12 print. String with: $A 'can''t' at: 4 'hello', 'world' 'mac' '12' 'A' $' 'hello world' > To introduce a single quote inside a string, just double it. © O. Nierstrasz 60
Comments and Tips Use comments to provide useful “doits” — Avoid putting a space between the " and the first character! "Test. Runner open" © O. Nierstrasz 61
Literal and dynamic arrays Literal arrays #(1 + 2. 3 ) #(1 #+ 2 #. 3) Dynamic arrays { 1 + 2. 3 } #(3 3) Array with: 1+2 with: 3 #(3 3) { … } is a shortcut for Array with: … © O. Nierstrasz 62
Symbols vs. Strings > Symbols are used as method selectors and unique keys for dictionaries — Symbols are read-only objects, strings are mutable — A symbol is unique, strings are not 'cal', 'vin' == 'calvin'. false ('cal', 'vin') as. Symbol == #calvin. true NB: Comparing strings is slower than comparing symbols by a factor of 5 to 10. However, converting a string to a symbol is more than 100 times more expensive. © O. Nierstrasz 63
Blocks > Syntax: [ : arg 1 : arg 2 | |temp 1 temp 2| expression ] — Returns last expression of the block [2 + 3 + 4 + 5] value [: x | x + 3 + 4 + 5 ] value: 2 [: x : y | x + y + 4 + 5] value: 2 value: 3 [: x : y : z | x + y + z + 5] value: 2 value: 3 value: 4 [: x : y : z : w | x + y + z + w] value: 2 value: 3 value: 4 value: 5 © O. Nierstrasz 64
Booleans © O. Nierstrasz 65
If. True: If. False: Integer>>factorial "Answer the factorial of the receiver. " self = 0 if. True: [^ 1]. self > 0 if. True: [^ self * (self - 1) factorial]. self error: 'Not valid for negative integers' © O. Nierstrasz 66
Six Pseudo-Variables > The following pseudo-variables are hard-wired into the Smalltalk compiler. nil true false self super this. Context © O. Nierstrasz A reference to the Undefined. Object Singleton instance of the class True Singleton instance of the class False Reference to this object Method lookup starts from object’s class Reference to this object (!) Method lookup starts from the superclass Reification of execution context 67
Control Constructs > All control constructs in Smalltalk are implemented by sending messages — No keywords — Open, extensible — Built up from Booleans, Numbers, Collections and Blocks © O. Nierstrasz 68
Various kinds of Loops |n| n: = 10. [n>0] while. True: [ Transcript show: n; cr. n: =n-1] 1 to: 10 do: [: n | Transcript show: n; cr ] (1 to: 10) do: [: n | Transcript show: n; cr ] 10 times. Repeat: [ Transcript show: 'hi'; cr ] In each case, what is the target object? © O. Nierstrasz 69
Collections Resist the temptation to program your own collections! © O. Nierstrasz 70
Common messages #(1 2 3 4) includes: 5 #(1 2 3 4) size #(1 2 3 4) is. Empty #(1 2 3 4) contains: [: some | some < 0 ] #(1 2 3 4) do: [: each | Transcript show: each ] #(1 2 3 4) with: #(5 6 7 8) do: [: x : y | Transcript show: x+y; cr] #(1 2 3 4) select: [: each | each odd ] #(1 2 3 4) reject: [: each | each odd ] #(1 2 3 4) detect: [: each | each odd ] #(1 2 3 4) collect: [: each | each even ] #(1 2 3 4) inject: 0 into: [: sum : each | sum + each] © O. Nierstrasz false 4 false #(1 3) #(2 4) 1 {false. true} 10 71
Iteration — the hard road and the easy road How to get absolute values of a collection of integers? |a. Col result| a. Col : = #( 2 -3 4 -35 4 -11). result : = a. Col species new: a. Col size. 1 to: a. Col size do: [ : each | result at: each put: (a. Col at: each) abs]. result #(2 3 4 35 4 11) #( 2 -3 4 -35 4 -11) collect: [: each | each abs ] #(2 3 4 35 4 11) NB: The second solution also works for indexable collections and sets. © O. Nierstrasz 72
Concatenation How do you put two collections together? > Send “, ” to the first with the second as argument (1 to: 3), (4 to: 6) #(1 2 3 4 5 6) (Dictionary new. From: { #a -> 1}), (Dictionary new. From: { #b -> 2}) a Dictionary(#a->1 #b->2 ) © O. Nierstrasz 73
Sorted Collection How do you sort a collection? > Convert it to a Sorted Collection. 'hello' as. Sorted. Collection a Sorted. Collection($e $h $l $l $o) 'hello' as. Sorted. Collection: [: a : b | b<=a ] a Sorted. Collection($o $l $l $h $e) © O. Nierstrasz 74
Duplicate Removing Set How do you remove the duplicates from a Collection? > Convert it to a Set 'hello world' as. Set a Set(Character space $r $d $e $w $h $l $o) © O. Nierstrasz 75
Converting > Send as. Set, as. Bag, as. Sorted. Collection etc. to convert between kinds of collections > Send keys, values to extract collections from dictionaries > Use various factory methods to build new kinds of collections from old Dictionary new. From: {1 ->#a. 2 ->#b. 3 ->#c} © O. Nierstrasz 76
Roadmap > What is Smalltalk? > Getting started > Syntax in a nutshell > Test-driven development > Version control > Standard classes — Object, Number, Boolean … > Common idioms > Debugging practices > Metaclasses in 7 points > Seaside demo © O. Nierstrasz 77
Snakes and Ladders See: http: //en. wikipedia. org/wiki/Snakes_and_ladders © O. Nierstrasz 78
Scripting a use case > Construct the board > Add some players > Play the game © O. Nierstrasz Snakes. And. Ladders class>>example "self example play. To. End" ^ (self new) add: First. Square new; add: (Ladder. Square forward: 4); add: Board. Square new; add: (Ladder. Square forward: 2); add: Board. Square new; add: (Snake. Square back: 6); add: Board. Square new; join: (Game. Player named: 'Jack'); join: (Game. Player named: 'Jill'); yourself 79
Distributing responsibilities © O. Nierstrasz 80
Lots of Little Methods > Once and only once — “In a program written with good style, everything is said once and only once. ” > Lots of little pieces — “Good code invariably has small methods and small objects. Only by factoring the system into many small pieces of state and function can you hope to satisfy the ‘once and only once’ rule. ” – © O. Nierstrasz Kent Beck, Smalltalk Best Practice Patterns 81
Composed Method How do you divide a program into methods? > Divide your program into methods that perform one identifiable task. — Keep all of the operations in a method at the same level of abstraction. — This will naturally result in programs with many small methods, each a few lines long. © O. Nierstrasz 82
Snakes and Ladders methods • 68 methods • only 7 are more than 6 LOC (including comments!) — 1 of these is the “main” method — the other 6 are test methods © O. Nierstrasz 83
How to initialize objects? In Smalltalk, — methods are public, and — instance variables are private So, how can a class (an object) initialize the instance variables of its instances (other objects)? © O. Nierstrasz 84
Explicit Initialization How do you initialize instance variables to their default values? > Implement a method initialize that sets all the values explicitly. —Override the class message new to invoke it on new instances Snakes. And. Ladders>>initialize super initialize. die : = Die new. squares : = Ordered. Collection new. players : = Ordered. Collection new. turn : = 1. over : = false. © O. Nierstrasz 85
Who calls initialize? > In Squeak 3. 9, the method new calls initialize by default. Behavior>>new ^ self basic. New initialize > NB: You can override new, but you should never override basic. New! > Every metaclass ultimately inherits from Behaviour – © O. Nierstrasz More on this later … 86
Constructor Method How do you represent instance creation? > Provide methods in the class side “instance creation” protocol that create well-formed instances. Pass all required parameters to them. Ladder. Square class>>forward: number ^ self new set. Forward: number Snake. Square class>>back: number ^ self new set. Back: number © O. Nierstrasz 87
Constructor Parameter Method How do you set instance variables from the parameters to a Constructor Method? > Code a single method that sets all the variables. Preface its name with “set”, then the names of the variables. Snake. Square>>set. Back: a. Number back : = a. Number. Ladder. Square>>set. Forward: a. Number forward : = a. Number. Board. Square>>set. Position: a. Number board: a. Board position : = a. Number. board : = a. Board © O. Nierstrasz 88
Debug Printing Method How do you code the default printing method? — There are two audiences: – – you (wanting a lot of information) your clients (wanting only external properties) > Override print. On: to provide information about object’s structure to the programmer — Put printing methods in the “printing” protocol © O. Nierstrasz 89
Viewing the game state Snakes. And. Ladders example inspect In order to provide a simple way to monitor the game state and to ease debugging, we need a textual view of the game © O. Nierstrasz 90
Implementing print. On: Snakes. And. Ladders>>print. On: a. Stream squares do: [: each | each print. On: a. Stream] Board. Square>>print. On: a. Stream next. Put. All: '[', position print. String, self contents, ']' Ladder. Square>>print. On: a. Stream super print. On: a. Stream next. Put. All: forward as. String, '+>' Snake. Square>>print. On: a. Stream next. Put. All: '<-', back as. String. super print. On: a. Stream Game. Player>>print. On: a. Stream next. Put. All: name © O. Nierstrasz 91
Viewing the game state Snakes. And. Ladders example inspect © O. Nierstrasz 92
Interacting with the game With a bit of care, the Inspector can serve as a basic GUI for objects we are developing © O. Nierstrasz 93
Query Method How do you represent testing a property of an object? > Provide a method that returns a Boolean. — Name it by prefacing the property name with a form of “be” — is, was, will etc. © O. Nierstrasz 94
Some query methods Snakes. And. Ladders>>is. Not. Over ^ self is. Over not Board. Square>>is. First. Square ^ position = 1 Board. Square>>is. Last. Square ^ position = board last. Position Board. Square>>is. Occupied ^ player not. Nil First. Square>>is. Occupied ^ players size > 0 © O. Nierstrasz 95
Constant Method How do you code a constant? > Create a method that returns the constant Fraction>>one ^ self numerator: 1 denominator: 1 © O. Nierstrasz 96
Roadmap > What is Smalltalk? > Getting started > Syntax in a nutshell > Test-driven development > Version control > Standard classes — Object, Number, Boolean … > Common idioms > Debugging practices > Metaclasses in 7 points > Seaside demo © O. Nierstrasz 97
Debug printing > Basic printing — You can use the Transcript to display progress Transcript cr; show: ‘The total= ’, self total print. String. > Optional printing — Use a global or a class to control printing information Debug if. True: [Transcript show: self total print. String] Debug > 4 if. True: [Transcript show: self total print. String] Debug print: [Transcript show: self total print. String] © O. Nierstrasz 98
Tests are your friends! > Resist the temptation to write debugging print methods — Write a test instead! > Resist the temptation to evaluate ad hoc expressions in a Workspace — Write a test instead! — Tests are reusable – – © O. Nierstrasz You will have to spend the effort debugging anyway Amortize the investment by coding your debugging effort as tests 99
The Inspector is your friend! > You can inspect anything — Inspect any expression — View the print. String state — Interact with any object — Inspect instance variables — Navigate through the system © O. Nierstrasz 100
Use the Inspector to make ad hoc changes > You can use the Inspector as an ad hoc interface to modify the state of a running system — Use this sparingly! If we change the name of a Game. Player, this will be reflected in the running system. © O. Nierstrasz 101
Breakpoints > Send the message self halt to start the debugger at an arbitrary location Snakes. And. Ladders>>play. One. Move | result | self assert: self invariant. self halt. ^ self is. Over … © O. Nierstrasz 102
The Debugger is your friend! Everything is an object! > You can: — Inspect any entity — Evaluate any code — Modify code on the fly Don’t forget: — Keep the Debugger open! © O. Nierstrasz 103
Dangling self halt > When you have finished debugging, don’t forget to remove any self halt in the code! — Running all the tests should catch this! © O. Nierstrasz 104
The Browser is your friend! How can we browse all methods that send to super? — — — Browse “browse” Object>>browse Object>>system. Navigation System. Navigation>>browse. Methods. With. Source. String: System. Navigation default browse. Methods. With. Source. String: 'super' © O. Nierstrasz A bit slow, and contains many false negatives 105
The Message Name Finder is your friend! > We continue browsing: — System. Navigation>>browse. Methods. With* — System. Navigation>>browse. All. Select: > Query the Message Name Finder for “super” — Yields Compiled. Method>>sends. To. Super > Better solution: System. Navigation default browse. All. Select: [: method | method sends. To. Super ] Fast, and accurate! © O. Nierstrasz 106
Roadmap > What is Smalltalk? > Getting started > Syntax in a nutshell > Test-driven development > Version control > Standard classes — Object, Number, Boolean … > Common idioms > Debugging practices > Metaclasses in 7 points > Seaside demo © O. Nierstrasz 107
Metaclasses in 7 points 1. Every object is an instance of a class 2. Every class eventually inherits from Object 3. Every class is an instance of a metaclass 4. The metaclass hierarchy parallels the class hierarchy 5. Every metaclass inherits from Class and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass Adapted from Goldberg & Robson, Smalltalk-80 — The Language © O. Nierstrasz 108
1. Every object is an instance of a class Remember the Snakes and Ladders Board Game … © O. Nierstrasz 109
2. Every class inherits from Object a. Snake. Square is-a Snake. Square and is-a Board. Square and is-an Object Caveat: in Squeak, Object has a superclass called Proto. Object © O. Nierstrasz 110
Method lookup follows the inheritance chain © O. Nierstrasz 111
3. Every class is an instance of a metaclass © O. Nierstrasz 112
4. The metaclass hierarchy parallels the class hierarchy © O. Nierstrasz 113
Classes are objects too … back: is a Snake constructor method © O. Nierstrasz 114
5. Every metaclass inherits from Class and Behavior Every class is-a Class © O. Nierstrasz 115
6. Every metaclass is an instance of Metaclass © Oscar Nierstrasz © O. Nierstrasz 116
7. The metaclass of Metaclass is an instance of Metaclass © O. Nierstrasz 117
Roadmap > What is Smalltalk? > Getting started > Syntax in a nutshell > Test-driven development > Version control > Standard classes — Object, Number, Boolean … > Common idioms > Debugging practices > Metaclasses in 7 points > Seaside demo © O. Nierstrasz 118
License > http: //creativecommons. org/licenses/by-sa/3. 0/ Attribution-Share. Alike 3. 0 Unported You are free: to Share — to copy, distribute and transmit the work to Remix — to adapt the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights. © O. Nierstrasz 119
Variables > Shared variables — initial uppercase > Private variables — initial lowercase variable Shared variable private variable Class variable instance variable Global variable named Pool variable © O. Nierstrasz temporary variable indexed method parameter | method temporary | : block parameter | block temporary | 120
Initializing class variables > Class variables should be initialized by an initialize method on the class side Magnitude subclass: #Date instance. Variable. Names: 'julian. Day. Number ' class. Variable. Names: 'Days. In. Month First. Day. Of. Month. Names Seconds. In. Day Week. Day. Names ' pool. Dictionaries: '' category: 'Kernel-Magnitudes' Date class>>initialize. . . Week. Day. Names : = #(Sunday Monday. . . ). Month. Names : = #(January February. . . ). Days. In. Month : = #(31 28 31 30 31). © O. Nierstrasz 121
Class. Variables vs. Instance Variables © O. Nierstrasz 122


























































































































