Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Object-oriented Programming with Smalltalk
Object-oriented Programming with Smalltalk
Object-oriented Programming with Smalltalk
Ebook848 pages9 hours

Object-oriented Programming with Smalltalk

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Object oriented programming is a way of thinking about problems. Smalltalk is one of the purest incarnations of an object-oriented programming language. Using a pedagogical approach, this book covers all aspects of object oriented programming: first through the study of various preexisting Smalltalk classes, their implementation and use; then through a detailed description of an implementation of an interactive Lindenmayer system and through implementation of a series of calculators.

The author addresses such subjects as graphics programming, dependency mechanisms and hierarchical specialization.

This book fills the gap for an in-depth self-study reference, permitting the reader to master all aspects of object-oriented programming through a large set of exercises with highly detailed resources.

  • Downloadable software content for practice applications
  • Covers all aspects of Smalltalk: theconcepts of primitive objects, classes and instances, static and dynamic inheritance and methods, as well as graphical programming, the dependency mechanisms and the handling of exceptions
  • Features in-depth studies of two programming projects and annotated solutions to all exercies and appendices
LanguageEnglish
Release dateNov 26, 2015
ISBN9780081008089
Object-oriented Programming with Smalltalk

Related to Object-oriented Programming with Smalltalk

Related ebooks

Programming For You

View More

Related articles

Reviews for Object-oriented Programming with Smalltalk

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Object-oriented Programming with Smalltalk - Harald Wertz

    Object-oriented Programming with Smalltalk

    Harald Wertz

    Table of Contents

    Cover image

    Title page

    Copyright

    Preface

    Program source

    Acknowledgements

    Introduction

    I.1 Everything is an object

    I.2 Object-oriented languages

    Part 1: The Basics of Smalltalk Programming

    1: A Brief Tour of Squeak

    Abstract

    1.1 The first contact

    1.2 Important points

    1.3 Exercises

    2: The First Program

    Abstract

    2.1 Defining new classes

    2.2 Defining new methods

    2.3 Program test

    2.4 Adding methods to subclasses of the Animal class

    2.5 Modification of Parrot behavior

    2.6 Exercises

    2.7 self and super

    2.8 Exercises

    3: Architecture and Inheritance

    Abstract

    3.1 Smalltalk software architecture

    3.2 Static and dynamic inheritance

    4: Some Elements of Squeak Syntax Grammar

    Abstract

    4.1 Pseudo-variables

    4.2 Comments and identifiers

    4.3 Literals

    4.4 Variables

    4.5 Messages

    4.6 Control structure

    4.7 Exception handling

    4.8 Exercises

    Part 2: Programming in Squeak

    5: Drawings Like in Logo

    Abstract

    5.1 The Pen class

    5.2 Some fractals: recursive drawings

    5.3 Exercises

    5.4. A restructuring of our Logo programs

    5.5 A user interface for Logo drawings

    5.6. Lindenmayer systems

    6: The Dependency Mechanism

    Abstract

    6.1 Basics of the dependency mechanism

    6.2 Programming a calculator

    6.3 Exercises

    6.4 A KCalc calculator

    6.5 Exercises

    6.6 Concluding remarks

    Part 3: Appendices

    Appendix 1: Solutions to Exercises

    A1.1 Exercises from Section 1.3

    A1.2 Exercises from section 2.6

    A1.3 Exercises for section 2.8

    A1.4 Exercises from section 4.3.6

    A1.5 Exercises for section 4.4.9

    A1.6 Exercises from section 4.8

    A1.7 Exercises for section 5.3

    A1.8 Exercises for section 5.6.6

    A1.9 Exercises for section 6.3

    A1.10 Exercises for section 6.5

    Appendix 2: List of Keyboard Shortcuts

    A2.1 Editing shortcuts

    A2.2 Search shortcuts

    A2.3 Cancel/accept shortcuts

    A2.4 Browser, inspector and explorer shortcuts

    A2.5 Shortcuts for parentheses, square brackets, curly brackets

    A2.6 Conversion shortcuts

    A2.7 Code completion shortcuts

    A2.8 Formatting shortcuts

    Appendix 3: Smalltalk Syntax Specification

    A3.1 Base characters

    A3.2 Constants

    A3.3 Expressions

    Appendix 4: List of Squeak Primitives

    A4.1 Arithmetic and logic primitives

    A4.2 Access primitives

    A4.3 Object creation primitives

    A4.4 Execution primitives

    A4.5 Process management primitives

    A4.6 Input/output primitives

    A4.7 Search and replace primitives

    A4.8 And the others

    Bibliography

    Index

    Copyright

    First published 2015 in Great Britain and the United States by ISTE Press Ltd and Elsevier Ltd

    Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the case of reprographic reproduction in accordance with the terms and licenses issued by the CLA. Enquiries concerning reproduction outside these terms should be sent to the publishers at the undermentioned address:

    ISTE Press Ltd

    27-37 St George’s Road

    London SW19 4EU

    UK

    www.iste.co.uk

    Elsevier Ltd

    The Boulevard, Langford Lane

    Kidlington, Oxford, OX5 1GB

    UK

    www.elsevier.com

    Notices

    Knowledge and best practice in this field are constantly changing. As new research and experience broaden our understanding, changes in research methods, professional practices, or medical treatment may become necessary.

    Practitioners and researchers must always rely on their own experience and knowledge in evaluating and using any information, methods, compounds, or experiments described herein. In using such information or methods they should be mindful of their own safety and the safety of others, including parties for whom they have a professional responsibility.

    To the fullest extent of the law, neither the Publisher nor the authors, contributors, or editors, assume any liability for any injury and/or damage to persons or property as a matter of products liability, negligence or otherwise, or from any use or operation of any methods, products, instructions, or ideas contained in the material herein.

    For information on all our publications visit our website at http://store.elsevier.com/

    © ISTE Press Ltd 2015

    The rights of Harald Wertz to be identified as the author of this work have been asserted by him in accordance with the Copyright, Designs and Patents Act 1988.

    British Library Cataloguing-in-Publication Data

    A CIP record for this book is available from the British Library

    Library of Congress Cataloging in Publication Data

    A catalog record for this book is available from the Library of Congress

    ISBN 978-1-78548-016-4

    Printed and bound in the UK and US

    Preface

    In order to learn how to program objects, we must first get to know them. For this, we need to know the programming. This results in an infernal loop which can make learning an object-oriented language very painful.

    With its intuitive presentation of topics, this book provides a way out of this loop. Being based on mini- and life-sized projects, it offers the support needed for acquiring the foundations and the know-how of object-oriented programming in an easy and, hopefully, stimulating way.

    It represents a summary of a decade of teaching an introductory course on object-oriented programming. After this course, a student would have solid general programming foundations and the ability to design, develop and implement his own object-oriented programming projects. We use the language Smalltalk through its dialect Squeak, chosen because of the pure object presentation, the power of its environment, the notable amount of sample programs available (the whole Squeak system is written in the Squeak language) and ultimately its open-source nature on all platforms. Effectively, Squeak works identically on computers of all types and operating systems, ranging from Windows, Apple-OS, Linux or others, to even mobile devices.

    With the exception of Chapter 3, each chapter is organized as a course accompanied with tutorials and practical exercises. The contents of this book cannot be fully absorbed without the tutorials and without solving the practical problems. In this case, the ability to solve the problems translates into the ability to design and implement programs.

    The course parts of this book are Chapter 1–Chapter 6.

    The tutorials are all in Appendix 1.

    In order to learn, you must practise. It is true that to best learn a foreign language, you have to live where this language is practiced on a daily basis. The same applies for learning a programming language, where you have to live in the system using this specific language. Chapter 1 explains how to acquire a copy of the Squeak system suitable for creating a place where you can live a life of object-oriented programming. Using the Squeak system while reading this book is essential for understanding the text.

    This constant use of the Squeak system provides the practical exercises which become crucial throughout the chapters all the way to Appendix 1. These are not discussed in this book and are the responsibility of the readers.

    How can you do this? At first, you must familiarize yourself with the wonderfully distinct Squeak interface by interactively exploring and playing with it.

    Then upon encountering a Smalltalk expression that does not seem explicit, a computer check is needed. Even if it seems clear, verify it on your computer.

    After reading each exercise description, stop and take the time to solve and implement it. The solution will not work right away. Have no fear, correcting and improving programs is part of a programmer’s daily routine, so it is worth getting used to it when taking your first steps in programming. It is important to go beyond the simple framework of the exercise, as there is more than one possible solution. Finding several solutions is always more educational than finding just one. After enough practice, programming becomes a pleasant activity, and even a passionate one. The more time you spend on it, the faster you will become a good programmer. Only after finding your own solutions, are you encouraged to examine the solutions proposed in Appendix 1. All exercises in this book have a solution illustrated in the book. These solutions, these tutorials, make up an integral part of the chapter in which these exercises have been proposed. Continuing reading becomes impossible without previously studying and implementing the solutions. Often, we will tackle concepts, notions or additional techniques necessary for understanding the subsequent text. The alternation between reading courses and consulting the exercise solutions, created by separating the course materials and the exercise solutions, in a way reflects the need for personal research from the beginning.

    Throughout this book, we have tried to present new features, new techniques and new concepts in a context where their use would be necessary, thus facilitating their understanding. Moreover, throughout the chapters, these same concepts, techniques and functionalities will reappear in various situations. This should improve the understanding of these concepts.

    This book is divided into three parts. The first part, composed of Chapters 1–4, presents the foundations for object-oriented programming and Smalltalk. It familiarizes the readers with the concepts such as class and instance, message passing, as well as static and dynamic inheritance. We also explore the most important Smalltalk classes with their most used methods. More precisely, the first chapter presents the Squeak system and the second chapter introduces, in an intuitive manner, object-oriented programming. After a brief presentation of the software architecture and the heritage mechanisms in Chapter 3, the very long Chapter 4 goes through a complete tour of the syntax and grammar aspects of Smalltalk.

    From Chapter 5, we enter the second part, which will deepen the understanding of programming techniques learned in the first part using examples of complete Squeak programs. This introduces graphical user interface (GUI) programming for developing a set of programs for producing line drawings.

    Chapter 6 presents the fundamental mechanism of dependencies by programming several calculators.

    To make the most out of this book around which the course is structured, our advice is to begin with the first three chapters and then possibly some sections of Chapter 4. Then, continue with those in the second part that addresses programming in the Logo style and L-systems (visualizing drawings is much more appealing than examining dull strings of characters), the use of dependencies and the process coordination.

    Chapter 4 then serves as a vade mecum for explaining and deepening the understanding of classes and methods encountered while developing the programs.

    If this book has created the desire for you to program objects in Squeak, it has served its role perfectly.

    Happy reading and happy compiling!

    Program source

    The programs found in the second part of this book are available on my Website http://www.ai.univ-paris8.fr/˜hw. After downloading, you should place them in the directory where you keep your Squeak programs. Then, just open the File List and load them (using the load them button) into your Squeak image.

    Acknowledgements

    This book could not have been written without the help of my colleagues, friends and students. I want to thank everyone, particularly Pierre Audibert who discovered a countless number of shells and helped with my Germanic style text; Françoise Balmas read the text in an attempt to understand it and thus was able to identify faults from an explanatory and educational perspective; Vincent Boyer for carefully reading and correcting my graphical programming vocabulary; Christian Fer, Julien Dervaux, Hubert Dupont and Serge Stinckwich all worked as additional correctors and each of them brought notable improvements; Jo Arditty and Marc Riesacher who helped me to find the appropriate words when I did not have more than foreign phrases in my head; Stephane Ducasse who asked me some great questions and whose relevant criticisms led me to actually rewrite parts of the text. Patrick Greussay, Jean-François Perrot, Gilles Bernard and Jym Feat have all proved their friendship to me and have inspired me throughout my CS career: through their discussions, courses and criticisms.

    Of course, this book would have never been possible without the continuous questioning from my students and without their enthusiasm for object-oriented programming.

    Like any book that develops in parallel with a course, its content is strongly influenced by the media used. My first object-oriented programming course was based on the system called LittleSmalltalk developed by Timothy Budd. His influence can be felt throughout this book. We thank him for making available a Smalltalk microsystem that made it possible to easily understand its functioning and its implementation, and was compatible with the small machines that we had available at that time.

    The idea of the calculators in Chapter 7 belongs to Philippe Krief, who used the programming of such calculators in his course on Smalltalk within our CS department.

    Of course, the vast resources found on the Internet equally contributed to the content of this book. In particular, the Websites on the Squeak language http://www.squeak.org/ and http://minnow.cc.gatech.edu/Squeak, which any reader should consult regularly.

    And a thank you also goes out to Fares Belhadj who graciously provided me with the wonderful image of the palm tree at the end of section A.8.

    Harald Wertz

    September 2015

    Introduction

    All programs manipulate a certain number of objects (numbers, trees, lists, tables, objects, etc.). These programmatic objects correspond, more or less, to the imaginary objects which the programmer has in his head and which are representations of the real world or the world of computations, one as real as the other.

    The power of object-oriented programming lies in proposing a definition of the programmatic objects that allows pasting them close to the imaginary objects.

    Note that we must distinguish between the two meanings of the word object: in the technical sense of object-oriented programming (defined further) and the ordinary sense: objects of discourse. The charm of object-oriented programming lies exactly in the simplicity in which one passes from one to the other¹.

    I.1 Everything is an object

    − The fundamental idea of object-oriented programming is to systematically consider complex entities which, for each of the beings appearing in the program, regroup the data structure that describes them with the procedures that allow their manipulation. These entities are naturally called the objects of the program. The structure of each of these entities is specified by a class: it can be said that the object is an instance of its class.

    − We can overlook the role of the class and only consider the object, without questioning its origins. Then, we talk of actors [AGH 97] rather than objects. The frames [MIN 85] form a particular variety of actors, complicated to meet the needs of artifical intelligence. The class/instance distinction and the explicit use of classes is essential when talking about object-oriented programming.

    − We could also have talked of types instead of classes; this essentially comes down to tradition. However, an important distinction must be made regarding the nature of classes: are they objects just like the rest or are they essentially different from other objects? The tradition of describing types, including abstract types, assumes that types are not objects. In contrast, object-oriented programming stricto sensu assumes that classes are objects: everything is an object.

    − Object-oriented programming systematically functions in two levels: class definitions (defining the behavior shared by the instances) and instances creation (defining what distinguishes different instances from one another), while traditional programming restricts and obscures this process by imposing the separation of data and procedures. This intellectual mechanism is not new, we practise it constantly. Mathematics and the structuralist thinking have accustomed us to the approach in two stages: Let’s call that thing an object that is … that has … Clearly thing is an object, etc. In short, this is to define what we will speak about before speaking about it effectively.

    Object-oriented programming is meant to provide particular methods of carrying out this intellectual approach, allowing us to write as we think.

    It is the possibility of directly using prevalent intellectual resources that makes object-oriented programming so powerful – and motivates its success. It allows less skilled programmers to tackle more difficult problems, solve them in less time and produce higher quality programs.

    In conclusion, object-oriented programming is basically programming in a natural way.

    I.2 Object-oriented languages

    Any language proposing the object-oriented programming style can proclaim itself an object-oriented language. Currently, there is a very large number of objectoriented languages. Here is a tentative classification:

    1) The amazing ancestor from which all have descended (see Figure I.1) is Simula-67 [DAH 67], itself derived from Algol-60, [PER 59] and [NAU 63]. All the essentials are there, including the object manual. As the name indicates, this language was created for complex process simulation applications.

    Figure I.1 The S queak family tree

    Simula can be seen as an extension of Algol-60 with added concepts of class (in a more complex form than the current concept) and instance. These concepts were introduced precisely for conveniently simulating the real world. Simula also contains a powerful co-routine mechanism, which (almost) disappeared with its successors.

    2) Classic imperative language extensions have been emerging since approximately 1983, when the fashion of pursuing object-oriented programming began to spread under the influence of Smalltalk [GOL 83, GOL 84] and Flavors [CAN 82]. For example, the language C [KER 88] served as the basis for developing the languages C++ [STR 87] and Objective C [COX 87], and Pascal [JEN 75] was used to develop Object Pascal, [JAC 87] and [SCH 86]. Currently, object extensions exist for everyone in all imperative languages, for example: object-oriented Forth [POU 87], Object Fortran [REE 91] or Object-Oriented Cobol [CHA 96].

    You can regard these as simplified versions of Simula, built on different substrates. Their users are mainly in the field of Software Engineering. These languages are more abstract type languages than object-oriented languages, and should be compared with CLU [LIS 77] and ADA [DOD 83].

    3) There are also LISP [MCC 62] extensions. There is a whole series: Flavors [MOO 80] from the Lisp Symbolics machine, LOOPS [BOB 86] of Xerox PARC and Interlisp, CEYX [HUL 83] and Alcyone [HUL 85] from Le_Lisp, Kool, [ALB 84] and [ALB 88], developed by Bull in Le_Lisp, ObjVlisp [BRI 87] as an extension of Vlisp, CLOS, the CommonLISP Object System [BOB 88], and many others (new ones are developed on a daily basis).

    This level of activity is due to the fact that object-oriented programming has acclimatized well in Lisp due to the incredible plasticity of the language and its cultural position as the main language of artificial intelligence. Indeed, these systems are very often based on complex artificial intelligence software for knowledge representation and the development of expert systems.

    For similar reasons, Prolog [GIA 85] extensions toward the objects multiply: LAP [ILI 87], EMICAT, ObjLlog-II [DUG 88], TAO [TAK 83] and others. We add to all this the more recent scripting languages such as Python [VAN 95], Ruby [MAT 01] and OCaml [LER 07].

    4) Finally, a language which is self-sufficient and directly implementable: Smalltalk, from Xerox PARC. Its spiritual father is Alan Kay and his Flex [KAY 68] machine, whose basic concepts were heavily influenced by the SketchPad program of Ivan Sutherland [SUT 63] and the Logo language of Seymour Papert [PAP 80]. It was developed at Xerox Palo Alto Research Center, which also developed LOOPS.

    The current version is Smalltalk-80. It was preceded by Smalltalk-72 and Smalltalk-76. A very good discussion on the history of Smalltalk is given by Kay [KAY 93]. This may be a small language but it is a big system, largely oriented toward the management of a bit-mapped screen. Initially, Smalltalk was only available for Xerox (1108, 1186) machines, on SUN and the Tektronix 4404 and 4406 computers. Since then versions have appeared for almost all computers and all operating systems. There is even a version for the Sony Playstation.

    Smalltalk has had a profound influence on the development of oriented programming, offering its most accomplished example, and (across Apple’s MacIntosh, developed by members of the original Smalltalk team) on all modern computing. Its conceptual simplicity and the richness of its environment amply justify its study, even if one has no plans to use it later.

    Smaller versions have also been developed, for example: Smalltalk-V by Digitalk or Little Smalltalk [BUD 88] by Timothy Budd. The latter is particularly useful for familiarizing yourself with the implementation of interpreters of objectoriented languages. However, let us consider the last in the Smalltalk family: Squeak [ING 97]. It was developed by a team centered around the original developers of Smalltalk, mainly Alan Kay, Dan Ingalls, Scott Wallace, Ted Kaehler, Jon Maloney, Andreas Raab and Michael Rueger.

    It integrates the latest developments in man/machine interfaces and resimplifies the Smalltalk virtual machine, which, nevertheless, had aged. It is distributed under an Open Source license (have a look at www.squeak.org) and runs on almost all computers. It is through this language that we will discuss object-oriented programming in this text.


    ¹ This passage does not necessarily go both ways: passing from languages to programs is always feasible in practice; the inverse, however, is less assured because of many poorly understood syntax questions related to programming languages.

    Part 1

    The Basics of Smalltalk Programming

    1

    A Brief Tour of Squeak

    Abstract

    We begin by giving a brief scenario for the use of SQUEAK. However, in order for us to truly experience it, we must first acquire an interpreter of this wonderful language. In order to do this, we simply have to proceed to the download page of the Website www.squeak.org to download the necessary files. Currently, there are versions for Windows, OS X and Linux. For installation, follow their advice on installation.

    Keywords

    Indication

    Integer class

    Main menu

    Pen example effect

    Selector

    SQUEAK

    Squeak squeak.image

    System browser

    Upside-down system browser

    Workspace window

    We begin by giving a brief scenario for the use of Squeak. However, in order for us to truly experience it, we must first acquire an interpreter¹ of this wonderful language. In order to do this, we simply have to proceed to the download page of the Website www.squeak.org to download the necessary files. Currently, there are versions for Windows, OS X and Linux. For installation, follow their advice on installation.

    Once the system has been installed, you should run Squeak. On Windows, this can be done by clicking the Squeak icon and then choosing your image, on Unix you activate it with the command squeak squeak.image from the directory containing the three files squeak.image, squeak.changes, SqueakV?.sources (in place of the question mark, there should be the version number). These three files are described in the table in Figure 1.1.

    Figure 1.1 The most important Smalltalk files

    Once Squeak has been launched, you should see a screen similar to that in Figure 1.2.

    Figure 1.2 Squeak (version 4.5) start screen

    When you work in Squeak, everything you do is done through this screen. Squeak is a complete system with its own editors, browsers, debuggers, etc. You will even have a tool for sending and receiving email, celeste, and another one called scamper, which provides us with the basics for browsing the Internet.

    This window is your interface with the Squeak system and the rest of the world. For now, the first thing to do is construct a private virtual image in which you will be able to do whatever you want, and which will guarantee you are always able to find the original image, if you were to get completely lost. In order to do this, place your mouse anywhere in the Squeak window, outside of any subwindows, and click on the left button, this should bring up the main menu shown in Figure 1.3.

    Figure 1.3 Main menu

    Choose save as…, and then in the window New File Name? (see Figure 1.4), which opens automatically, give a name of your choice (such as myImage.image), and then from that moment on, you will be working in your own image, and not the initial image distributed with Squeak. Then in the same menu, choose save or, when you want to quit, save and quit, you will save a personal image, accessible through the name that you have chosen (myImage.image in the example here), and usable in any subsequent Squeak launch.

    Figure 1.4 A name for the image

    Finally, we can begin to explore Squeak.

    1.1 The first contact

    On the start screen (Figure 1.2 page 4), the border of the Smalltalk window is decorated with small colored rectangles: these are the "flaps. By default, there is a flap called Squeak in the middle left of the window called Tools, and in the middle right, and at the bottom of the window there are, from left to right, the flaps Widgets and Supplies. All these flaps, if activated (by the click of the left mouse button), give access to a set of Squeak tools². Figure 1.5, page 7, shows the content of the flap Supplies". In order to activate any of its tools, simply select it (always with the left mouse button³) and pull it out of the flap (keeping the button pressed and by moving the mouse to the inside of the Squeak window).

    Figure 1.5 The graphic object flap (Supplies)

    If we activate the tool Workspace of the flap Tools, we obtain a window similar to the one in Figure 1.6. In this window, we can enter code, select it and run it. Thus, if we enter in it:

    Figure 1.6 A Workspace window

    7 + 4

    and then select in the shortcut menu print it, Squeak will respond quite naturally with 11. Note that instead of selecting print it in the shortcut menu, we could type Alt + p (by pressing) Alt at the same time as p⁴.

    Note also that the expression 7 + 4 is read as: ask object 7 what it gets if we send it the message + with the argument 4.

    Or even clearer: the object 7 responds to the transmission of the message + with argument 4 by returning the object 11. We should also note that all the objects 7, 4 and 11 are integers, more precisely: small integers.

    In short, in Smalltalk, a calculation is always done by sending messages to objects and objects receiving messages respond by sending a response object to the initiator of the message. We will return to this fundamental aspect of object-oriented programming later. For now, recall that in Smalltalk everything is an object and that the objects communicate between themselves by transmitting messages.

    We can surely agree: this first program, 7 + 4, is a very minimalist one. Let us try another one and by using do it (Alt + d) on the expression:

    Display restoreAfter: [Pen example]

    which will draw on our Squeak screen a beautiful spiral as can be seen in Figure 1.7.

    Figure 1.7 The Pen example effect

    Once more: Display restoreAfter: [Pen example] is the writing in Smalltalk for transmitting to the Display object, the Squeak screen, the message restoreAfter: with the argument [Pen example], which itself is a transmission of the message example to the object Pen. Remember this: all Smalltalk calculations are the results of the transmission of a message, possibly with or without arguments. The message example does not have an argument. This is a unary message. On the other hand, the message restoreAfter: ⁵ has one argument (syntactically indicated by the colon). Here it is, in brackets, the Smalltalk code corresponding to the transmission of the message example to the object Pen.

    Note also that the action do it does the same thing as print it, which means: it calculates, it evaluates the selected expression, but unlike print it, it does not print the value of the transmission returned to the emitter (in this case, the returned value would be a Display object).

    A final note on this example: Display and Pen begin with a capital letter. In Smalltalk, which is case sensitive, this means that those words describe classes – objects that are classses or objects known to the whole Smalltalk system. A class is a specific and important object in Smalltalk. A class describes a structure and it is the only object type which is capable of creating other objects upon demand (in general with the message new). It is almost like an object mold. Since in Smalltalk everything is an object, at least now we know where they come from: they are created by objects of the class type. The objects that a class can create are called instances of a class. The classes themselves are instances of other classes: their meta-class. We will return to these concepts in the following chapters. For now, let us remember that:

    – everything is an object;

    – every object is an instance of a class;

    – thus all messages are transmitted to an instance of a class.

    In its current version, the Squeak system puts approximately 3500 already existing classes at our disposal. We can always add new ones, defined so as to suit our needs. It is this activity of defining new classes that will be the main activity of a Smalltalk programmer. But what is a class?

    For now, let us say that they are the mold for producing instances and that this mold must define the characteristics determining the specificity of each instance (we can say that it must define the instance variables) and that it must define the specific messages that each of the instances can understand (we can say that it must define its methods). Thus, if in a program, we need to model cars, we will probably distinguish each car by its power, color, size, etc. These are the instance variables and each car will have its specific values associated with these instance variables. However, all cars will know how to move forward, reverse, accelerate, etc. These are the defined methods of the Car class, and all the specific cars (each instance of the Car class) can look into their class on how to accelerate, reverse, move forward, etc. We say that instances use, a priori, the methods defined in their own class. But, this is not all. Since a program will likely contain many different classes, and since, in any case, the Squeak system provides us with a notable number of classes, these are organized hierarchically in a tree structure, similar to classification systems, which always handle concepts organized in tree-like structures. Thus, we will be speaking not only about classes, but also about subclasses and superclasses. For example, the class Car could have subclasses called StationWagon, Truck, Caravan, Formula-1, etc. which can be distinguished by their power, ability or inability to carry loads, high speeds, the necessary permits, etc. However, they will all reverse, move forward and accelerate. Thus, we can say that the subclasses inherit the behavior of their superclasses. Here, it is the class Car that knows how to move forward, there is no need to repeat this in the classes StationWagon, Truck, Caravan, Bus, etc. They can inherit this knowledge from their superclass (the class Car).

    If, in the main menu (see Figure 1.3) we click on open … and then on browser, or if using the Tools flap we import the icon browser, or (beginning with Squeak version 4) if we select the menu tools from the top left of the screen and then choose this same icon⁶, Squeak gives us a window, a System Browser, a browser for existing classes. We show an example of this in Figure 1.8. This tool allows us to explore the set of Smalltalk classes: those that already existed a priori in the system and those that you have defined yourself. You can see how the classes and their methods have been defined, their hierarchy, their implementation. The whole Smalltalk system has been written in Smalltalk! So, you have an absolutely remarkable set of Smalltalk sample programs to explore. Do not hesitate, launch a system browser (System Browser) and play around with it.

    Figure 1.8 System browser focused on the Number class

    The system browser consists of a first row of subwindows which describe, from left to right:

    – Categories: Smalltalk classes are grouped into categories based on the designer’s ideas for these classes. A category is something conceptual and does not correspond to a particular class hierarchical organization level. Very often, project classes are grouped in a single category named after the project. The category window (see Figure 1.9, menu page 12) gives the opportunity to look for classes conceptually related. In our example, Figure 1.8, page 11, it was the Kernel-Numbers category that, when selected, made the set of classes related to this category appear in the classes subwindow.

    Figure 1.9 The menus in the first row of the subwindows of the system browser, from left to right: categories (1 menu), classes (2 menus), protocols (1 menu) and methods (3 menus)

    – Classes: in this drop-down menu, we find sets of classes belonging to a particular category. Just click on the name of the desired class for its definition to appear in the large window below the first row. Below the class drop-down menu, there are three additional buttons: instance,?, class.

    The button ? gives the relevant commentary pertaining to the class currently being viewed. The buttons instance and class indicate in the two following windows what messages can be sent to instances of a class or what messages can be sent to the class itself. In our example, Figure 1.8, it is the Number class and the button instance that has been selected, and has led to the set of existing instance protocols related to this class being displayed in the subwindow titled protocols.

    – Protocols: similarly to categories, which conceptually regroup classes, protocols allow us to conceptually regroup methods. Often, we will see a protocol for testing, printing or private. As the names indicate, the protocol testing groups the methods that return either true or false, the protocol printing groups the methods for printing the instances of the class and the protocol private groups the methods that should only be known by instances of this class and should not be accessible outside of this class.

    – Methods: in this drop-down menu, we find the names of the methods (the selectors) grouped together under the chosen protocol. In our example, Figure 1.8, this is the set of methods implementing arithmetic operations for the Number class.

    Below this row, there are (in the current version) nine buttons which allow us (from left to right) to open another browser on the current selection, on all the methods that contain the selector of the currently selected method in a transmission (the transmitters of this message), on all the classes which implement the chosen selector, on all the different existing versions of the selection, on the set of methods with the same name and the indications on the means of inheritance, on the hierarchy of the selected class, on all accesses to the selected instance or class variable; and, finally, the last button determines the display manner of what is shown on the large window for the code.

    We are left with only the most important and the largest subwindow: the one that contains the Smalltalk code for the item, often the selected method. This subwindow contains an editor and all the capabilities of the Workspace window. Any code that is shown here can be edited, saved or evaluated. For the change to come into effect, we must choose to accept in the contextual menu (or simply by typing Alt + s).

    1.2 Important points

    Let us summarize what we already learned about objects, messages, transmissions and expressions:

    1) Smalltalk uniformly manipulates objects, which can be very simple, such as numbers and characters, or very complicated, such as collections, processes and graphic screens.

    These objects work by receiving messages and adopting for each message the appropriate behavior, which is described in the objects, class. The couple "receiver object, message" constitutes a transmission.

    The elementary act of programming in Smalltalk is the execution of a transmission, similar to the evaluation of a form in LISP and the resolution of a goal in Prolog.

    These transmissions can either give a value (an object) as the result (functions) or operate the edge effects on the environment (procedures). We can compose them in a variety of ways, including placing them in a sequence.

    2) We can assign an object as a value to a variable. The assignment is not a transmission because we are interested in the variable name, while a transmission would require the object to be its value.

    Finally, we can call an expression any composition of variables, constants and transmissions, which results in an object as the value.

    3) An object is characterized by its state and behavior.

    The behavior depends on the state and the state can be modified by the behavior, according to the laws that are defined in the object class.

    4) We will formalize the object state by the values of a set of variables (like in physics), and the behavior by a set of procedures or functions.

    The variables in question appear – of course – in the procedure texts, which ensures the bidirectional link between the state and behavior.

    According to the most common Smalltalk terminology, the variables are called instance variables. Similarly, we call methods the procedures or functions defining the behavior of the object.

    5) The list of instance variable names (but not their values) and the method catalog belong to the object class.

    The object itself (the instance) consists of the list of values of its instance variables that characterize its current state, and the indication (the address) of its class.

    6) Therefore, the execution of one of these procedures, realizing the behavior of the object, only requires that we know the procedure to run and its arguments: these elements constitute the message sent to the object that should produce, in response, the said behavior.

    7) A message is composed of a selector, identifying the procedure in the catalog of methods (its name corresponding to the class), and the arguments transmitted.

    Upon receiving the message, the object aquires of its class the method matching the selector, and then executes the corresponding code giving the instance variables occuring in code the values determined by the objects current state. This version should result in the expected behavior.

    1.3 Exercises

    The solutions can be found on page 341:

    1) As shown in Figure 1.11, open a Workspace and enter any text, such as the following: This text is the first text entered in Squeak. Select a part of the text and then open the contextual menu and try the commands copy, paste, cut, undo on the text. Try the same commands without opening the shortcut menu using only the keyboard commands Alt + c, Alt + v, Alt + x and Alt + z (see Appendix 2, 501–504). You should also try the command do again (either using the contextual menu or with the key combination Alt + j). To do this, select the second word of the text (the word text), enter the word test and then give the command do again.

    Figure 1.11 A Workspace with a selected part of the text

    2) Open a system browser, and:

    – search for the Integer class;

    – display the class hierrachy of the

    Integer class;

    – search the method + of the Integer class;

    – search all the methods + (all the implementations of a method called +);

    – search for all the methods using the selector + (that send the message +);

    – manipulate the system browser in such a way that it would appear as presented in Figure 1.12, i.e. it is reversed, on its head.

    Figure 1.12 An upside-down system browser

    Figure 1.10 A system browser showing the hierarchy of the Number class


    ¹ Without going into technical details, the question whether Smalltalk is an interpreter or not depends on our interpretation of the term. If we use the definition that an interpreter allows us to run programs without first using an explicit compilation phase, in that case Squeak is an interpreter. If, on the other hand, we consider that any acceptance of a Smalltalk method is actually a compilation of the method to the language of the bytecode interpreter of the Smalltalk virtual machine, in that sense we could say that we are dealing with a compiler and not an interpreter. For now, in these early pages, let us consider that this language is interpreted.

    ² To disable a flap, it is enough to simply click once more on the flap icon.

    ³ Smalltalk-80, the ancestor of Squeak, assumed that the user had a mouse with three buttons in red, yellow and blue. The red button was used for selecting objects, the yellow button was used for manipulating inside a window and the blue button was used to manipulate the window itself. In order not to repeat ourselves so much, here are some basic mouse manipulations:

    – 

    with a mouse that has three buttons: the left button makes the selection, in Smalltalk we call the button the red button. The middle button gives the contextual menu of the currently pointed window pane, this corresponds to the yellow button. The button on the right, for manipulating the current window (or subwindow), is called the blue button;

    – 

    on a PC, a mouse has two buttons: the middle button and yellow button, the shortcut menu can be obtained by Ctrl + left click and the blue button can be obtained by Ctrl + right click;

    – 

    on a Macintosh with a mouse that has one button: the simple click gives the selection, Option + click gives the shortcut menu and command + click gives the window menu.

    ⁴ Many menu entries can be obtained simply by the combinations of the Ctrl or Alt keys and a letter, like in this case, print it which can be obtained using Alt + p. Appendix 2 gives a list of these abbreviations.

    ⁵ restoreAfter: asks Squeak to restore the restore the screen to its state prior to the command given as an argument to restoreAfter. This

    Enjoying the preview?
    Page 1 of 1