Squeak by Example
Total Page:16
File Type:pdf, Size:1020Kb
Portland State University PDXScholar Computer Science Faculty Publications and Presentations Computer Science 2009 Squeak by Example Andrew P. Black Portland State University, [email protected] Stéphane Ducasse Oscar Nierstrasz Damien Pollet Damien Cassou See next page for additional authors Follow this and additional works at: https://pdxscholar.library.pdx.edu/compsci_fac Part of the Programming Languages and Compilers Commons Let us know how access to this document benefits ou.y Citation Details Black, Andrew, et al. Squeak by example. 2009. This Book is brought to you for free and open access. It has been accepted for inclusion in Computer Science Faculty Publications and Presentations by an authorized administrator of PDXScholar. Please contact us if we can make this document more accessible: [email protected]. Authors Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz, Damien Pollet, Damien Cassou, and Marcus Denker This book is available at PDXScholar: https://pdxscholar.library.pdx.edu/compsci_fac/110 Squeak by Example Andrew P. Black Stéphane Ducasse Oscar Nierstrasz Damien Pollet with Damien Cassou and Marcus Denker Version of 2009-09-29 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 978-3-9523341-0-2 First Edition, September, 2007. Revised editions with corrections, March 2008, May 2008, September 2009. Contents Preface ix I Getting Started 1 A quick tour of Squeak3 1.1 Getting started.................3 1.2 The World menu................7 1.3 Saving, quitting and restarting a Squeak session.....9 1.4 Workspaces and Transcripts............ 11 1.5 Keyboard shortcuts............... 12 1.6 SqueakMap.................. 15 1.7 The System Browser............... 17 1.8 Finding classes................. 18 1.9 Finding methods................ 20 1.10 Defining a new method.............. 21 1.11 Chapter summary................ 26 2 A first application 29 2.1 The Quinto game................ 29 2.2 Creating a new class Category........... 30 2.3 Defining the class SBECell............. 31 2.4 Adding methods to a class............. 32 2.5 Inspecting an object............... 34 2.6 Defining the class SBEGame............ 36 iv Contents 2.7 Organizing methods into protocols......... 39 2.8 Let’s try our code................ 42 2.9 Saving and sharing Smalltalk code.......... 45 2.10 Chapter summary................ 49 3 Syntax in a nutshell 51 3.1 Syntactic elements............... 51 3.2 Pseudo-variables................ 54 3.3 Message sends................. 55 3.4 Method syntax................. 56 3.5 Block syntax.................. 57 3.6 Conditionals and loops in a nutshell......... 58 3.7 Primitives and pragmas............. 60 3.8 Chapter summary................ 61 4 Understanding message syntax 63 4.1 Identifying messages.............. 63 4.2 Three kinds of messages............. 65 4.3 Message composition.............. 68 4.4 Hints for identifying keyword messages....... 74 4.5 Expression sequences.............. 76 4.6 Cascaded messages............... 76 4.7 Chapter summary................ 76 II Developing in Squeak 5 The Smalltalk object model 81 5.1 The rules of the model.............. 81 5.2 Everything is an Object.............. 81 5.3 Every object is an instance of a class......... 82 5.4 Every class has a superclass............ 89 5.5 Everything happens by message sending....... 93 5.6 Method lookup follows the inheritance chain...... 94 5.7 Shared variables................ 101 v 5.8 Chapter summary................ 106 6 The Squeak programming environment 109 6.1 Overview................... 110 6.2 The System Browser............... 111 6.3 Monticello.................. 124 6.4 The Inspector and the Explorer........... 132 6.5 The Debugger................. 135 6.6 The Process Browser............... 144 6.7 Finding methods................ 145 6.8 Change sets and the Change Sorter......... 146 6.9 The File List Browser.............. 148 6.10 In Smalltalk, you can’t lose code.......... 151 6.11 Chapter summary................ 152 7 SUnit 155 7.1 Introduction.................. 155 7.2 Why testing is important............. 156 7.3 What makes a good test?............. 157 7.4 SUnit by example................ 158 7.5 The SUnit cook book............... 162 7.6 The SUnit framework.............. 164 7.7 Advanced features of SUnit............ 166 7.8 The implementation of SUnit............ 168 7.9 Some advice on testing.............. 171 7.10 Chapter summary................ 172 8 Basic Classes 175 8.1 Object.................... 175 8.2 Numbers................... 184 8.3 Characters.................. 188 8.4 Strings.................... 189 8.5 Booleans................... 190 8.6 Chapter summary................ 192 vi Contents 9 Collections 193 9.1 Introduction.................. 193 9.2 The varieties of collections............. 195 9.3 Implementations of collections........... 198 9.4 Examples of key classes.............. 199 9.5 Collection iterators............... 209 9.6 Some hints for using collections........... 213 9.7 Chapter summary................ 214 10 Streams 217 10.1 Two sequences of elements............ 217 10.2 Streams vs. collections.............. 218 10.3 Streaming over collections............. 219 10.4 Using streams for file access............ 227 10.5 Chapter summary................ 230 11 Morphic 231 11.1 The history of Morphic.............. 231 11.2 Manipulating morphs.............. 233 11.3 Composing morphs............... 233 11.4 Creating and drawing your own morphs....... 235 11.5 Interaction and animation............. 239 11.6 Interactors.................. 242 11.7 Drag-and-drop................. 243 11.8 A complete example............... 245 11.9 More about the canvas.............. 249 11.10 Chapter summary................ 250 III Advanced Squeak 12 Classes and metaclasses 255 12.1 Rules for classes and metaclasses.......... 255 12.2 Revisiting the Smalltalk object model......... 256 12.3 Every class is an instance of a metaclass........ 258 vii 12.4 The metaclass hierarchy parallels the class hierarchy... 259 12.5 Every metaclass Inherits from Class and Behavior ..... 261 12.6 Every metaclass is an instance of Metaclass ....... 264 12.7 The metaclass of Metaclass is an Instance of Metaclass ... 265 12.8 Chapter summary................ 266 IV Appendices A Frequently Asked Questions 271 A.1 Getting started................. 271 A.2 Collections.................. 271 A.3 Browsing the system............... 272 A.4 Using Monticello and SqueakSource......... 274 A.5 Tools.................... 275 A.6 Regular expressions and parsing.......... 275 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 en- tirely 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 Smalltalk1.) 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 1Alec Sharp, Smalltalk by Example. McGraw-Hill, 1997 hURL: http://stephane.ducasse.free.fr/ FreeBooks/ByExample/i. 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