Pharo by Example

(0 reviews)

star01star02star03star04star05

Andrew Black, Portland State University
Stéphane Ducasse, INRIA Lille
Oscar Nierstrasz, University of Bern
Damien Pollet, University of Lille

Pub Date: 2010

ISBN 13: 978-3-9523341-4-0

Publisher: Independent

Read This Book

Conditions of Use

Attribution-ShareAlike
CC BY-SA

Reviews

  All reviews are licensed under a CC BY-ND license.

Learn more about reviews.

There are no reviews for this book






Table of Contents

Preface ix

I Getting Started

1 A quick tour of Pharo 3
1.1 Getting started . . . . . . . . . . . . . . . . . . 3
1.2 TheWorld menu . . . . . . . . . . . . . . . . . 7
1.3 Sending messages. . . . . . . . . . . . . . . . . 8
1.4 Saving, quitting and restarting a Pharo session . . . . . . 9
1.5 Workspaces and Transcripts . . . . . . . . . . . . . 11
1.6 Keyboard shortcuts . . . . . . . . . . . . . . . . 12
1.7 The Class Browser . . . . . . . . . . . . . . . . 15
1.8 Finding classes . . . . . . . . . . . . . . . . . . 16
1.9 Finding methods . . . . . . . . . . . . . . . . . 18
1.10 Defining a new method. . . . . . . . . . . . . . . 20
1.11 Chapter summary. . . . . . . . . . . . . . . . . 25

2 A first application 27
2.1 The Lights Out game. . . . . . . . . . . . . . . . 27
2.2 Creating a new Package . . . . . . . . . . . . . . 28
2.3 Defining the class LOCell . . . . . . . . . . . . . . 29
2.4 Adding methods to a class. . . . . . . . . . . . . . 31
2.5 Inspecting an object . . . . . . . . . . . . . . . . 33
2.6 Defining the class LOGame . . . . . . . . . . . . . 34
2.7 Organizing methods into protocols . . . . . . . . . . 37
2.8 Let’s try our code . . . . . . . . . . . . . . . . . 40
2.9 Saving and sharing Smalltalk code. . . . . . . . . . . 43
2.10 Chapter summary. . . . . . . . . . . . . . . . . 47

3 Syntax in a nutshell 49
3.1 Syntactic elements . . . . . . . . . . . . . . . . 49
3.2 Pseudo-variables . . . . . . . . . . . . . . . . . 52
3.3 Message sends . . . . . . . . . . . . . . . . . . 53
3.4 Method syntax . . . . . . . . . . . . . . . . . . 54
3.5 Block syntax . . . . . . . . . . . . . . . . . . . 55
3.6 Conditionals and loops in a nutshell . . . . . . . . . . 56
3.7 Primitives and pragmas . . . . . . . . . . . . . . 58
3.8 Chapter summary. . . . . . . . . . . . . . . . . 58

4 Understanding message syntax 61
4.1 Identifying messages . . . . . . . . . . . . . . . 61
4.2 Three kinds of messages . . . . . . . . . . . . . . 63
4.3 Message composition . . . . . . . . . . . . . . . 65
4.4 Hints for identifying keyword messages . . . . . . . . 72
4.5 Expression sequences . . . . . . . . . . . . . . . 73
4.6 Cascaded messages . . . . . . . . . . . . . . . . 74
4.7 Chapter summary. . . . . . . . . . . . . . . . . 74

II Developing in Pharo

5 The Smalltalk object model 79
5.1 The rules of the model . . . . . . . . . . . . . . . 79
5.2 Everything is an Object . . . . . . . . . . . . . . . 79
5.3 Every object is an instance of a class . . . . . . . . . . 80
5.4 Every class has a superclass . . . . . . . . . . . . . 87
5.5 Everything happens by sending messages . . . . . . . . 91
5.6 Method lookup follows the inheritance chain . . . . . . . 92
5.7 Shared variables . . . . . . . . . . . . . . . . . 98
5.8 Chapter summary. . . . . . . . . . . . . . . . . 103

6 The Pharo programming environment 105
6.1 Overview . . . . . . . . . . . . . . . . . . . . 106
6.2 The Browser . . . . . . . . . . . . . . . . . . . 107
6.3 Monticello . . . . . . . . . . . . . . . . . . . 119
6.4 The Inspector and the Explorer . . . . . . . . . . . . 126
6.5 The Debugger . . . . . . . . . . . . . . . . . . 128
6.6 The Process Browser . . . . . . . . . . . . . . . . 137
6.7 Finding methods . . . . . . . . . . . . . . . . . 138
6.8 Change sets and the Change Sorter . . . . . . . . . . 138
6.9 The File List Browser . . . . . . . . . . . . . . . 141
6.10 In Smalltalk, you can’t lose code . . . . . . . . . . . 143
6.11 Chapter summary. . . . . . . . . . . . . . . . . 144

7 SUnit 147
7.1 Introduction . . . . . . . . . . . . . . . . . . . 147
7.2 Why testing is important . . . . . . . . . . . . . . 148
7.3 What makes a good test? . . . . . . . . . . . . . . 149
7.4 SUnit by example . . . . . . . . . . . . . . . . . 150
7.5 The SUnit cook book. . . . . . . . . . . . . . . . 154
7.6 The SUnit framework . . . . . . . . . . . . . . . 155
7.7 Advanced features of SUnit . . . . . . . . . . . . . 158
7.8 The implementation of SUnit. . . . . . . . . . . . . 159
7.9 Some advice on testing . . . . . . . . . . . . . . . 162
7.10 Chapter summary. . . . . . . . . . . . . . . . . 163

8 Basic Classes 165
8.1 Object . . . . . . . . . . . . . . . . . . . . . 165
8.2 Numbers . . . . . . . . . . . . . . . . . . . . 174
8.3 Characters . . . . . . . . . . . . . . . . . . . 177
8.4 Strings . . . . . . . . . . . . . . . . . . . . . 178
8.5 Booleans . . . . . . . . . . . . . . . . . . . . 179
8.6 Chapter summary. . . . . . . . . . . . . . . . . 181

9 Collections 183
9.1 Introduction . . . . . . . . . . . . . . . . . . . 183
9.2 The varieties of collections. . . . . . . . . . . . . . 184
9.3 Implementations of collections . . . . . . . . . . . . 186
9.4 Examples of key classes. . . . . . . . . . . . . . . 188
9.5 Collection iterators . . . . . . . . . . . . . . . . 197
9.6 Some hints for using collections . . . . . . . . . . . . 201
9.7 Chapter summary. . . . . . . . . . . . . . . . . 202

10 Streams 205
10.1 Two sequences of elements . . . . . . . . . . . . . 205
10.2 Streams vs. collections . . . . . . . . . . . . . . . 206
10.3 Streaming over collections. . . . . . . . . . . . . . 207
10.4 Using streams for file access . . . . . . . . . . . . . 215
10.5 Chapter summary. . . . . . . . . . . . . . . . . 217

11 Morphic 219
11.1 The history of Morphic . . . . . . . . . . . . . . . 219
11.2 Manipulating morphs . . . . . . . . . . . . . . . 221
11.3 Composing morphs . . . . . . . . . . . . . . . . 222
11.4 Creating and drawing your own morphs . . . . . . . . 222
11.5 Interaction and animation . . . . . . . . . . . . . . 226
11.6 Interactors . . . . . . . . . . . . . . . . . . . 229
11.7 Drag-and-drop . . . . . . . . . . . . . . . . . . 230
11.8 A complete example . . . . . . . . . . . . . . . . 232
11.9 More about the canvas . . . . . . . . . . . . . . . 236
11.10 Chapter summary. . . . . . . . . . . . . . . . . 237

12 Seaside by Example 239
12.1 Why do we need Seaside? . . . . . . . . . . . . . . 239
12.2 Getting started . . . . . . . . . . . . . . . . . . 240
12.3 Seaside components . . . . . . . . . . . . . . . . 244
12.4 Rendering XHTML . . . . . . . . . . . . . . . . 248
12.5 CSS: Cascading style sheets . . . . . . . . . . . . . 254
12.6 Managing control flow . . . . . . . . . . . . . . . 256
12.7 A complete tutorial example . . . . . . . . . . . . . 263
12.8 A quick look at AJAX . . . . . . . . . . . . . . . 269
12.9 Chapter summary. . . . . . . . . . . . . . . . . 272

III Advanced Pharo
13 Classes and metaclasses 277
13.1 Rules for classes and metaclasses . . . . . . . . . . . 277
13.2 Revisiting the Smalltalk object model. . . . . . . . . . 278
13.3 Every class is an instance of a metaclass . . . . . . . . . 280
13.4 The metaclass hierarchy parallels the class hierarchy . . . . 281
13.5 Every metaclass Inherits from Class and Behavior . . . . . . 283
13.6 Every metaclass is an instance of Metaclass . . . . . . . . 286
13.7 The metaclass of Metaclass is an Insta

About the Book

Pharo is a modern open-source development environment for the classic Smalltalk-80 programming language. Despite being the first purely object-oriented language and environment, Smalltalk is in many ways still far ahead of its successors in promoting a vision of an environment where everything is an object, and anything can change at run-time.

Pharo by Example, intended for both students and developers, will guide you gently through the Pharo language and environment by means of a series of examples and exercises. The book helps you get started with A Quick Tour of Pharo and guides you through A First Application. The Smalltalk language is introduced in the chapters Syntax in a Nutshell and Understanding Message Syntax.

Development with Pharo is introduced in The Smalltalk Object Model, The Pharo Programming Environment and SUnit. Several of the key classes are presented in chapters on Basic Classes, Collections, Streams and Morphic. A chapter dedicated to Seaside by Example introduces web application development with the popular Seaside framework.

The first edition of the book concludes with chapters on Classes and Metaclasses, Reflection and Frequently Asked Questions.

About the Contributors

Author(s)

Andrew P. Black, Professor, Department of Computer Science, Portland State University. D.Phil., Programming Research Group, University of Oxford (England). 

Stéphane Ducasse is the research director at INRIA Lille leading the RMoD Team.

Oscar Nierstrasz is Professor of Computer Science at the Institute of Computer Science (IAM) of the University of Bern, where he founded the Software Composition Group in 1994. He is co-author of over 200 publications and co-author of the open-source books Object-Oriented Reengineering Patterns and Pharo by Example.

Damien Pollet, associate professor (Maître de conférences) of the UFR IEEA at University of Lille 1 since september 2008, and affiliated with the Laboratoire d’Informatique Fondamentale de Lille (LIFL), the joint laboratory between Lille 1, Lille 3, CNRS, and Inria. Pollet teaches programming and software engineering at Telecom Lille 1, and research on programming languages and reengineering within the RMoD project-team of Inria.