11 A bit of Smalltalk P 2 A

11. A bit of Smalltalk

P 2 — A bit of Smalltalk Overview > Some history > Smalltalk syntax & object model > The Smalltalk environment (demo) References: > en. wikipedia. org/wiki/Smalltalk > squeak. org/documentation/ © O. Nierstrasz 2

P 2 — A bit of Smalltalk What you should know! What are the key differences between Smalltalk, C++ and Java? What is at the root of the Smalltalk class hierarchy? What kinds of messages can one send to objects? What is a cascade? Why does 1 + 2/3 = 1 in Smalltalk? How are control structures realized? How is a new class created? What are categories for? What are Factory methods? When are they useful? © O. Nierstrasz 3

P 2 — A bit of Smalltalk Essential Smalltalk Texts > Smalltalk by Example, Alec Sharp, Mc. Graw Hill, 1997 [ PDF available below] > The Smalltalk Design Pattern Companion, S. Alpert, K. Brown and B. Woolf, Addison-Wesley, 1998 > Smalltalk Best Practice Patterns, K. Beck, Prentice Hall, 1997 > Squeak, X. Briffault, S. Ducasse, Eyrolles, 2001 [in French] Squeak, Open Personal Computing and Multimedia, Kim Rose and Mark Guzdial, Prentice-Hall 2000. More: www. iam. unibe. ch/~ducasse/Free. Books. html > > © O. Nierstrasz 4

P 2 — A bit of Smalltalk Available Smalltalks > Squeak — www. squeak. org > Cincom Visual. Works Smalltalk — smalltalk. cincom. com > Dolphin Smalltalk — www. object-arts. com/Home. htm > Gnu Smalltalk — www. gnu. org/software/smalltalk. html > Others … — en. wikipedia. org/wiki/Smalltalk © O. Nierstrasz 5

P 2 — A bit of Smalltalk History © O. Nierstrasz 6

P 2 — A bit of Smalltalk Origins of Smalltalk > Project at Xerox PARC in 1970 s — Language and environment for new generation of graphical workstations (target: “Dynabook”) > In Smalltalk-72, every object was an independent entity — Language was designed for children (!) — Evolved towards a meta-reflective architecture > Smalltalk-80 is the standard > See: The Early History of Smalltalk, by Alan Kay — gagne. homedns. org/~tgagne/contrib/Early. History. ST. html © O. Nierstrasz 7

P 2 — A bit of Smalltalk What is Smalltalk? > Guiding principle: “Everything is an Object” > Language and environment — Class browser, debugger, inspector, … — Mature class library and tools > Virtual machine — Objects exist in a persistent image [+ changes] — Incremental compilation > Pure OO language — Single inheritance — Dynamically typed © O. Nierstrasz 8

P 2 — A bit of Smalltalk vs. C++ vs. Java Smalltalk C++ Java Pure Hybrid Automatic Manual Automatic Single Multiple Single Dynamic Static Fully reflective Introspection Concurrency Semaphores Some libraries Monitors Modules Categories, namespaces Namespaces Packages Object model Garbage collection Inheritance Types Reflection © O. Nierstrasz 9

P 2 — A bit of Smalltalk “Hello World” © O. Nierstrasz 10

P 2 — A bit of Smalltalk Syntax example. With. Number: x "A method that illustrates every part of Smalltalk method syntax except primitives. It has unary, binary, and key word messages, declares arguments and temporaries (but not block temporaries), accesses a global variable (but not and instance variable), uses literals (array, character, symbol, string, integer, float), uses the pseudo variable true false, nil, self, and super, and has sequence, assignment, return and cascade. It has both zero argument and one argument blocks. It doesn’t do anything useful, though" |y| true & false not & (nil is. Nil) if. False: [self halt]. y : = self size + super size. #($a #a 'a' 1 1. 0) do: [: each | Transcript show: (each class name); show: (each print. String); show: ' ']. ^ x < y © O. Nierstrasz 11

P 2 — A bit of Smalltalk 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 12

P 2 — A bit of Smalltalk Fun with Numbers > Large and exact numbers 1000 factorial / 999 factorial 1000 factorial print. String size 1000 2568 > Automatic coercion (1/3) + (2/3) 1 class max. Val (1 class max. Val + 1) class © O. Nierstrasz 1 1073741823 Large. Positive. Integer 13

P 2 — A bit of Smalltalk “Natural language” album play. Track: 1 album play. From. Track: 5 to: 10 © O. Nierstrasz 14

P 2 — A bit of Smalltalk Syntax Every expression is a message send > Unary messages Transcript cr 5 factorial > Binary messages > Keyword messages © O. Nierstrasz 3 + 4 Transcript show: 'hello world' 2 raised. To: 32 3 raised. To: 10 modulo: 5 15

P 2 — A bit of Smalltalk Precedence (…) > Unary > Binary > Keyword 1. 2. 3. 4. Evaluate left-to-right Unary messages have highest precedence Next are binary messages Keyword messages have lowest precedence 2 raised. To: 1 + 3 factorial 5. 128 Use parentheses to change precedence 1 + 2 * 3 1 + (2 * 3) © O. Nierstrasz 9 7 (!) 16

P 2 — A bit of Smalltalk More syntax > Comments are enclosed in double quotes "This is a comment. " > Use periods to separate expressions Transcript cr. Transcript show: 'hello world’. Transcript cr "NB: don’t need one here" > Use semi-colons to send a cascade of messages to the same object Transcript cr; show: 'hello world'; cr © O. Nierstrasz 17

P 2 — A bit of Smalltalk Variables > Declare local variables with | … | | x y | > Use : = or (_) to assign a value to a variable x : = 1 © O. Nierstrasz 18

P 2 — A bit of Smalltalk 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 19

P 2 — A bit of Smalltalk Block closures > Use square brackets to delay evaluation of expressions ^ 1 < 2 if. True: ['smaller'] if. False: ['bigger'] 'smaller' > Use a caret to return a value from a method or a block 1 < 2 if. True: [^'smaller'] if. False: [^'bigger'] 'smaller' © O. Nierstrasz 20

P 2 — A bit of Smalltalk 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 21

P 2 — A bit of Smalltalk Control Structures (I) > 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 22

P 2 — A bit of Smalltalk Control Structures > Every control structure is realized by message sends |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 ] © O. Nierstrasz 23

P 2 — A bit of Smalltalk Creating objects > Class methods Ordered. Collection new Array with: 1 with: 2 > Factory methods 1@2 1/2 © O. Nierstrasz a Point a Fraction 24

P 2 — A bit of Smalltalk 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 25

P 2 — A bit of Smalltalk The Smalltalk Browser © O. Nierstrasz 26

P 2 — A bit of Smalltalk The Debugger © O. Nierstrasz 27

P 2 — A bit of Smalltalk The Inspector © O. Nierstrasz 28

P 2 — A bit of Smalltalk Other Tools > File List — Browse, import, open files > Change Sorter — Name, organize all source code changes > Method Finder, Message Name tool — Find methods by name, behaviour > SUnit — Manage & run unit tests © O. Nierstrasz 29

P 2 — A bit of Smalltalk Demo Changing a running system … © O. Nierstrasz 30

P 2 — A bit of Smalltalk What you should know! What are the key differences between Smalltalk, C++ and Java? What is at the root of the Smalltalk class hierarchy? What kinds of messages can one send to objects? What is a cascade? Why does 1 + 2/3 = 1 in Smalltalk? How are control structures realized? How is a new class created? What are categories for? What are Factory methods? When are they useful? © O. Nierstrasz 31

P 2 — A bit of Smalltalk Can you answer these questions? Which is faster, a program written in Smalltalk, C++ or Java? Which is faster to develop & debug, a program written in Smalltalk, C++ or Java? How are Booleans implemented? Is a comment an Object? How would you check this? What is the equivalent of a static method in Smalltalk? How do you make methods private in Smalltalk? What is the difference between = and ==? If classes are objects too, what classes are they instances of? © O. Nierstrasz 32

P 2 — A bit of Smalltalk License > http: //creativecommons. org/licenses/by-sa/2. 5/ Attribution-Share. Alike 2. 5 You are free: • to copy, distribute, display, and perform the work • to make derivative works • to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. • For any reuse or distribution, you must make clear to others the license terms of this work. • Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. © O. Nierstrasz 33
- Slides: 33