Presentation is loading. Please wait.

Presentation is loading. Please wait.

12. A bit of Smalltalk. © O. Nierstrasz P2 — A bit of Smalltalk 11.2 Roadmap  Some history  Smalltalk syntax & object model  The Smalltalk environment.

Similar presentations


Presentation on theme: "12. A bit of Smalltalk. © O. Nierstrasz P2 — A bit of Smalltalk 11.2 Roadmap  Some history  Smalltalk syntax & object model  The Smalltalk environment."— Presentation transcript:

1 12. A bit of Smalltalk

2 © O. Nierstrasz P2 — A bit of Smalltalk 11.2 Roadmap  Some history  Smalltalk syntax & object model  The Smalltalk environment

3 © O. Nierstrasz P2 — A bit of Smalltalk 11.3 Roadmap  Some history  Smalltalk syntax & object model  The Smalltalk environment

4 © 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

5 © O. Nierstrasz P2 — A bit of Smalltalk 11.5 Squeak resources Free download — Print-on-demand SqueakByExample.org www.squeak.org A modern, open-source Smalltalk www.seaside.st One-click image

6 © O. Nierstrasz P2 — A bit of Smalltalk 11.6 Available Smalltalks  Squeak —www.squeak.org —www.seaside.st  Cincom VisualWorks Smalltalk —smalltalk.cincom.com  Dolphin Smalltalk —www.object-arts.com/Home.htm  Gnu Smalltalk —www.gnu.org/software/smalltalk/smalltalk.html  Others … —en.wikipedia.org/wiki/Smalltalk

7 © O. Nierstrasz P2 — A bit of Smalltalk 11.7 Object-oriented language genealogy

8 © O. Nierstrasz P2 — A bit of Smalltalk 11.8 The origins of Smalltalk Alan Kay’s Dynabook project (1968) Alto — Xerox PARC (1973)

9 © 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

10 © O. Nierstrasz P2 — A bit of Smalltalk 11.10 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

11 © O. Nierstrasz P2 — A bit of Smalltalk 11.11 Smalltalk architecture Image Changes + Virtual machine Sources +

12 © O. Nierstrasz P2 — A bit of Smalltalk 11.12 Smalltalk vs. C++ vs. Java SmalltalkC++Java Object modelPureHybrid Garbage collectionAutomaticManualAutomatic InheritanceSingleMultipleSingle TypesDynamicStatic ReflectionFully reflectiveIntrospection ConcurrencySemaphoresSome librariesMonitors ModulesCategories, namespaces NamespacesPackages

13 © O. Nierstrasz P2 — A bit of Smalltalk 11.13 Roadmap  Some history  Smalltalk syntax & object model  The Smalltalk environment

14 © O. Nierstrasz P2 — A bit of Smalltalk 11.14 Three kinds of messages > Unary messages > Binary messages > Keyword messages 5 factorial Transcript cr 3 + 4 3 raisedTo: 10 modulo: 5 Transcript show: 'hello world'

15 © O. Nierstrasz P2 — A bit of Smalltalk 11.15 Precedence 2 raisedTo: 1 + 3 factorial 1 + 2 * 3 1 + (2 * 3) 128 9(!) 7 First unary, then binary, then keyword: Use parentheses to force order:

16 © O. Nierstrasz P2 — A bit of Smalltalk 11.16 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] (2@3) <= (5@6) true Method nameArgumentComment Return Binary message Keyword messageInstance variable Block

17 © O. Nierstrasz P2 — A bit of Smalltalk 11.17 Statements and cascades | p pen | p := 100@100. pen := Pen new. pen up. pen goto: p; down; goto: p+p Temporary variables Statement Cascade

18 © O. Nierstrasz P2 — A bit of Smalltalk 11.18 Literals and constants Strings & Characters'hello' $a Numbers1 3.14159 Symbols#yadayada Arrays#(1 2 3) Pseudo-variablesself super Constantstrue false

19 © O. Nierstrasz P2 — A bit of Smalltalk 11.19 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 1000 2568 1 1073741823 LargePositiveInteger

20 © O. Nierstrasz P2 — A bit of Smalltalk 11.20 “Natural language” album play album playTrack: 1 album playFromTrack: 5 to: 10

21 © O. Nierstrasz P2 — A bit of Smalltalk 11.21 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"

22 © O. Nierstrasz P2 — A bit of Smalltalk 11.22 Variables  Declare local variables with | … |  Use := to assign a value to a variable [NB: also  (_) ] | x y | x := 1

23 © O. Nierstrasz P2 — A bit of Smalltalk 11.23 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

24 © O. Nierstrasz P2 — A bit of Smalltalk 11.24 Blocks  Use square brackets to delay evaluation of expressions ^ 1 < 2 ifTrue: ['smaller'] ifFalse: ['bigger'] 'smaller'

25 © O. Nierstrasz P2 — A bit of Smalltalk 11.25 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(3628800 120)

26 © O. Nierstrasz P2 — A bit of Smalltalk 11.26 Control Structures (I)  Every control structure is realized by message sends max: aNumber ^ self < aNumber ifTrue: [aNumber] ifFalse: [self] 4 timesRepeat: [Beeper beep]

27 © O. Nierstrasz P2 — A bit of Smalltalk 11.27 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 ]

28 © O. Nierstrasz P2 — A bit of Smalltalk 11.28 Creating objects  Class methods  Factory methods OrderedCollection new Array with: 1 with: 2 1@2 1/2 a Point a Fraction

29 © O. Nierstrasz P2 — A bit of Smalltalk 11.29 Creating classes  Send a message to a class (!) Number subclass: #Complex instanceVariableNames: 'real imaginary' classVariableNames: '' poolDictionaries: '' category: 'ComplexNumbers'

30 © O. Nierstrasz P2 — A bit of Smalltalk 11.30 Roadmap  Some history  Smalltalk syntax & object model  The Smalltalk environment

31 © O. Nierstrasz P2 — A bit of Smalltalk 11.31 Three-button mouse Select Operate Window

32 © O. Nierstrasz P2 — A bit of Smalltalk 11.32 World Menu and Open Menu

33 © O. Nierstrasz P2 — A bit of Smalltalk 11.33 “Hello World”

34 © O. Nierstrasz P2 — A bit of Smalltalk 11.34 Do it, Print it, … You can evaluate any expression anywhere in Smalltalk.

35 © O. Nierstrasz P2 — A bit of Smalltalk 11.35 The Smalltalk Browser

36 © O. Nierstrasz P2 — A bit of Smalltalk 11.36 The Debugger

37 © O. Nierstrasz P2 — A bit of Smalltalk 11.37 The Inspector

38 © O. Nierstrasz P2 — A bit of Smalltalk 11.38 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

39 © O. Nierstrasz P2 — A bit of Smalltalk 11.39 Demo Changing a running system …

40 © O. Nierstrasz P2 — A bit of Smalltalk 11.40 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?

41 © O. Nierstrasz P2 — A bit of Smalltalk 11.41 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?

42 © O. Nierstrasz P2 — A bit of Smalltalk 11.42 License > http://creativecommons.org/licenses/by-sa/2.5/ 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.


Download ppt "12. A bit of Smalltalk. © O. Nierstrasz P2 — A bit of Smalltalk 11.2 Roadmap  Some history  Smalltalk syntax & object model  The Smalltalk environment."

Similar presentations


Ads by Google