Dylan Programming Release 1.0
Total Page:16
File Type:pdf, Size:1020Kb
Dylan Programming Release 1.0 Neal Feinberg, Sonya E. Keene, Robert O. Mathews, P. Tucker Withington Nov 25, 2018 CONTENTS 1 Front Matter 3 2 Preface 5 2.1 Dylan...................................................5 2.2 Audience.................................................5 2.3 Goals of this book............................................5 2.4 Organization of this book........................................6 2.5 Program examples............................................6 2.6 Conventions used in this book......................................6 2.7 An image of Dylan............................................7 2.8 Acknowledgments............................................7 3 Part 1. Basic Concepts 9 3.1 Introduction...............................................9 3.2 Quick Start................................................ 14 3.3 Methods, Classes, and Objects...................................... 24 3.4 User-Defined Classes and Methods................................... 32 3.5 Class Inheritance............................................. 40 3.6 Multimethods............................................... 55 3.7 Modularity................................................ 59 3.8 A Simple Library............................................. 69 4 Part 2. Intermediate Topics 77 4.1 Nonclass Types.............................................. 77 4.2 Slots................................................... 82 4.3 Collections and Control Flow...................................... 95 4.4 Functions................................................. 111 4.5 Libraries and Modules.......................................... 126 4.6 Four Complete Libraries......................................... 147 5 Part 3. Sample Application 159 5.1 Design of the Airport Application.................................... 159 5.2 Definition of a New Collection...................................... 164 5.3 The Airport Application......................................... 169 6 Part 4. Advanced Topics 191 6.1 Multiple Inheritance........................................... 191 6.2 Performance and Flexibility....................................... 206 6.3 Exceptions................................................ 223 6.4 Macros.................................................. 235 i 7 Source Code of Program Examples 247 8 Resources on Dylan 249 8.1 World Wide Web pages for this book and its examples......................... 249 8.2 Newsgroup................................................ 249 8.3 Harlequin................................................. 249 8.4 Carnegie Mellon University....................................... 250 8.5 Apple Computer, Inc........................................... 250 8.6 Digitool, Inc................................................ 251 8.7 Marlais.................................................. 251 9 Dylan Object Model for C and C++ Programmers 253 9.1 The concept of pointers......................................... 253 9.2 The concept of classes.......................................... 256 10 Glossary 261 Index 267 ii Dylan Programming, Release 1.0 This book will be useful to anyone learning dynamic, object-oriented programming, whether it be in Dylan, Java, Smalltalk, or Lisp. —Andrew Shalit, author of The Dylan Reference Manual. CONTENTS 1 Dylan Programming, Release 1.0 2 CONTENTS CHAPTER ONE FRONT MATTER by Neal Feinberg, Sonya E. Keene, Robert O. Mathews, P. Tucker Withington Harlequin Incorporated Published by Addison-Wesley Longman, ISBN 0-201-47976-1 The authors have all worked on Harlequin’s Dylan product. Neal Feinberg manages the development of Harlequin’s database technology. Sonya E. Keene, author of Object-Oriented Programming in Common Lisp (Addison Wesley Longman, 1989) is also involved in publishing large documents ont the World Wide Web. Robert O. Mathews was previously the OSF documentation project leader for Motif and DCE. P. Tucker Withington designs and develops automatic memory-management facilities for Dylan. Dylan Programming is available in print from Harlequin, Addison-Wesley, or your bookseller. Copyright 1994, 1995, 1996 by The Harlequin Group Limited. All rights reserved. 3 Dylan Programming, Release 1.0 4 Chapter 1. Front Matter CHAPTER TWO PREFACE 2.1 Dylan Dylan (DYnamic LANguage) is a new programming language invented by Apple Computer and several partners. Dylan is dynamic, is object-oriented, and delivers efficient applications. The Dylan language is defined by The Dylan Reference Manual, written by Andrew Shalit, and published by Addison- Wesley (1996). That manual is the definitive reference on Dylan. The Dylan Reference Manual is available on the World Wide Web; see Resources on Dylan, for details. Dylan is up and running. You can get it from Harlequin, Carnegie Mellon University, Apple Computer, Digitool, and other organizations. Dylan implementations run on most of the popular computer platforms. Full-fledged implemen- tations provide both a compiler and a development environment. You can obtain public-domain implementations. See Resources on Dylan. 2.2 Audience This book is written for application programmers who have experience working in a conventional language, such as C, Pascal, COBOL, FORTRAN, or BASIC, or in an object-oriented language, such as C++, Java, Smalltalk, or Common LISP with CLOS. Familiarity with object-oriented programming and dynamic languages is not required. We do compare Dylan to C, C++, and Java in this book, but you can read and understand the book without any knowledge of C, C++, or Java. 2.3 Goals of this book The primary goals of this book are to teach you how to program in Dylan, and how to write programs in an object- oriented style. Along the way, we hope to convince you to use Dylan. It is intended to be a practical, elegant, and fun language to use. This book is a tutorial on programming in Dylan, and it does the following: • Begins with the most basic use of Dylan, and gradually expands to show the more powerful and advanced techniques. • Gives the flavor of working with the Dylan language in a typical Dylan environment. • Shows how to define classes and methods that work together to solve a problem. • Shows how to use many of Dylan’s classes, functions, and features to good effect within the context of an example application. • Introduces the more advanced features of Dylan, including multiple inheritance, performance, exceptions, and macros. 5 Dylan Programming, Release 1.0 This book does not attempt to be as complete as The Dylan Reference Manual, and does not provide the following kinds of material: • Complete descriptions of all classes and functions provided by Dylan • Complete descriptions of the detailed mechanisms in Dylan • To make full use of Dylan, programmers need The Dylan Reference Manual, as well as this book. 2.4 Organization of this book We have divided the chapters of this book into four parts: • Part 1. Basic Concepts, introduces the object-oriented and dynamic nature of Dylan. • Part 2. Intermediate Topics, provides more details about Dylan’s object-oriented techniques, and covers col- lections (that is, how to use strings, vectors, lists, and other kinds of collections), control flow, libraries, and modules. • Part 3. Sample Application, contains a complete working application that illustrates the topics covered in Part 1. Basic Concepts and Part 2. Intermediate Topics. • Part 4. Advanced Topics, covers four areas that are sophisticated and powerful: multiple inheritance, perfor- mance versus flexibility, exceptions, and macros. The chapters in Part 4. Advanced Topics show how we can improve the example shown in Part 3. Sample Application by applying advanced techniques. 2.5 Program examples This book includes many program examples. Our approach is to show how evolutionary programming might work by presenting an example simply at first, and then expanding it gradually. In Part 1. Basic Concepts, we develop an example of a simple library that represents time and position. That library is needed for the sample airport application that we develop in Part 3. Sample Application. The airport application simulates airplanes, runways, gates, flights, and airports. Its goal is to schedule gates for arriving and departing aircraft. To do scheduling, we need the library that represents and manipulates time and position. Harlequin and Addison-Wesley provide World Wide Web pages containing the source code of the program examples. See Source Code of Program Examples. Dylan’s core language is lean. It does not include input–output facilities, support for a user interface, or interfaces for communicating with programs written in other languages. These features are available in libraries supplied by vendors or in the public domain. We want this book to be applicable to the widest possible range of Dylan implementations, so we focus on the core Dylan language, and use only those library interfaces that are widely available. 2.6 Conventions used in this book • We use boldface when we introduce new terms, such as library. • We use bold typewriter font for code examples and names of Dylan functions and objects, such as define method. Code comments appear in oblique typewriter font — for example, // Method that says a greeting define method say-greeting (greeting :: <object>); format-out("%s\n", greeting); end; 6 Chapter 2. Preface Dylan Programming, Release 1.0 • Many Dylan environments provide a listener, which enables you to type in expressions and to see their return values and output. We use a hypothetical Dylan listener to show the result of evaluating