Squeak by Example. Andrew P. Black Stéphane Ducasse Oscar Nierstrasz Damien Pollet. with Damien Cassou and Marcus Denker. Version of - PDF

Description
Squeak by Example Andrew P. Black Stéphane Ducasse Oscar Nierstrasz Damien Pollet with Damien Cassou and Marcus Denker Version of ii This book is available as a free download from SqueakByExample.org,

Please download to get full document.

View again

of 274
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information
Category:

Study Guides, Notes, & Quizzes

Publish on:

Views: 35 | Pages: 274

Extension: PDF | Download: 1

Share
Transcript
Squeak by Example Andrew P. Black Stéphane Ducasse Oscar Nierstrasz Damien Pollet with Damien Cassou and Marcus Denker Version of ii This book is available as a free download from SqueakByExample.org, hosted by the Institute of Computer Science and Applied Mathematics of the University of Bern, Switzerland. Copyright 2007, 2008, 2009 by Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz and Damien Pollet. The contents of this book are protected under Creative Commons Attribution-ShareAlike 3.0 Unported license. You are free: to Share to copy, distribute and transmit the work to Remix to adapt the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page: creativecommons.org/ licenses/by-sa/3.0/ Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author s moral rights. Your fair dealing and other rights are in no way affected by the above. This is a human-readable summary of the Legal Code (the full license): creativecommons.org/licenses/by-sa/3.0/legalcode Published by Square Bracket Associates, Switzerland. SquareBracketAssociates.org ISBN First Edition, September, Revised editions with corrections, March 2008, May 2008, September 2009. Contents Preface ix I Getting Started 1 A quick tour of Squeak Getting started The World menu Saving, quitting and restarting a Squeak session Workspaces and Transcripts Keyboard shortcuts SqueakMap The System Browser Finding classes Finding methods Defining a new method Chapter summary A first application The Quinto game Creating a new class Category Defining the class SBECell Adding methods to a class Inspecting an object Defining the class SBEGame iv Contents 2.7 Organizing methods into protocols Let s try our code Saving and sharing Smalltalk code Chapter summary Syntax in a nutshell Syntactic elements Pseudo-variables Message sends Method syntax Block syntax Conditionals and loops in a nutshell Primitives and pragmas Chapter summary Understanding message syntax Identifying messages Three kinds of messages Message composition Hints for identifying keyword messages Expression sequences Cascaded messages Chapter summary II Developing in Squeak 5 The Smalltalk object model The rules of the model Everything is an Object Every object is an instance of a class Every class has a superclass Everything happens by message sending Method lookup follows the inheritance chain Shared variables v 5.8 Chapter summary The Squeak programming environment Overview The System Browser Monticello The Inspector and the Explorer The Debugger The Process Browser Finding methods Change sets and the Change Sorter The File List Browser In Smalltalk, you can t lose code Chapter summary SUnit Introduction Why testing is important What makes a good test? SUnit by example The SUnit cook book The SUnit framework Advanced features of SUnit The implementation of SUnit Some advice on testing Chapter summary Basic Classes Object Numbers Characters Strings Booleans Chapter summary vi Contents 9 Collections Introduction The varieties of collections Implementations of collections Examples of key classes Collection iterators Some hints for using collections Chapter summary Streams Two sequences of elements Streams vs. collections Streaming over collections Using streams for file access Chapter summary Morphic The history of Morphic Manipulating morphs Composing morphs Creating and drawing your own morphs Interaction and animation Interactors Drag-and-drop A complete example More about the canvas Chapter summary III Advanced Squeak 12 Classes and metaclasses Rules for classes and metaclasses Revisiting the Smalltalk object model Every class is an instance of a metaclass vii 12.4 The metaclass hierarchy parallels the class hierarchy Every metaclass Inherits from Class and Behavior Every metaclass is an instance of Metaclass The metaclass of Metaclass is an Instance of Metaclass Chapter summary IV Appendices A Frequently Asked Questions 271 A.1 Getting started A.2 Collections A.3 Browsing the system A.4 Using Monticello and SqueakSource A.5 Tools A.6 Regular expressions and parsing Bibliography 277 Index 278 Preface What is Squeak? Squeak is a modern, open source, fully-featured implementation of the Smalltalk programming language and environment. Squeak is highly portable even its virtual machine is written entirely in Smalltalk, making it easy to debug, analyze, and change. Squeak is the vehicle for a wide range of innovative projects from multimedia applications and educational platforms to commercial web development environments. Who should read this book? This book presents the various aspects of Squeak, starting with the basics, and proceeding to more advanced topics. This book will not teach you how to program. The reader should have some familiarity with programming languages. Some background with object-oriented programming would be helpful. This book will introduce the Squeak programming environment, the language and the associated tools. You will be exposed to common idioms and practices, but the focus is on the technology, not on object-oriented design. Wherever possible, we will show you lots of examples. (We have been inspired by Alec Sharp s excellent book on Smalltalk 1.) There are numerous other books on Smalltalk freely available on the web but none of these focuses specifically on Squeak. See for example: stephane.ducasse.free.fr/freebooks.html 1 Alec Sharp, Smalltalk by Example. McGraw-Hill, 1997 URL: FreeBooks/ByExample/. x Preface A word of advice Do not be frustrated by parts of Smalltalk that you do not immediately understand. You do not have to know everything! Alan Knight expresses this principle as follows 2 : 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. An open book This book is an open book in the following senses: The content of this book is released under the Creative Commons Attribution-ShareAlike (by-sa) license. In short, you are allowed to freely share and adapt this book, as long as you respect the conditions of the license available at the following URL: creativecommons.org/ licenses/by-sa/3.0/. This book just describes the core of Squeak. Ideally we would like to encourage others to contribute chapters on the parts of Squeak that we have not described. If you would like to participate in this effort, please contact us. We would like to see this book grow! For more details, visit the book s web site, SqueakByExample.org, hosted by the Institute of Computer Science and Applied Mathematics of the University of Bern, Switzerland. The Squeak community The Squeak community is friendly and active. Here is a short list of resources that you may find useful: 2 xi is the main web site of Squeak. (Do not confuse it with which is dedicated to the etoy environment built on top of Squeak but whose audience is elementary school teachers.) is the equivalent of SourceForge for Squeak projects. wiki.squeak.org/squeak is a wiki with up-to-date information about Squeak. About mailing-lists. There are a lot of mailing-lists and sometimes they can be just a little bit too active. If you do not want to get flooded by mail but would still like to participate we suggest you to use news.gmane.org or to browse the lists. You can find the complete list of Squeak mailing-lists at lists. squeakfoundation.org/mailman/listinfo. Note that Squeak-dev refers to the developers mailing-list, which can be browsed here: news.gmane.org/gmane.comp.lang.smalltalk.squeak.general Newbies refers to a friendly mailing-list for beginners where any question can be asked: news.gmane.org/gmane.comp.lang.smalltalk.squeak.beginners (There is so much to learn that we are all beginners in some aspect of Squeak!) IRC. Have a question that you need answered quickly? Would you like to meet with other squeakers around the world? A great place to participate in longer-term discussions is the IRC channel on the #squeak channel at irc.freenode.net. Stop by and say Hi! Other sites. There are several websites supporting the Squeak community today in various ways. Here are some of them: people.squeakfoundation.org is the site of SqueakPeople, which is a kind of advogato.org for squeakers. It offers articles, diaries and an interesting trust metric system. planet.squeak.org is the site of PlanetSqueak which is an RSS aggregator. It is good place to get a flood of squeaky things. This includes the latest blog entries from developers and others who have an interest in Squeak. xii Preface is a site that tracks Squeak users around the world. Examples and exercises We make use of two special conventions in this book. We have tried to provide as many examples as possible. In particular, there are many examples that show a fragment of code which can be evaluated. We use the symbol to indicate the result that you obtain when you select an expression and print it : if you select 3+4 and 'print it', you will see 7 In case you want to play in Squeak with these code snippets, you can download a plain text file with all the example code from the book s web site: SqueakByExample.org. The second convention that we use is to display the icon when there is something for you to do: Go ahead and read the next chapter! to indicate Acknowledgments We would like to thank Hilaire Fernandes and Serge Stinckwich who allowed us to translate parts of their columns on Smalltalk, and Damien Cassou for contributing the chapter on streams. We also thank Tim Rowledge for the use of the Squeak logo, and Frederica Nierstrasz for the original cover art. We especially thank Lukas Renggli and Orla Greevy for their comments on drafts of the first release. We thank the University of Bern, Switzerland, for graciously supporting this open-source project and for hosting the web site of this book. We also thank the Squeak community for their enthusiastic support of this project, and for informing us of the errors found in the first edition of this book. Finally we thank the team that developed Squeak in the first place for making this amazing development environment available to us. Part I Getting Started Chapter 1 A quick tour of Squeak In this chapter we will give you a high-level tour of Squeak to help you get comfortable with the environment. There will be plenty of opportunities to try things out, so it would be a good idea if you have a computer handy when you read this chapter. We will use this icon: to mark places in the text where you should try something out in Squeak. In particular, you will fire up Squeak, learn about the different ways of interacting with the system, and discover some of the basic tools. You will also learn how to define a new method, create an object and send it messages. 1.1 Getting started Squeak is available as a free download from There are three parts that you will need to download, consisting of four files (see Figure 1.1). Virtual Machine Shared Sources User specific system files Figure 1.1: The Squeak download files. 1. The virtual machine (VM) is the only part of the system that is different for each operating system and processor. Pre-compiled virtual 4 A quick tour of Squeak machines are available for all the major computing environments. In Figure 1.1 we see the VM for the Mac is called Squeak beta1U.app. 2. The sources file contains the source code for all of the parts of Squeak that don t change very frequently. In Figure 1.1 it is called SqueakV39.sources. Note that the file SqueakV39.sources is only for versions 3.9 and later of Squeak. For earlier versions, use a sources file corresponding to the main version e.g., SqueakV3.sources for versions of Squeak from 3.0 up to The current system image is a snapshot of a running Squeak system, frozen in time. It consists of two files: an.image file, which contains the state of all of the objects in the system (including classes and methods, since they are objects too), and a.changes file, which contains a log of all of the changes to the source code of the system. In Figure 1.1, we see that we have grabbed the Squeak3.9-final-7067 image and changes files. Actually, we will use a slightly different image in this book. Download and install Squeak on your computer. We recommend that you use the image provided on the Squeak by Example web page. 1 Most of the introductory material in this book will work with any version, so if you already have one installed, you may as well continue to use it. However, if you notice differences between the appearance or behaviour of your system and what is described here, do not be surprised. On the other hand, if you are about to download Squeak for the first time, you may as well grab the Squeak by Example image. As you work in Squeak, the image and changes files are modified, so you need to make sure that they are writable. Always keep these two files together. Never edit them directly with a text editor, as Squeak uses them to store the objects you work with and to log the changes you make to the source code. It is a good idea to keep a backup copy of the downloaded image and changes files so you can always start from a fresh image and reload your code. The sources file and the VM can be read-only they can be shared between different users. All of these files can be placed in the same directory, but it is also possible to put the Virtual Machine and sources file in separate directory where everyone has access to them. Do whatever works best for your style of working and your operating system. Launching. To start Squeak, do whatever your operating system expects: drag the.image file onto the icon of the virtual machine, or double-click the 1 Visit and look for Download Squeak in the sidebar. Getting started 5 Figure 1.2: A fresh SqueakByExample.org image..image file, or at the command line type the name of the virtual machine followed by the path to the.image file. (When you have multiple VMs installed on your machine the operating system may not automatically pick the right one; in this case it is safer to drag and drop the image onto the virtual machine, or to use the command line.) Once Squeak is running, you should see a single large window, possibly containing some open workspace windows (see Figure 1.2), and it s not obvious how to proceed! You will notice that there is no menu bar, or at least not a useful one. Instead, Squeak makes heavy use of context-dependent pop-up menus. Start Squeak. You can dismiss any open workspaces by clicking on the X in the top left corner of the workspace window. You can collapse the windows (so that they can be expanded again later) by clicking on the in the top-right corner. First Interaction. in Figure 1.3 (a). A good place to get started is the world menu shown 6 A quick tour of Squeak Click with the mouse on the background of the main window to show the world menu, then choose open... workspace to create a new workspace. (a) The menu world (b) The menu contextual (c) The morphic halo Figure 1.3: The world menu (brought up by the red mouse button), a contextual menu (yellow mouse button), and a morphic halo (blue mouse button). Squeak was originally designed for a computer with a three button mouse. If your mouse has fewer than three buttons, you will have to press extra keys while clicking the mouse to simulate the extra buttons. A two-button mouse works quite well with Squeak, but if you have only a single-button mouse, you should seriously consider buying a two-button mouse with a clickable scroll wheel: it will make working with Squeak much more pleasant. Squeak avoids terms like left mouse click because different computers, mice, keyboards and personal configurations mean that different users will need to press different physical buttons to achieve the same effect. Instead, the mouse buttons are labeled with colors. The mouse button that you pressed to get the World menu is called the red button; it is most often used for selecting items in lists, selecting text, and selecting menu items. When you start using Squeak, it can be surprisingly helpful to actually label your mouse, as shown in Figure Figure 1.4: The author s mouse. Clicking the scroll wheel activates the blue button. 2 We will avoid the term red-click and use click instead, since this is the default. The World menu 7 The yellow button is the next most used button; it is used to bring up a contextual menu, that is, a menu that offers different sets of actions depending on where the mouse is pointing; see Figure 1.3 (b). Type Time now in the workspace. workspace. Select print it. Now click the yellow button in the Finally, there is the blue button, which is used to activate the morphic halo, an array of handles that are used to perform operations on the on-screen objects themselves, such as rotating them or resizing them; see Figure 1.3 (c). If you let the mouse linger over a handle, a help balloon will explain its function. Click the blue button on the workspace. Grab the left corner and drag it to rotate the workspace. handle near the bottom We recommend that right-handed people configure their mouse to put the red button on the left side of their mouse, the yellow button on the right, and use a clickable scroll wheel, if one is available, for the blue button. If you don t have a clickable scroll wheel, then you can get the Morphic halo by holding down the alt or option key while clicking the red button. If you are using a Macintosh without a second mouse button, you can simulate one by holding down the key while clicking the mouse. However, if you are going to be using Squeak at all often, we recommend investing in a mouse with at least two buttons. You can configure your mouse to work the way you want by using the preferences of your operating system and mouse driver. Squeak has some preferences for customising the mouse and the meta keys on your keyboard. You can find the preference browser in the open item of the World menu. In the preference browser, the general category contains an option swapmousebuttons that switches the yellow and blue functions (see Figure 1.5). The keyboard category has options to duplicate the various command keys. Open the preference browser and find the swapmousebuttons option using the search box. 1.2 The World menu Click again on the Squeak background. You will see the World menu again. Most Squeak menus are not modal; you can leave them on the screen for as long as you wish by clicking the 8 A quick tour of Squeak Figure 1.5: The Preference Browser. push pin icon in the top-right corner. Do this. Also, notice that menus appear when you click the mouse, but do not disappear when you release it; they stay visible until you make a selection, or until you click outside of the menu. You can even move the menu around by grabbing its title bar. The world menu provides you a simple means to access many of the tools that Squeak offers. Have a closer look at the world open... menu. You will see a list of several of the core tools in Squeak, including the system browser (one of many available class browsers) and the workspace. We will encounter most of them in the coming chapters. Saving, quitting and restarting a Squeak session 9 Figure 1.6: The open... dialogue of the world
Related Search
Similar documents
View more...
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks