12. A bit of Smalltalk
© O. Nierstrasz P2 — A bit of Smalltalk 11.2 Roadmap Some history Smalltalk syntax & object model The Smalltalk environment
© O. Nierstrasz P2 — A bit of Smalltalk 11.3 Roadmap Some history Smalltalk syntax & object model The Smalltalk environment
© O. Nierstrasz P2 — A bit of Smalltalk 11.4 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 P2 — A bit of Smalltalk 11.5 Squeak resources Free download — Print-on-demand SqueakByExample.org A modern, open-source Smalltalk One-click image
© O. Nierstrasz P2 — A bit of Smalltalk 11.6 Available Smalltalks Squeak — — Cincom VisualWorks Smalltalk —smalltalk.cincom.com Dolphin Smalltalk — Gnu Smalltalk — Others … —en.wikipedia.org/wiki/Smalltalk
© O. Nierstrasz P2 — A bit of Smalltalk 11.7 Object-oriented language genealogy
© O. Nierstrasz P2 — A bit of Smalltalk 11.8 The origins of Smalltalk Alan Kay’s Dynabook project (1968) Alto — Xerox PARC (1973)
© O. Nierstrasz P2 — A bit of Smalltalk 11.9 Origins of Smalltalk Project at Xerox PARC in 1970s —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/EarlyHistoryST.htmlgagne.homedns.org/~tgagne/contrib/EarlyHistoryST.html
© O. Nierstrasz P2 — A bit of Smalltalk What is Smalltalk? Guiding principles: —“Everything is an Object” —“Everything happens by sending messages” Pure OO language —Single inheritance —Dynamically typed Language and environment —Class browser, debugger, inspector, … —Mature class library and tools Virtual machine —Objects exist in a persistent image [+ changes] —Incremental compilation
© O. Nierstrasz P2 — A bit of Smalltalk Smalltalk architecture Image Changes + Virtual machine Sources +
© O. Nierstrasz P2 — A bit of Smalltalk Smalltalk vs. C++ vs. Java SmalltalkC++Java Object modelPureHybrid Garbage collectionAutomaticManualAutomatic InheritanceSingleMultipleSingle TypesDynamicStatic ReflectionFully reflectiveIntrospection ConcurrencySemaphoresSome librariesMonitors ModulesCategories, namespaces NamespacesPackages
© O. Nierstrasz P2 — A bit of Smalltalk Roadmap Some history Smalltalk syntax & object model The Smalltalk environment
© O. Nierstrasz P2 — A bit of Smalltalk Three kinds of messages > Unary messages > Binary messages > Keyword messages 5 factorial Transcript cr raisedTo: 10 modulo: 5 Transcript show: 'hello world'
© O. Nierstrasz P2 — A bit of Smalltalk Precedence 2 raisedTo: factorial * (2 * 3) 128 9(!) 7 First unary, then binary, then keyword: Use parentheses to force order:
© O. Nierstrasz P2 — A bit of Smalltalk A typical method in the class Point <= aPoint "Answer whether the receiver is neither below nor to the right of aPoint." ^ x <= aPoint x and: [y <= aPoint y] <= true Method nameArgumentComment Return Binary message Keyword messageInstance variable Block
© O. Nierstrasz P2 — A bit of Smalltalk Statements and cascades | p pen | p := pen := Pen new. pen up. pen goto: p; down; goto: p+p Temporary variables Statement Cascade
© O. Nierstrasz P2 — A bit of Smalltalk Literals and constants Strings & Characters'hello' $a Numbers Symbols#yadayada Arrays#(1 2 3) Pseudo-variablesself super Constantstrue false
© O. Nierstrasz P2 — A bit of Smalltalk Fun with Numbers Large and exact numbers Automatic coercion 1000 factorial / 999 factorial 1000 factorial printString size (1/3) + (2/3) 1 class maxVal (1 class maxVal + 1) class LargePositiveInteger
© O. Nierstrasz P2 — A bit of Smalltalk “Natural language” album play album playTrack: 1 album playFromTrack: 5 to: 10
© O. Nierstrasz P2 — A bit of Smalltalk More syntax Comments are enclosed in double quotes Use periods to separate expressions Use semi-colons to send a cascade of messages to the same object "This is a comment." Transcript cr; show: 'hello world'; cr Transcript cr. Transcript show: 'hello world’. Transcript cr "NB: don’t need one here"
© O. Nierstrasz P2 — A bit of Smalltalk Variables Declare local variables with | … | Use := to assign a value to a variable [NB: also (_) ] | x y | x := 1
© O. Nierstrasz P2 — A bit of Smalltalk Method Return Use a caret to return a value from a method or a block By default, methods return self max: aNumber ^ self < aNumber ifTrue: [aNumber] ifFalse: [self] 1 max: 2 2
© O. Nierstrasz P2 — A bit of Smalltalk Blocks Use square brackets to delay evaluation of expressions ^ 1 < 2 ifTrue: ['smaller'] ifFalse: ['bigger'] 'smaller'
© O. Nierstrasz P2 — A bit of Smalltalk Variables Local variables are delimited by |var| Block variables by :var| OrderedCollection>>collect: aBlock "Evaluate aBlock with each of my elements as the argument." | newCollection | newCollection := self species new: self size. firstIndex to: lastIndex do: [ :index | newCollection addLast: (aBlock value: (array at: index))]. ^ newCollection (OrderedCollection with: 10 with: 5) collect: [:each| each factorial ] an OrderedCollection( )
© O. Nierstrasz P2 — A bit of Smalltalk Control Structures (I) Every control structure is realized by message sends max: aNumber ^ self < aNumber ifTrue: [aNumber] ifFalse: [self] 4 timesRepeat: [Beeper beep]
© O. Nierstrasz P2 — A bit of Smalltalk Control Structures (II) Every control structure is realized by message sends |n| n := 10. [n>0] whileTrue: [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 P2 — A bit of Smalltalk Creating objects Class methods Factory methods OrderedCollection new Array with: 1 with: 2 1/2 a Point a Fraction
© O. Nierstrasz P2 — A bit of Smalltalk Creating classes Send a message to a class (!) Number subclass: #Complex instanceVariableNames: 'real imaginary' classVariableNames: '' poolDictionaries: '' category: 'ComplexNumbers'
© O. Nierstrasz P2 — A bit of Smalltalk Roadmap Some history Smalltalk syntax & object model The Smalltalk environment
© O. Nierstrasz P2 — A bit of Smalltalk Three-button mouse Select Operate Window
© O. Nierstrasz P2 — A bit of Smalltalk World Menu and Open Menu
© O. Nierstrasz P2 — A bit of Smalltalk “Hello World”
© O. Nierstrasz P2 — A bit of Smalltalk Do it, Print it, … You can evaluate any expression anywhere in Smalltalk.
© O. Nierstrasz P2 — A bit of Smalltalk The Smalltalk Browser
© O. Nierstrasz P2 — A bit of Smalltalk The Debugger
© O. Nierstrasz P2 — A bit of Smalltalk The Inspector
© O. Nierstrasz P2 — 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 P2 — A bit of Smalltalk Demo Changing a running system …
© O. Nierstrasz P2 — 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 P2 — 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 P2 — A bit of Smalltalk License > Attribution-ShareAlike 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.