Pharo by Example
Total Page:16
File Type:pdf, Size:1020Kb
Portland State University PDXScholar Computer Science Faculty Publications and Computer Science Presentations 2009 Pharo by Example Andrew P. Black Portland State University, [email protected] Stéphane Ducasse Oscar Nierstrasz University of Berne Damien Pollet University of Lille Damien Cassou See next page for additional authors Let us know how access to this document benefits ouy . Follow this and additional works at: http://pdxscholar.library.pdx.edu/compsci_fac Citation Details Black, Andrew, et al. Pharo 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. For more information, please contact [email protected]. Authors Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz, Damien Pollet, Damien Cassou, and Marcus Denker This book is available at PDXScholar: http://pdxscholar.library.pdx.edu/compsci_fac/108 Pharo by Example Andrew P. Black Stéphane Ducasse Oscar Nierstrasz Damien Pollet with Damien Cassou and Marcus Denker Version of 2009-10-28 ii This book is available as a free download from http://PharoByExample.org. 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. http://SquareBracketAssociates.org ISBN 978-3-9523341-4-0 First Edition, October, 2009. Cover art by Samuel Morello. Contents Preface ix I Getting Started 1 A quick tour of Pharo3 1.1 Getting started..................3 1.2 The World 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 iv Contents 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 v 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 vi Contents 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 vii 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 Instance of Metaclass .... 286 13.8 Chapter summary................. 288 14 Reflection 289 14.1 Introspection.................. 290 14.2 Browsing code.................. 294 14.3 Classes, method dictionaries and methods........ 297 14.4 Browsing environments.............. 299 14.5 Accessing the run-time context............ 300 14.6 Intercepting messages not understood......... 303 14.7 Objects as method wrappers............. 307 14.8 Pragmas.................... 310 14.9 Chapter summary................. 311 IV Appendices A Frequently Asked Questions 317 A.1 Getting started.................. 317 A.2 Collections................... 317 A.3 Browsing the system................ 318 A.4 Using Monticello and SqueakSource.......... 320 viii Contents A.5 Tools..................... 321 A.6 Regular expressions and parsing........... 321 Bibliography 323 Index 324 Preface What is Pharo? Pharo is a modern, open source, fully-featured implementation of the Smalltalk programming language and environment. Pharo is derived from Squeak1, a re-implementation of the classic Smalltalk-80 system. Whereas Squeak was developed mainly as a platform for developing experimental educational software, Pharo strives to offer a lean, open-source platform for professional software development, and a robust and stable platform for research and development into dynamic languages and environments. Pharo serves as the reference implementation for the Seaside web development framework. Pharo resolves some licensing issues with Squeak. Unlike previous ver- sions of Squeak, the Pharo core contains only code that has been contributed under the MIT license. The Pharo