The Taste of Smalltalk Stphane Ducasse Stephane Ducasseunivsavoie

  • Slides: 30
Download presentation
The Taste of Smalltalk Stéphane Ducasse Stephane. Ducasse@univ-savoie. fr http: //www. listic. univ-savoie. fr/~ducasse/

The Taste of Smalltalk Stéphane Ducasse Stephane. Ducasse@univ-savoie. fr http: //www. listic. univ-savoie. fr/~ducasse/ S. Ducasse 1

License: CC-Attribution-Share. Alike 2. 0 http: //creativecommons. org/licenses/by-sa/2. 0/ S. Ducasse 2

License: CC-Attribution-Share. Alike 2. 0 http: //creativecommons. org/licenses/by-sa/2. 0/ S. Ducasse 2

Goals • Two examples: • “hello world” • a LAN simulator • To give

Goals • Two examples: • “hello world” • a LAN simulator • To give you an idea of: • the syntax • the elementary objects and classes • the environment • To provide the basis for all the lectures: • all the code examples, • constructs, • design decisions, . . . S. Ducasse 3

An Advice You do not have to know everything!!! • “Try not to care

An Advice You do not have to know everything!!! • “Try not to care - Beginning Smalltalk programmers often have trouble because they think they need to understand all the details of how a thing works before they can use it. This means it takes quite a while before they can master Transcript show: ‘Hello World’. One of the great leaps in OO is to be able to answer the question "How does this work? " with "I don’t care"“. Alan Knight. Smalltalk Guru • We will show you how to learn and find your way S. Ducasse 4

Some Conventions • • • S. Ducasse Return Values • • 1 + 3

Some Conventions • • • S. Ducasse Return Values • • 1 + 3 -> 4 Node new -> a. Node Method selector #add: Method scope conventions Instance Method defined in class Node: • Node>>accept: a. Packet Class Method defined in class Node (in the class of the class Node) • Node class>>with. Name: a. Symbol a. Something is an instance of the class Something 5

Roadmap • “hello world” • Syntax • a LAN simulator S. Ducasse 6

Roadmap • “hello world” • Syntax • a LAN simulator S. Ducasse 6

Hello World Transcript show: ‘hello world’ • At anytime we can dynamically ask the

Hello World Transcript show: ‘hello world’ • At anytime we can dynamically ask the system to evaluate an expression. To evaluate an expression, select it and with the middle mouse button apply do. It. • Transcript is a special object that is a kind of standard output. • It refers to a Text. Collector instance associated with the launcher. S. Ducasse 7

Transcript show: ‘hello world’ S. Ducasse 8

Transcript show: ‘hello world’ S. Ducasse 8

Everything is an Object The workspace is an object. The window is an object:

Everything is an Object The workspace is an object. The window is an object: it is an instance of Application. Window. The text editor is an object: it is an instance of Paragraph. Editor. The scrollbars are objects too. ‘hello word’ is an object: it is a. String instance of String. #show: is a Symbol that is also an object. The mouse is an object. The parser is an object: instance of Parser. The compiler is also an object: instance of Compiler. The process scheduler is also an object. The garbage collector is an object: instance of Memory. Object. Smalltalk is a consistent, uniform world written in itself. You can learn how it is implemented, you can extend it or even modify it. All the code is available and readable S. Ducasse 9

Smalltalk Object Model • ***Everything*** is an object ⇒ Only message passing ⇒ Only

Smalltalk Object Model • ***Everything*** is an object ⇒ Only message passing ⇒ Only late binding • • • Instance variables are private to the object Methods are public Everything is a pointer Garbage collector Single inheritance between classes Only message passing between objects S. Ducasse 10

Roadmap • • • S. Ducasse Hello World First look at the syntax LAN

Roadmap • • • S. Ducasse Hello World First look at the syntax LAN Simulator 11

Complete Syntax on a Post. Card example. With. Number: x “Illustrates every part of

Complete Syntax on a Post. Card example. With. Number: x “Illustrates every part of Smalltalk method syntax. It has unary, binary, and key word messages, declares arguments and 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. ” |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 S. Ducasse 12

Yes if. True: is sent to a boolean Weather is. Raining if. True: [self

Yes if. True: is sent to a boolean Weather is. Raining if. True: [self take. My. Umbrella] if. False: [self take. My. Sunglasses] if. True: if. False is sent to an object: a boolean! S. Ducasse 13

Yes a collection is iterating on itself #(1 2 -4 -86) do: [: each

Yes a collection is iterating on itself #(1 2 -4 -86) do: [: each | Transcript show: each abs print. String ; cr ] >1 >2 >4 > 86 Yes we ask the collection object to perform the loop on itself S. Ducasse 14

Do. It, Print. It, Inspect. It and Accept • Accept = Compile: Accept a

Do. It, Print. It, Inspect. It and Accept • Accept = Compile: Accept a method or a class • • • definition Do. It: send a message to an object Print. It: send a message to an object + print the result (#print. On: ) Inspect. It: send a message to an object + inspect the result (#inspect) S. Ducasse 15

Objects send messages • • S. Ducasse Transcript show: ‘hello world’ The above expression

Objects send messages • • S. Ducasse Transcript show: ‘hello world’ The above expression is a message • • the object Transcript is the receiver of the message the selector of the message is #show: one argument: a string ‘hello world’ Transcript is a global variable (starts with an uppercase letter) that refers to the Launcher’s report part. 16

Vocabulary Point Message passing or sending a message is equivalent to invoking a method

Vocabulary Point Message passing or sending a message is equivalent to invoking a method in Java or C++ calling a procedure in procedural languages applying a function in functional languages of course the last two points must be considered under the light of polymorphism S. Ducasse 17

Roadmap • • • S. Ducasse Hello World First look at the syntax LAN

Roadmap • • • S. Ducasse Hello World First look at the syntax LAN Simulator 18

A LAN Simulator A LAN contains nodes, workstations, printers, file servers. Packets are sent

A LAN Simulator A LAN contains nodes, workstations, printers, file servers. Packets are sent in a LAN and each node treats them differently. S. Ducasse 19

Three Kinds of Objects Node and its subclasses represent the entities that are connected

Three Kinds of Objects Node and its subclasses represent the entities that are connected to form a LAN. Packet represents the information that flows between Nodes. Network. Manager manages how the nodes are connected S. Ducasse 20

LAN Design S. Ducasse 21

LAN Design S. Ducasse 21

Interactions Between Nodes S. Ducasse 22

Interactions Between Nodes S. Ducasse 22

Node and Packet Creation |mac. Node pc. Node node 1 printer. Node node 2

Node and Packet Creation |mac. Node pc. Node node 1 printer. Node node 2 node 3 packet| mac. Node : = Workstation with. Name: #mac. pc. Node : = Workstation with. Name: #pc. node 1 : = Node with. Name: #node 1. node 2 : = Node with. Name: #node 2. node 3 : = Node with. Name: #node 2. printer. Node : = Printer with. Name: #lpr. mac. Node next. Node: node 1 next. Node: pc. Node next. Node: node 2. node 3 next. Node: printer. Node. lpr next. Node: mac. Node. packet : = Packet send: 'This packet travelled to' to: #lpr. S. Ducasse 23

Objects Send Messages Message: 1 + 2 receiver : 1 (an instance of Small.

Objects Send Messages Message: 1 + 2 receiver : 1 (an instance of Small. Integer) selector: #+ arguments: 2 Message: lpr next. Node: mac. Node receiver: lpr (an instance of Lan. Printer) selector: #next. Node: arguments: mac. Node (an instance of Workstation) Message: Packet send: 'This packet travelled to' to: #lpr receiver: Packet (a class) selector: #send: to: arguments: 'This packet travelled to' and #lpr S. Ducasse 24

Transmitting a Packet | a. Lan packet mac. Node|. . . mac. Node :

Transmitting a Packet | a. Lan packet mac. Node|. . . mac. Node : = a. Lan find. Node. With. Address: #mac. packet : = Packet send: 'This packet travelled to the printer' to: #lpr. mac. Node originate: packet. -> mac sends a packet to pc -> pc sends a packet to node 1 -> node 1 sends a packet to node 2 -> node 2 sends a packet to node 3 -> node 3 sends a packet to lpr -> lpr is printing -> this packet travelled to lpr S. Ducasse 25

How to Define a Class? Smalltalk define. Class: #Packet superclass: #{Object} indexed. Type: #none

How to Define a Class? Smalltalk define. Class: #Packet superclass: #{Object} indexed. Type: #none private: false instance. Variable. Names: 'addressee originator contents' class. Instance. Variable. Names: '' imports: '' category: 'LAN' S. Ducasse 26

How to Define a Class? Name. Of. Superclass subclass: #Name. Of. Class instance. Variable.

How to Define a Class? Name. Of. Superclass subclass: #Name. Of. Class instance. Variable. Names: 'inst. Var. Name 1' class. Variable. Names: 'class. Var. Name 1' pool. Dictionaries: '' category: 'LAN' S. Ducasse 27

How to Define a Method? message selector and argument names "comment stating purpose of

How to Define a Method? message selector and argument names "comment stating purpose of message" | temporary variable names | statements accept: the. Packet "If the packet is addressed to me, print it. Otherwise just behave like a normal node. " (the. Packet is. Addressed. To: self) if. True: [self print: the. Packet] if. False: [super accept: the. Packet] S. Ducasse 28

In Java • In Java we would write void accept(the. Packet) /*If the packet

In Java • In Java we would write void accept(the. Packet) /*If the packet is addressed to me, print it. Otherwise just behave like a normal node. */ if (the. Packet. is. Addressed. To(this)){ this. print(the. Packet)} else super. accept(the. Packet)} S. Ducasse 29

Summary What is a message? What is the message receiver? What is the method

Summary What is a message? What is the message receiver? What is the method selector? How to create a class? How to define a method? S. Ducasse 30