10. smalltalk and closures programmierung 2, ss 2007 adrian kuhn, phd candidate software composition...
TRANSCRIPT
10. Smalltalk and Closures
Programmierung 2, SS 2007
Adrian Kuhn, PhD candidate
Software Composition Group
University of Bern
Beyond Java
Beyond is the future
Beyond Java 6.0 is Java 7.0
Beyond, also, is the past
Beyond Java is Smalltalk
Developed in the 70ies
Smalltalk-80 since 1980
For example, Closures
Planned for Java 7.0 in 2008
Featured in Smalltalk since 1980
Get winner I
public boolean hasWinner() { for (Player each : players) { if (each.isWinner()) { return true; } } return false;}
Get winner II
public boolean hasWinner() { return players.contains({ Player each | each.isWinner() });}
Get winner III
hasWinner ^players contains: [ :each | each isWinner ]
Handler I
Jbutton button = new JButton(“Save”);button.addActionListener( new ActionListener() { public void actionPerformed (ActionEvent e) { this.saveAndClose(); } });
Handler II
Jbutton button = new JButton(“Save”);button.addActionListener({ ActionEvent e | this.saveAndClose() });
Handler III
button = Button new: ‘Save’.Button addActionListener: [ :event | self saveAndClose ].
Closure
• A code block as first-class object
• Developed in the 60ies
• First implemented in Scheme, ie LISP
• Smalltalk-80, Ruby, Python, etc…
• In discussion for Java 7
Smalltalk
• An object oriented language
• Everything is an object– including primitive values
• Everything is a message send– including the control flow
Syntax
How to format it?
Smalltalk Syntax IAlbum album = new Album();album.play();album.playTrack(1);album.playTrack(1,5);
Album = Album new. album play.album playTrack: 1.album playFromTrack: 1 to: 5.
Smalltak Syntax IIalbum.setName(“Fat of the Land”);String str = album.getName();char ch = str.charAt(0); //starts at 0assert ch == ‘F’;
album name: ‘Fat of the Land’.str := album name.ch := str at: 1. “starts at 1, not 0!”[ ch == $F ] assert.
Semantics
What does it mean?
Smalltak Semantics// Semantics of = is assignment// Semantics of “…” defines a string literal
variable = “Foobar”; assert variable == null; //blahblah
“Semantics of = is comparison”“Semantics of “…” defines a comment”
variable := ‘Foobar’.[ variable = nil ] assert. “blahblah”
Closure, more examples
Closure syntaxdiff := [ :a :b | Transcript show: a. Transcript show: b. (a - b) abs “closure return the result “of the last expression.” ].
x := 23;y := 42;Z := diff value: x value: y.
Sort I
List<String> list = /* some code here */;Collections.sort(list, new Comparable<String>() { public int compare(String a, String b) { return o1.length() - o2.length();
}); }
Sort II
List<String> list = /* some code here */;Collections.sort(list, { String a, String b | return o1.length() - o2.length() });
Sort III
list = “some code here”. List sort: [ :a :b | a size <= b size ].
Iterate collectionfor (Object each : list) { System.out.println(each);}
list.do({ Object each | System.out.println(each) });
list do: [ :each | Transcript show: each ].
Sequence Diagram:Client a :Arrayb :Closure
b value: a1
a do: b
b value: a2
b value: a3
b value: an
Times repeatfor (Integer n = 0; n < 10; n++) { System.out.println(“foo”);}
10.timesRepeat({ System.out.println(“foo”) });
10 timesRepeat: [ Transcript show: ‘foo’ ].
For loopfor (Integer n = 0; n < 10; n++) { System.out.println(n+1);}
0.loop(10,{ int each | System.out.println(each+1) });
1 to: 10 do: [ :each | Transcript show: each ].
Branchingif (game.hasWinner()) { System.out.prinrtln(game.getWinner());}
game.hasWinner().ifTrue({ System.out.println(game.getWinner()) });
game hasWinner ifTrue: [ Transcript show: game winner ].
Class Diagram
TrueifTrue: aClosure ^aClosure value
FalseifTrue: aClosure ^nil
BooleanifTrue: aClosure “abstract”
For loopint sum = 1;for (int each : numbers) { sum += each;}
numbers.map(0,{ int sum, int each | sum + each });
numbers inject: 0 into: [ :sum :each | sum + each ].
Closures and Collections
Collection >> do:
Collection >> allSatisfy:Collection >> anySatisfy:Collection >> contains:Collection >> inject:into: Collection >> detect:Collection >> select:Collection >> reject:
ArrayedCollection >> sort:
Closures and Flow Control
Boolean >> ifTrue:Boolean >> ifFalse:Boolean >> ifTrue:ifFalse:
Integer >> timesRepeat:Integer >> to:do:
BlockClosure >> whileTrue:BlockClosure >> whileFalse:
More Smalltalk
Smalltalk’s family tree
Family tree
Objects, Garbage Collection, Byte code, etc…
Bit Blitting
Overlapping windows
Model-View-Controller
Design Patterns
Refactoring Browser
Unit Testing
Collection framework
Extreme programming
JIT compilation
1st Apple Prototype
Eclipse IDE
Java, Self, JavaScript
1st wiki community
And some name dropping
• Alan Kay• Dan Ingalls• Adele Goldberg• Ted Kaehler• Scott Wallance• John Brandt• Don Roberts
• Kent Beck• Gilad Bracha• Ward Cunningham• Erich Gamma• Ron Jeffries• Martin Fowler• Ralph Johnson
Pure Object-Orientation
• Pure object oriented language
• Everything is an object– including primitive values
• Everything is a message send– including the control flow
Persistent Object Memory
• Smalltalk is language and environment
• Everything inside the image– persistent objects– fully reflective system– incremental compilation– “hot debugging”
Virtual machine
• Compiled code, ie byte code• Source code• Objects• Classes• IDE• Application
*.im file, the image
Squeak ScreenshotQuickTime™ and a
TIFF (Uncompressed) decompressorare needed to see this picture.
Hands-on demo
Postoffice exercise in Smalltalk