{\rtf1\mac\deff2 {\fonttbl{\f0\fswiss Chicago;}{\f2\froman New York;}{\f3\fswiss Geneva;}{\f4\fmodern Monaco;}{\f13\fnil Zapf Dingbats;}{\f16\fnil Palatino;}{\f20\froman Times;}{\f21\fswiss Helvetica;}{\f22\fmodern Courier;}{\f23\ftech Symbol;} {\f1948\fnil Lithos-Bold;}{\f2285\fnil Lithos-Light;}{\f2651\fnil Lithos-Black;}{\f2847\fnil Lithos-ExtraLight;}{\f3015\fnil Lithos-Regular;}{\f5862\fnil Bellevue;}{\f12899\fnil AppleGaramond LtIt;}{\f12900\fnil AppleGaramond BkIt;} {\f12901\fnil AppleGaramond BdIt;}{\f12902\fnil AppleGaramond Lt;}{\f12903\fnil AppleGaramond Bk;}{\f12904\fnil AppleGaramond Bd;}{\f13101\fnil Tekton Oblique;}{\f13102\fnil Tekton;}{\f14590\fnil BlkI New Aster BlackItalic;} {\f14591\fnil Blk New Aster Black;}{\f14592\fnil SBI New Aster SemiBoldItalic;}{\f14593\fnil SB New Aster SemiBold;}{\f14594\fnil BI New Aster BoldItalic;}{\f14595\fnil B New Aster Bold;}{\f14596\fnil I New Aster Italic;}{\f14597\fnil New Aster;} {\f14819\fnil BI LucidaSans BoldItalic;}{\f14820\fnil B LucidaSans Bold;}{\f14821\fnil I LucidaSans Italic;}{\f14822\fnil LucidaSans Roman;}{\f14887\fnil BI Lucida BoldItalic;}{\f14888\fnil B Lucida Bold;}{\f14889\fnil I Lucida Italic;} {\f14890\fnil Lucida;}{\f14917\fnil BI Letter Gothic BoldSlanted;}{\f14918\fnil B Letter Gothic Bold;}{\f14919\fnil I Letter Gothic Slanted;}{\f14920\fnil Letter Gothic;}}{\colortbl\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255; \red0\green255\blue0;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;}{\stylesheet{\s224\li480\ri480 \b\v\f16\fs20 \sbasedon0\snext0 PostScript;}{\s231\li1440\ri720\keep\tqr\tldot\tx8640 \f16 \sbasedon232\snext0 toc 3;}{\s232\li960\ri480\tqr\tldot\tx8640 \f16 \sbasedon233\snext0 toc 2;}{\s233\li480\ri480\tqr\tldot\tx8640 \b\f16 \sbasedon0\snext0 toc 1;}{\s235\li1800\ri480 \f16 \sbasedon0\snext0 index 6;}{\s236\li1440\ri480 \f16 \sbasedon0\snext0 index 5;}{\s237\li1080\ri480 \f16 \sbasedon0\snext0 index 4;}{\s238\li720\ri480 \f16 \sbasedon0\snext0 index 3;}{\s239\li960 \f16 \sbasedon0\snext0 index 2;}{\s240\li480 \f16 \sbasedon0\snext0 index 1;}{\s242\li480\ri480 \f16 \sbasedon0\snext0 page number;}{\s243\li440\ri460\brdrt\brdrs \tqc\tx3600\tqr\tx8900 \i\f16 \sbasedon0\snext243 footer;}{\s244\ri480\brdrb\brdrs \tqr\tx8900 \i\f16\fs20 \sbasedon0\snext244 header;}{\s245\li480\ri480 \f16\fs20\up6 \sbasedon0\snext0 footnote reference;}{\s246\li480\ri480 \f16\fs20 \sbasedon0\snext246 footnote text;}{\s249\sb240\keep \i\f16\fs20 \sbasedon0\snext0 heading 7;}{\s250\sb240\keep \b\f16 \sbasedon251\snext0 heading 6;}{\s251\sb240\keep \b\f16 \sbasedon0\snext0 heading 5;}{ \s252\li475\ri480\keepn \b\f16 \sbasedon253\snext0 heading 4,heading 4 (1 top page),H3;}{\s253\li475\ri480\sb120\sa120\keepn\brdrt\brdrs \b\f16\fs28 \sbasedon7\snext0 heading 3,H2;}{\s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 \sbasedon3\snext0 heading 2,H1;}{\s255\li475\ri475\sb80\sa360\keepn\pagebb\brdrt\brdrs \b\f16\fs48 \sbasedon1\snext0 heading 1;}{\li480\ri480 \f16 \sbasedon222\snext0 Normal;}{\s1\li480\ri475\sb80\sa360\keepn\pagebb\brdrt\brdrs \b\f16\fs48 \sbasedon2\snext2 sec1;}{\s2\li480\ri480\sa120 \f16 \sbasedon0\snext2 body;}{\s3\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 \sbasedon1\snext2 sec2;}{\s4\li480\ri480\tx2640 \f16 \sbasedon13\snext4 list 2;}{ \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 \sbasedon2\snext6 example 1;}{\s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 \sbasedon5\snext6 example 2;}{\s7\li480\ri480\sb120\sa120\keepn\brdrt\brdrs \b\f16\fs28 \sbasedon3\snext2 sec3;}{ \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 \sbasedon6\snext6 example 3;}{\s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tqr\tx9360 \f16 \sbasedon2\snext10 def1;}{\s10\li480\ri480\sa120\keepn\tqr\tx8640\tqr\tx9360 \f16 \sbasedon9\snext11 def2;}{ \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 \sbasedon10\snext11 def3;}{\s12\li480\ri480\sa240\tx2640 \f16 \sbasedon4\snext12 list 3;}{\s13\li480\ri480\sb120\tx2640 \f16 \sbasedon2\snext13 list 1;}{\s14\fi-2160\li2640\ri480\sb120\sa120\tx2640 \f16 \sbasedon2\snext14 table 1;}{\s15\fi-2160\li2640\ri480\sa160\tx2640 \f16 \sbasedon14\snext15 table 2;}{\s16\fi-2160\li2640\ri480\sa240\tx2640 \f16 \sbasedon15\snext16 table 3;}{\s17\li480\ri480\sa120\tx2640 \f16 \sbasedon2\snext17 body tight;}{ \s18\fi-360\li1440\ri480\sa160 \f16 \sbasedon0\snext18 list13;}{\s19\fi-480\li480\ri480\sa120\keepn\tx480\tqr\tx9360 \f16 \sbasedon9\snext19 def1-alt;}{\s20\fi-1800\li2880\ri480\sa160 \f16 \sbasedon15\snext20 table 2a;}{\s21\fi-360\li840\ri480\sa160 \f16 \sbasedon2\snext21 bulleted list;}{\s22\li960\ri480\sb120\sa240\keepn\tx2880\tx4800\tx6720 \f22\fs20 \sbasedon5\snext22 example13;}{\s23\fi-2160\li2640\ri480\sa240\keep\tx2640 \f16 \sbasedon14\snext23 table13;}{\s24\fi-2160\li4860\ri480\sa160\tx4860 \f16 \sbasedon15\snext24 table indented;}{\s25\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 \sbasedon6\snext25 example small;}{\s26\sb160\sl-240\keep\keepn\tx720\tx2160 \i\f16\fs20 \sbasedon0\snext27 left side;}{\s27\li720\sl-240\keep\tx720\tx2160 \i\f16\fs20 \sbasedon26\snext26 right side;}{\s28\keep \f16 \sbasedon0\snext28 Normal Tight;}{\s29\sb240\keep \b\f16\fs96 \sbasedon0\snext29 Titlepage;}{\s30\li-360\ri720\sb120\keep\tx2880 \f16\fs20 \sbasedon32\snext30 Blurb;}{\s31\qc\li-2160\sb120\keep \f16\fs72 \sbasedon0\snext0 Chapter;}{\s32\ri720\sb120\keep\tx2880 \f16 \sbasedon0\snext32 Contributors;}{\s33\li2880\ri720\keep\tx2880 \f16 \sbasedon32\snext33 ContributorsMore;}{\s34\sb240\keep \v\f16 \sbasedon224\snext34 Hidden;}{ \s35\li360\keep\keepn\tx3240 \f22\fs20 \sbasedon0\snext35 code example;}{\s36\sb80\keep\keepn\tx360\tx3240 \f16\fs20 \sbasedon0\snext36 code w/tabbed comment;}{\s37\fi-280\li360\keep \i\f16 \sbasedon0\snext37 doc comment;}{\s38\fi-180\li180\sb120\keep \f16 \sbasedon0\snext38 Bulleted;}{\s39\sb240\keep\tqr\tx7920 \f16 \sbasedon0\snext39 design note title;}{\s40\sb180\keep \f65535\fs28 \sbasedon0\snext40 table description;}{\s41\li360\ri180\sb240\keep\keepn\box\brdrdot \phpg\absw2520 \f16 \sbasedon253\snext41 Note;}{\s42\sb720\sl240\keepn\pagebb\tx720 \b\f16\fs36 \sbasedon43\snext43 Heading;}{\s43\sb720\sl240\keepn\tx720 \b\f16\fs28 \sbasedon0\snext44 Subheading;}{\s44\li475\ri475\sb160\sl240\keep\tx720 \f16 \sbasedon0\snext44 Text;}{ \s45\fi-260\li980\sl240 \f16 \sbasedon0\snext45 blah;}{\s46\li360\sb240\sl240\keep\tx720 \f22\fs20 \sbasedon44\snext46 Code;}}\margl1440\margr1440\margt2160\margb2160\facingp\widowctrl\ftnbj\fracwidth \sectd \pgnrestart\linemod0\linex0\cols1\endnhere \pard\plain \li480\ri480\sb2880 \f16 {\fs120 Dylan}{\fs18\up52 TM}{\fs120 \par }\pard \li480\ri480 {\fs72 Interim \par Reference Manual\par }\pard \li480\ri480\sb480 {{\pict\macpict\picw228\pich31 035800000000001f00e4001102ff0c00ffffffff000000000000000000e40000001f00000000000000a1006400086450726f0000000100a0008200a0008c00a100d800040000400000a100d600040000400000a000d7001e0001000a00000000002000e50007000000000008001700230e0f00a100ca000800000000000000 0000a100c800080000015201520005001affff4ccd4ccd001b00000006f7160041000500040019001800a000c900a100d600040000400000a000d700a000d900a100d600040000400000a100d800040000400000a000be00a000d9000800080071001e00020001001c001b000200140015001b001c0009000a000100020014 00a100d800040000400000a000bf00a000d900a000d700a100d800040000400000a100d600040000400000a000d7000800170022000200143c0d00a100ca0008000000000000000000a100c80008000000150014fff1001affff799b4ccd0041000500460019005a00a000c900a100d600040000400000a000d700a000d900 a100d600040000400000a100d800040000400000a000be00a000d9000800080071001e00020043001c005e0002004b0009005e001c0056001500430002004b00a100d800040000400000a000bf00a000d900a000d700a100d800040000400000a100d600040000400000a000d70008001700220002004b490d00a100ca0008 000000000000000000a100c800080000002e002e000a001a0001cccb666400410005008a0019009e00a000c900a100d600040000400000a000d700a000d900a100d600040000400000a100d800040000400000a000be00a000d9000800080071001e00010086001e00a200010094001000a2001e0094000f00860001009400 a100d800040000400000a000bf00a000d900a000d700a100d800040000400000a100d600040000400000a000d700080017002200010094430e00a100ca0008000000000000000000a100c800080000015a0159ffe4001a4ccd8371ffff0041000500cc001900e000a000c900a100d600040000400000a000d700a000d900a1 00d600040000400000a100d800040000400000a000be00a000d9000800080071001e000300ca001c00e3000300de001700e3001c00cf000800ca000300de00a100d800040000400000a000bf00a000d900a000d700a0008d00a0008300ff}}{\fs72 \par }\pard \li480\ri480\sb720 {\fs36 June 4, 1994}{\fs48 \par }\pard \li480\ri480 \page \par \pard\plain \s2\li480\ri480\sa120\sl-220 \f16 {\fs16 \'a9 1992-1994 Apple Computer, Inc.\line All rights reserved.\line \par }\pard \s2\li480\ri480\sa120\sl-220 {\fs16 Apple Computer, Inc.\line 20525 Mariani Avenue\line Cupertino, CA 95014-6299\line (408) 996-1010\par }\pard \s2\li480\ri480\sa120\sl-220 {\fs16 Apple, and the Apple Logo are trademarks of Apple Computer, Inc., registered in the United States and other countries. Dylan is a trademark of Apple Computer.\par Smalltalk-80 is a trademark of ParcPlace Systems. PL/I is a trademark of International Business Machines Corp. Eiffel is a trademark of Interactive Software Engineering, Inc.\par }\pard \s2\li480\ri480\sa120\sl-220 {\fs16 \par }{\b\i\fs16 Limited Warranty on Media and Replacement}{\i\fs16 \par }\pard \s2\li480\ri480\sa120\sl-220 {\fs16 Even though Apple has reviewed this manual, Apple makes no warranty or representation , either express or implied, with respect to this manual, its quality, accuracy, merchantability, or fitness for a partic ular purpose; as a result, this manual is provided "as is," and you, the reader, are assuming the entire risk as to its quality and accuracy.\par }\pard \s2\li480\ri480\sa120\sl-220 {\fs16 In no event will Apple be liable for direct, indirect, special, incidental, or consequential damages resulting from any defect or inaccuracy in this manual, even if advised of the possibility of such damages. \par The warranty and remedies set forth above are exclusive and in lieu of all others, oral or written, express or implied. No Apple dealer, agent, or employee is authorized to make any modification, extension, or addition to this warranty.\par }\pard \s2\li480\ri480\sa120\sl-220 {\fs16 Some states do not allow the exclusion or limitation of implied warranties or liability for incidental or consequential damages, so the above limitation or exclusion may not apply to you. This warranty gives you specific legal rights, and you may also hav e other rights which vary from state to state.\par }\pard \s2\li480\ri480\sa120\sl-220 {\fs16 \par }\pard\plain \li480\ri480 \f16 \par \sect \sectd \sbknone\pgnrestart\pgnlcrm\linemod0\linex0\cols1\endnhere {\headerl \pard\plain \s2\li480\ri480\sa120 \f16 \par }{\headerr \pard\plain \s2\li480\ri480\sa120 \f16 \par }{\footerl \pard\plain \s243\li440\ri460\brdrt\brdrs \tqc\tx3600\tqr\tx8900 \i\f16 {\chpgn }\tab June 4, 1994\tab Dylan Interim Reference Manual\tab \par }{\footerr \pard\plain \s243\li440\ri460\brdrt\brdrs \tqc\tx5040\tqr\tx8900 \i\f16 Dylan Interim Reference Manual\tab June 4, 1994\tab {\chpgn }\par }\pard\plain \s255\li475\ri475\sb80\sa360\keepn\pagebb\brdrt\brdrs \b\f16\fs48 About this Manual\par \pard\plain \s2\li480\ri480\sa120 \f16 When the first Dylan manual was published in 1992, it was met with an enthusiastic response. People agreed with our goals for the language, and many people suggested ways in which Dylan could better meet its goals. \par \pard \s2\li480\ri480\sa120 Since that time, an expanded group of engineers both inside and outside Apple have worked to refine the language design in response to those suggestions. The language has become simpler and more efficient. Loose ends have been tied up. The language has been given a new syntax. Throughout this process, changes to the language design have been published electronically in the form of design notes.\par With the exception of the macro system, the current round of language design is now essentially complete. A new Dylan language reference will be published early in 1995. The new book will be the definitive specification of the Dylan language. Apple is worki ng closely with other Dylan implementors to ensure that the new book will not be Apple-specific, but will apply equally to all Dylan implementations.\par We realize that many people want to read about Dylan now! For that reason, to fill the gap until the new book is published, we\rquote ve put together the {\i Dylan Interim Reference Manual} . This document is an interim user reference for the Dylan language. It combines the original Dylan book, the previously published design notes, and additional previously unpublished design decisions as of May 1994.\par \pard \s2\li480\ri480\sa120 Our goal for this interim document has been to get something useful out to you as soon as possible, not to spend time refining it, so it\rquote s important to understand the limitations of this document:\par \pard \s2\fi-360\li840\ri480\sa120 \bullet \tab This is a very rough document, intended only as a temporary user reference until the new Dylan book is ready. The writing and formatting are messy in places. We hope you\rquote ll excuse us.\par \pard \s2\fi-360\li840\ri480\sa120 \bullet \tab This document is not intended as a language specification. There are places where it is imprecise or inconsistent. We hope you will understand that these are shortcomings of this document, and not of the language design as a whole.\par \bullet \tab This document is not intended as a tutorial or introduction to Dylan programming. We assume that you are alrea dy somewhat familiar with object oriented programming. You will often need to devise your own examples and flip back and forth through the manual as you read.\par \pard \s2\li480\ri480\sa120 Despite these limitations, we think that this document will be useful to people who want to learn about Dylan, especially to those people who are using an early Dylan implementation. Enjoy, and let us know what you think! \par \pard\plain \s255\li475\ri475\sb80\sa360\keepn\pagebb\brdrt\brdrs \b\f16\fs48 Contents\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 About this Manual\tab i\par Contents\tab iii\par Foreword to the First Edition\tab vii\par Preface to the First Edition\tab ix\par Acknowledgments\tab xvii\par 1. Introduction\tab 1\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 Manual Notation\tab 1\par Background and Goals\tab 3\par Language Overview\tab 4\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 2. Syntax\tab 7\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 Lexical Notation\tab 7\par Expressions\tab 10\par Statement bodies\tab 14\par Special function call syntax\tab 15\par Syntax of Dylan Files\tab 17\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 3. Variables\tab 19\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 Module Variables\tab 19\par Lexical Variables\tab 20\par Checking Types\tab 21\par Binding Multiple Values\tab 22\par Assignment\tab 24\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 4. Control Constructs\tab 27\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 True and False\tab 27\par Conditionals\tab 27\par Iteration Constructs\tab 31\par Non-local Exits\tab 35\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 5. Comparisons\tab 39\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 Equality Comparisons\tab 39\par Magnitude Comparisons\tab 40\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 6. Functions\tab 43\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 Function Defining Forms\tab 43\par Parameter Lists\tab 48\par Method Dispatch\tab 55\par Reflective Operations on Functions\tab 62\par The classes , , and \tab 65\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 7. Classes\tab 67\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 Introduction\tab 67\par Defining New Classes\tab 69\par Slot Options\tab 71\par Instance creation\tab 78\par Reflective Operations on Types\tab 84\par Coercing and Copying Objects\tab 86\par The classes , , and \tab 87\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 8. Controlling Dynamism\tab 91\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 Declaring characteristics of classes\tab 91\par Declaring sealing of generic functions\tab 92\par The seal generic form\tab 92\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 9. Collections\tab 95\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 Functions for Collections\tab 96\par Functions for Sequences\tab 101\par The Instantiable Collection Classes\tab 110\par Operations on Arrays\tab 113\par Operations on Deques\tab 115\par Operations on Lists\tab 116\par Operations on Ranges\tab 118\par Operations on Stretchy Vectors\tab 119\par Operations on Strings\tab 119\par Operations on Tables\tab 121\par Operations on Vectors\tab 122\par The Iteration Protocol\tab 122\par Iteration Stability and Natural Order\tab 128\par Collection Keys \tab 128\par Mutability\tab 130\par Collection Alignment\tab 132\par Defining a New Collection Class: A Summary\tab 134\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 10. Characters and Symbols\tab 135\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 Characters\tab 135\par Symbols \tab 136\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 11. Numbers \tab 137\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 Automatic Type Conversion\tab 137\par Numeric Classes\tab 138\par General Arithmetic Functions\tab 138\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 12. Other Operations\tab 143\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 Functional Operations\tab 143\par Function application\tab 146\par Identity function\tab 146\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 13. Conditions \tab 147\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 Background\tab 147\par Overview\tab 149\par Specification\tab 152\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 14. Modules\tab 163\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 Overview\tab 163\par Programs, module declarations\tab 164\par Module declarations\tab 165\par Examples\tab 168\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 15. Libraries\tab 171\par Appendix A: How to get more information\tab 175\par Appendix B: Dylan Syntax BNF\tab 177\par \pard\plain \s232\li960\ri480\tqr\tldot\tx8640 \f16 Lexical grammar\tab 177\par Phrase Grammar\tab 183\par \pard\plain \s233\li480\ri480\tqr\tldot\tx8640 \b\f16 Index\tab 189\par \pard\plain \li480\ri480 \f16 \page \par \pard\plain \s255\li475\ri475\sb80\sa360\keepn\pagebb\brdrt\brdrs \b\f16\fs48 Foreword to the First Edition\line {\plain \b\f16 by Larry Tesler\par }\pard\plain \s2\li480\ri480\sa120 \f16 In the mid-1980\rquote s, a number of researchers in Apple\rquote s Advanced Technology Group (ATG) discovered that exploration of their software ideas was increasingly constrained by languages and tools. They migrated a significant number of projects from Object Pasca l, C, and C++ to Smalltalk, Lisp, and various knowledge-engineering environments. \par \pard \s2\li480\ri480\sa120 Soon management became concerned that technologies developed in these unrelated environments woul d not be able to play together. Boldly, we proposed adoption of a single dynamic programming environment throughout the group. The goal was acknowledged, but we soon found that none of the environments we were using could meet every need. For some proje cts, Common Lisp was too big. For others, Smalltalk-80\rquote s simplicity was limiting. And of course, there were forcefully stated differences in personal preference.\par \pard \s2\li480\ri480\sa120 Humbled, we decided to search outside the company for an environment that satisfied all requirements\emdash at least technical considerations, if not matters of taste. We encountered some interesting languages, including Eiffel, Self, Beta, and OakLisp, to name just a few, but we could find no environment that met our requirements. \par \pard \s2\li480\ri480\sa120 Reluctantly, we entertained the thought of designing yet another language. We envisioned a language that would:\par \pard\plain \s21\fi-360\li840\ri480\sa160 \f16 \bullet \tab combine the best aspects of Lisp and Smalltalk\par \bullet \tab provide static language users an attractive dynamic alternative\par \bullet \tab be practical on small machines\par \bullet \tab provide high dynamism during rapid prototyping and development\par \bullet \tab provide tools to achieve commercial performance in production code\par \pard\plain \s2\li480\ri480\sa120 \f16 During this exercise, we were approached by an Apple engineering group seeking an object-oriented dynamic language for a contemplated product. Instead of developing a language just for research, we agreed that ATG would strive to create a system practical for delivery of commercial applications.\par \pard \s2\li480\ri480\sa120 After some analysis, we concluded that a language that would meet all our requirements would be more like Lisp than Smalltalk, except that, like Smalltalk, it would be \ldblquote objects all the way down\rdblquote . By providing alternate syntaxes atop the extremely neutral base of Lisp, we believed we could present a familiar face not only to Lisp programmers, but also to those accustomed to other syntaxes, including Smalltalk\rquote s.\par \pard \s2\li480\ri480\sa120 We felt we could not design a good language without concurrently implementing it. But we did not have the skills in house to implement the kind of language we envisioned. Around that time we were approached by Massachusetts-based Coral Software, Inc., which was seeking to be acquired. Coral had created a popular Common Lisp implementation distinguished by its small memory footprint, very usable speed, interesting object system, and thorou gh integration into the Macintosh. A few months later, we acquired the assets of the company, hired most of the staff, and created \ldblquote ATG East\rdblquote in Cambridge. \par \pard \s2\li480\ri480\sa120 We asked the new group to accept two challenges: (1) continue to develop their Common Lisp implem entation, adding CLOS, ephemeral garbage collection, and other features; and (2) develop a new language and environment meeting the aforementioned requirements. They accepted both challenges. The first led to the very popular product known as Macintosh C ommon Lisp (MCL) 2.0. The second led to Dylan.\par \pard \s2\li480\ri480\sa120 Dylan is not yet a frozen language. Debates about proposed improvements continue. But enough of Dylan has been designed, implemented, and experienced that now is the time to take it public and benefit from the wisdom of a wider programming community. We welcome your comments.\par \pard\plain \li480\ri480 \f16 \par \tab {\i Larry Tesler\par \tab Cupertino, California\par \tab March 1992}\par \par \pard\plain \s255\li475\ri475\sb80\sa360\keepn\pagebb\brdrt\brdrs \b\f16\fs48 Preface to the First Edition\line {\plain \b\f16 by Ike Nassi\par }\pard\plain \s2\li480\ri480\sa120 \f16 Since its founding, Apple Computer has been dedicated to the cause of moving the power of computers into the hands of computer users. Our most successful product to date has been the Apple\'a8 Macintosh\'aa . Since its introduction eight years ago, Macintosh has defined the state of the art in computer ease of learning and ease of use. Macintosh, almost single-handedly, has transformed the way we use computers.\par \pard \s2\li480\ri480\sa120 It\rquote s time to transform the way we program them, as well.\par \pard \s2\li480\ri480\sa120 In 1984, before Macintosh, computer users were asked to make their work patterns conform to the requirements of the machine in front of them. All aspects of computing were presented in terms defined by the computer. The user was given little control ove r the organization of data, the order in which tasks were performed, and the manner in which work was presented. The machine presented a model which had little to do with the user\rquote s world, and the user had no choice but to conform to this model. Rather than facilitate work, the computer came between the user and the user\rquote s work. The user had to overcome the computer before getting any work done.\par \pard \s2\li480\ri480\sa120 In 1992, the situation is still much the same for programmers. There is still a large gap between software product conception and software product realization. This gap is filled with bits, bytes, arrays, machine-level debuggers, dangling poin ters, hours-long recompilations, and months-long design and redesign cycles. Programmers are still asked to present their high-level ideas of program behavior in low-level terms defined by CPU architectures of the early 1970\rquote s. Programming work flow is s till dictated by the historical limitations of compilers, linkers, and debuggers. Program design in 1992 is reminiscent of typographical design in 1982: design and execution are performed by a series of specialists, resulting in an awkward, lengthy and e xpensive design, test, and redesign process.\par \pard \s2\li480\ri480\sa120 {\i Time-to-market} is consistently rated very high in lists of factors affecting the success of high technology products. The end result of poor programming tools is either poor time-to-market or poor programs. Software companies are faced with a dilemma: they can take y ears, and do the job right, or they can cut corners and get their software into the market before their competitors. Neither solution results in a healthy software industry. Neither solution allows our products to keep up with our visions.\par \pard \s2\li480\ri480\sa120 Software development today also has a large {\i barrier to entry} . Only companies with deep financial resources can bring a full fledged application to market. The result is a loss of healthy competition and fresh ideas in the software field.\par \pard \s2\li480\ri480\sa120 Finally, our computing engines are becoming more diverse. In addition to the usual dimensions of speed, memory capacity, screen resolution, interconnectedness, power consumption, and size, the underlying operating systems and toolboxes are becoming broader in scope. Specific implementations can also become more focused, requiring a higher degree of tailorability. For example, with the introduction of mobile computing, software requirements may change as locations change. So ftware used on mobile computers needs to be able to rapidly respond to spontaneously changing conditions in a reliable way.\par \pard \s2\li480\ri480\sa120 Object oriented programming takes important steps towards fixing these problems. By letting programmers structure the text of thei r programs in terms of the problem at hand, object oriented programming narrows the gap between conception and realization. However, object oriented programming by itself is insufficient. It addresses how programs are described, but it does not address m any problems of the day-to-day activity of programming. It doesn\rquote t change programming\rquote s awkward work flow, nor does it make any guarantees about robustness, nor does it relieve the programmer of many tedious bookkeeping tasks, tasks which are better performed by a computer than by a human.\par Today\rquote s most popular object oriented languages are still {\i static languages} . In such languages, most of the information about objects is discarded during compilation, so programs cannot be modified without recompilation, and debugging is more likely to occur at machine level than the level at which the program was designed. In addition, these languages encourage mixing objects with non-object oriented bits and bytes. Even objects can be treated as undifferentiated bits, leading to the possibility of protocol violations and obscure errors. Finally, there is no attempt to put the process of programming into the hands of the programmer. Generally speaking, large programs must still be written in their entirety before they can be compiled and tested.\par \pard \s2\li480\ri480\sa120 Static object oriented languages provide only half of a solution. The other half is provided by {\i dynamism}, yielding what Apple calls {\i object oriented dynamic languages}, or {\i OODLs} . In addition to supporting the object oriented methodology, OODLs must support a number of features which guarantee that programming takes place in terms defined by the programmer, rather than in the terms of the hardware. \par \pard \s2\li480\ri480\sa120 OODLs must support rapid creation, delivery, and subsequent modification of ambitious, reliable, and efficient software. Among the specific requirements an ideal OODL should satisfy are the following:\par \pard \s2\li480\ri480\sa120 {\b Automatic Memory Management\par }\pard \s2\li480\ri480\sa120 Memory management bugs are among the most common and difficult errors in static programming languages. Bugs involving dangling pointers and twice-freed objects are notoriously hard to track down.\par \pard \s2\li480\ri480\sa120 The language run-time, and not the programmer, should be responsible for allocating storage for objects and reclaiming the storage of objects which are no longer used. There should be no explicit procedure calls for allocating or deallocating memory, or fo r deallocating objects. \par In a well engineered implementation, automatic memory management should be robust and scalable. It should not create memory fragmentation, or fail in the presence of large (possibly virtual) address spaces. It should not cause seemingly arbitrary and unpredictable delays for end users.\par \pard \s2\li480\ri480\sa120 In a true OODL, there should be no machine-level pointers, only objects. Once freed from dealing with pointers, the programmer can begin to think of objects at a higher level and the primitives become comparably richer. For example, a programmer working with collections of objects does not need to worry about memory leaks as collections expand and contract. The programmer can concentrate on the task at hand, rather than on the bookkeeping details.\par \pard \s2\li480\ri480\sa120 Many large programming projects begin with the design of a memory management subsystem. There is no reason that this task should not be performed once, and the corresponding implementation of that design embodied in the language run-time, and thereby made available to all programmers.\par \pard \s2\li480\ri480\sa120 {\b Dynamic Linking/ Incremental Development\par }\pard \s2\li480\ri480\sa120 Programmers should have the ability to build up their programs piece by piece, integrating preexisting pieces when possible and where available. The transition between rapid prototyping and mainstream development should be continuous rather than discrete. It should not require changing languages or tools.\par \pard \s2\li480\ri480\sa120 This requirement affects the programming process in at least four ways:\par \pard\plain \s21\fi-360\li840\ri480\sa160 \f16 \bullet \tab During the initial construction of a program, classes and functions can be compiled and tested individually. This gives programmers the freedom to use a bottom-up programming style, if they so choose, obviating the nee d for the construction of a complex superstructure for initial testing.\par \pard \s21\fi-360\li840\ri480\sa160 \bullet \tab During debugging, individual functions and classes can be redefined without resorting to a full recompilation of the program and perhaps without even halting the execution of the program. \par \bullet \tab Programs can be delivered in components, which can be linked together on either the development machine or the end-user machine, using built-in language features.\par \pard \s21\fi-360\li840\ri480\sa160 \bullet \tab Program patches (i.e., field upgrades) can be distributed to end-users using buil t-in language features. Applying the patch should be a very low overhead operation. There should be virtually no performance penalty for executing patched code. Because you don\rquote t need source code for the original application in order to apply the patch, traditional intellectual property issues arising from propagating source code simply need not arise.\par \pard\plain \s2\li480\ri480\sa120 \f16 {\b Self Identifying Objects / Introspection\par }\pard \s2\li480\ri480\sa120 Operations should be checked for type safety before they are performed. If possible, this check should be perf ormed at compile-time, otherwise it should be performed at run-time. This feature guarantees that type errors are noticed as soon as they occur, before they can propagate and cause system corruption. Because the integrity of the object model is maintaine d, error reporting can occur in terms of programmer objects and end-user objects, rather than in machine-level terms. In many cases, complete error recovery is possible.\par \pard \s2\li480\ri480\sa120 The language should contain features for introspection. This means that the language run-time should have sufficient power to answer questions about itself and the objects it manages. For example, it should be possible at execution time to analyze the structure of an object, find the subclasses of a class, etc.\par \pard \s2\li480\ri480\sa120 To facilitate type-safety and introspection, objects are self-identifying in memory. Unless all uses of an object can be analyzed at compile-time, the run-time memory for the object should contain enough information to identify its class and value.\par \pard \s2\li480\ri480\sa120 {\b Object Oriented Programming Environment\par }\pard \s2\li480\ri480\sa120 The programming environment should present all debugging information at an object oriented level. Errors should be described in high-level terms similar to those used by the programmer in constructing the program. Inspection facilities should show a prog ram as a collection of objects, not as a mass of undifferentiated bits. There should be tools for performance analysis and monitoring of single objects as well as collections of objects.\par \pard \s2\li480\ri480\sa120 There should be rich libraries of components, and the means to navigate within and between them and to organize and administer them.\par \pard \s2\li480\ri480\sa120 There should be a well thought out distinction between development environment and execution environment, and where they are different, the development environment should manage the communication between them in as transparent a manner as possible. This f eature is missing from current OODLs, but it is essential for the delivery of OODL-based applications to end-users.\par \pard \s2\li480\ri480\sa120 An important aspect of these features is that they are mutually supporting, forming an {\i organic whole. } The simplest implementation of each depends on the existence of the others. For example, automatic memory management relies on the ability to walk memory and identify objects. Another example is that rapid prototyping requires rapid modification of the program, and so incremental dynamic linking becomes essential. Incremental dynamic relinking utilizes automatic reclamation of storage occupied by the functions, methods, and data being replaced.\par \pard \s2\li480\ri480\sa120 Many people in commercia l industry and in the research community have recognized the problems with static languages, and some have started to build products that provide some of the features of OODLs. For example, interactive programming environments for static languages are beg inning to appear. We applaud this development. However, we believe that starting with a static language requires too many compromises. If each OODL feature were to be added in isolation, the mutually supporting characteristics would be lost, creating re dundancy, conflicts, and inefficiency. When instead these features are built together into the core of a programming system, they provide a simple and secure foundation for growth. \par \pard \s2\li480\ri480\sa120 The common criticism of OODLs is that we cannot afford them. Most programmers view OODLs as slow and as memory hogs. The common wisdom is that OODLs do not make good use of machine resources. Fortunately, this view is out of date. The combination of im proved OODL implementation technology and increasingly powerful hardware make OODLs eminently practical. Every year or two our hardware gets twice as fast and has twice as much memory. Would anyone say that the quality of our software has increased at the same rate? Programming must be made easier, or the fastest hardware i n the world will only give us incremental software improvements. By investing a few cycles we can enable a new generation of applications.\par \pard \s2\li480\ri480\sa120 When Macintosh was first released, many people thought that windows, menus, and a bit mapped display were a waste of machine resources. We see in retrospect that this was not true. What good is the power of a computer, if it can\rquote t be accessed by a user?\par This book describes Dylan\'aa , a new object oriented dynamic language designed by Apple. Dylan is our attempt at a language which is simple, yet powerful, one which keeps programming at a high level but which can be compiled efficiently and has a relatively modest memory footprint. \par \pard \s2\li480\ri480\sa120 Apple already has one OODL product: Macintosh Common Lisp. Dylan is intended to complement Common Lisp, not to replace it. Common Lisp is a rich environment defined by a standard and available in compatible implementations on a broad range of platforms. Dylan is lean and stripped down to a minimum feature set. At present Dylan is not availab le on any platform (outside Apple), but is intended to run on a wide variety of machines, including very small machines that don\rquote t have the horsepower to support a modern Common Lisp. Common Lisp is aimed primarily at the Lisp community, while Dylan is ac cessible to application developers unfamiliar with Lisp. Common Lisp is oriented more towards exploratory programming with delivery capability, while Dylan is oriented more towards delivery with exploratory capability. \par \pard \s2\li480\ri480\sa120 In our research and development labs, Apple has its own implementations of Dylan, which are being produced hand in glove with the language definition as a design assurance measure. We would like to see others create additional implementations.{\fs18\up6 \chftn {\footnote \pard\plain \s246\li480\ri480 \f16\fs20 {\fs18\up6 \chftn }Since this was written, such implementations are well underway on a variety of platforms.}} We would be particularly interested in worki ng with a group to create a reference implementation optimized for portability rather than performance, so that anyone could try out Dylan no matter what brand of computer they use. Because Dylan is a trademark of Apple Computer, anyone implementing the l anguage must obtain permission to use the name. However, it is our intention that permission will be granted to anyone with a conforming implementation.\par Apple Eastern Research and Technology{\fs18\up6 \chftn {\footnote \pard\plain \s246\li480\ri480 \f16\fs20 {\fs18\up6 \chftn }Now Apple Cambridge Engineering.}} will be the focal point for discussion of the Dylan language, i ts implementations, and its future evolution. Current and prospective users or implementors of Dylan, as well as programming language researchers who are interested in object oriented dynamic languages, are invited to comment on the design of the language and suggest improvements that are consistent with the goals noted above. We will also continue our past policy of driving the evolution of the language from the needs and comments of our users, who are system and application developers whose previous exper ience is largely with conventional languages. We hope to integrate all inputs to make Dylan the best language we can and use it to bring the benefits of OODLs to as many programmers as we can possibly reach.\par \pard\plain \li480\ri480 \f16 \par {\i \tab Ike Nassi\par \tab Cambridge, Massachusetts\par \tab March 1992}\par \par \pard\plain \s255\li475\ri475\sb80\sa360\keepn\pagebb\brdrt\brdrs \b\f16\fs48 \page Acknowledgments\par \pard\plain \s2\li480\ri480\sa120 \f16 Designing Dylan has been a work of many hands.\par \pard \s2\li480\ri480\sa120 The primary contributors to the language design were Kim Barrett, Glenn S. Burke, Robert Cassels, Bill Chiles, Jerome T. Coonen, Scott Fahlman, Paul Gleichauf, James Grandy, Paul Haahr, Joh n Hotchkiss, Jeremy A. Jones, Michael Kahl, William Lott, Rob MacLachlan,David A. Moon, Ike Nassi, Jeffrey Piazza, Kent Pitman, Keith Playford, Andrew Shalit, Jonathan Sobel, Walter R. Smith, Bill St. Clair, Orca Starbuck, Oliver Steele, Robert Stockton, S teve Strassmann, Larry Tesler, Derek White, and Gail Zacharias.\par \pard \s2\li480\ri480\sa120 Additional design work and oodles of helpful comments were provided by Jim Allard, Peter Alley, Jonathan Bachrach, Richard Barber, David Betz, John Barr, Alan Bawden, Stoney Ballard, Ernie Bee rnink, Brent Benson, Eric Benson, Rasha Bozinovic, Bill Campbell, Steve Capps, Yu-Ying Chow, Doug Cutting, Ken Dickey, Richard Duncan, Mikel Evins, Marc Feeley, Gregg Foster, Tom Gordon, Jed Harris, Alice K. Hartley, Alan Kay, Larry Kenyon, Mike Lockwood, Matthew MacLaurin, Robin Mair, Neil Mayle, Tim McInerney, Scott McKay, Jim Meehan, John Meier, Jim Miller, Richard Mlynarik, Robert Muller, Carl Nelson, Julian Padgett, Paige Parsons, Ed Petrus, Peter Potrebic, Jonathan Rees, Kalman Reti, David Rosenfeld, Carl Schwarcz, David Singer, Andy Sizer, David C. Smith, Andy Stadler, Joshua Susser, Michael Tibbott, Tom Vrhel, John Wainwright, Bob Welland, Paul Wilson, and Dan Zigmond.\par \pard \s2\li480\ri480\sa120 Moral and logistical support were provided by Donna Auguste, Chrissy Boggs, Mickey Dennison, Gina Field, Rick Fleischman, James Joaquin, Rick LeFaivre, Ross Knights, Becky Mulhearn, David Nagel, Wendy Phillips, Mark Preece, Mary Reagan, Shane Robison, and Susan M. Whittemore.\par \pard \s2\li480\ri480\sa120 The Dylan project is directed by Ike Nassi and Carl Schwarcz.\par \pard \s2\li480\ri480\sa120 The first edition of the Dylan manual was written by Andrew Shalit with contributions from Jeffrey Piazza, David Moon, and Steve Strassmann. It was extensively revised by Orca Starbuck, with editorial help from Kim Barrett, David Moon, and Steve Strassman n to incorporate recent language changes.\par \pard \s2\li480\ri480\sa120 The first edition of the manual was designed by Scott Kim and Steve Strassmann. The original cover art was designed by Scott Kim.\par \pard \s2\li480\ri480\sa120 The Dylan project is funded by Apple Computer\rquote s AppleSoft Division.\par \pard\plain \li480\ri480 \f16 \par \pard \li480\ri480 \page \sect \sectd \sbknone\pgnrestart\linemod0\linex0\cols1\endnhere {\headerl \pard\plain \s2\li480\ri480\sa120 \f16 \par }{\headerr \pard\plain \s2\li480\ri480\sa120 \f16 \par }{\footerl \pard\plain \s243\li440\ri460\brdrt\brdrs \tqc\tx3600\tqr\tx8900 \i\f16 {\chpgn }\tab June 4, 1994\tab Dylan Interim Reference Manual\tab \par }{\footerr \pard\plain \s243\li440\ri460\brdrt\brdrs \tqc\tx3600\tqc\tx5040\tqr\tx8900 \i\f16 Dylan Interim Reference Manual\tab June 4, 1994\tab {\chpgn }{\plain \f16 \par }}\pard\plain \s255\li475\ri475\sb80\sa360\keepn\pagebb\brdrt\brdrs \b\f16\fs48 1. Introduction\par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Manual Notation\par \pard\plain \s2\li480\ri480\sa120 \f16 This manual uses a small number of typographic conventions:\par Body text appears in Roman.\par First uses of terms appear in {\b Bold}.\par \pard \s2\li480\ri480\sa120 Text which appears as it would be entered into the computer appears in {\f22 Courier}.\par In the body of the manual, the following notation is used to describe syntax forms:\par \pard\plain \s14\fi-2160\li2640\ri480\sb120\sa120\tx2640 \f16 \{...\}\tab Curly braces indicate a group of items.\par \pard\plain \s15\fi-2160\li2640\ri480\sa160\tx2640 \f16 \{\'c9\}{\fs20\up6 *}\tab Curly braces followed by an asterisk indicate that the contents can appear zero or more times.\par \{\'c9\}{\fs20\up6 +}\tab Curly braces followed by a plus sign indicate that the contents can appear one or more times.\par \pard \s15\fi-2160\li2640\ri480\sa160\tx2640 [\'c9]\tab Square brackets indicate that the contents are optional.\par \pard\plain \s16\fi-2160\li2640\ri480\sa240\tx2640 \f16 \'c9|\'c9\tab Items separated by a vertical bar are mutually exclusive. One or the other may appear.\par \pard\plain \s2\li480\ri480\sa120 \f16 The BNF grammar at the end of the manual uses its own notation which is different from the above.\par \pard \s2\li480\ri480\sa120 Formal parameters (i.e., place holders for the actual text you would enter) appear in {\i Italic}. The names of parameters are often descriptive of the type of value acceptable as a value of the parameter. They will often m atch the names of classes, indicating a general instance of the class. For example, {\i number} indicates a general instance of the class {\f22 }, and {\i string} indicates a general instance of the class {\f22 }.\par \pard \s2\li480\ri480\sa120 Interactions between a user and a Dylan interpreter are shown in a mixture of {\f22 Courier}{\b\f22 }and {\i\f22 Courier Italic}. {\f22 Courier }shows the text entered by the user, and {\i\f22 Courier Italic} shows the text printed by the interpreter. The question mark used in these sections is the interpreter\rquote s prompt.{\fs18\up6 \chftn {\footnote \pard\plain \s246\li480\ri480 \f16\fs20 {\fs18\up6 \chftn }The term {\i interpreter} is used, even though most implementations of Dylan are expected to be compiled. In most cases, an interpreter will be implemented by compiling Dylan expressions and immediately executing the compiled code.}}\par \pard \s2\li480\ri480\sa120 The error messages in the manual have been edited for brevity. The details of interactions with Dylan (prompt text, error message texts, etc.) will differ among implementations.\par \pard \s2\li480\ri480\sa120 The functions {\f22 format} and {\f22 print} are used in some examples but are not part of Dylan.\par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 \page Background and Goals\par \pard\plain \s2\li480\ri480\sa120 \f16 Dylan is a general-purpose high-level programming language, designed for use both in application and systems programming. Dylan includes garbage collection, run-time type checking, selective dynamism, error recovery, and a module system. These f eatures simplify programming and support attractive debugging and development tools.\par \pard \s2\li480\ri480\sa120 The motivation for Dylan is that programs and large software systems have become too complex for traditional static programming languages. To create a new generation of software\emdash and to ensure that this software is maintainable and extensible\emdash requires a new, friendlier set of programming tools. The core of these tools is a simple and expressive language, one that is efficient but which protects the programmer from crashes and machine-level debugging. All programming should take place at an object-oriented level. Improved implementation techniques and increasingly powerful computer hardware make it possible to use fully dynamic object-oriented languages for a wide range of programming tasks.\par \pard \s2\li480\ri480\sa120 Dylan is built from the ground up with a thoroughly integrated object model. Like some object-oriented extensions to Lisp, Dylan implements polymorphism through generic functions and class dispatch, rather than through a Smalltalk-styl e message-passing mechanism.{\fs20\up6 \chftn {\footnote \pard\plain \s246\li480\ri480 \f16\fs20 {\fs18\up6 \chftn }Notable Lisp systems that use generic functions are CLOS (Common Lisp Object System), Oak Lisp, and EuLisp.}} Generic functions provide a very convenient model for a broad range of programming tasks.\par \pard \s2\li480\ri480\sa120 The target audience of Dylan is application developers now using languages such as C, C++, and Pascal who are ready to move up to Object Oriented Dynamic Languages. Dylan is not intended primarily for the Lisp or Smalltalk community, thus compatibility wi th Smalltalk or with Lisp dialects such as Scheme and Common Lisp is not a primary goal. Dylan has been rethought from the ground up, on a fully object-oriented foundation. A primary goal was to make the language as simple as possible, but no simpler. We have tried to avoid multiple ways to do the same thing, to omit features that are difficult for the average application developer to understand and use effectively, to leave out anything that we do not know how to implement efficiently in both space and time, and to provide a clear separation of the language executed at run-time from the development tools. At the same time, we\rquote ve moved complicated low-level bookkeeping underneath the language and taken it out of the hands of the application programmers, to make them more productive. The overriding goal of Dylan is rapid development and delivery of applications and application component s on very small computers.\par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Language Overview\par \pard\plain \s2\li480\ri480\sa120 \f16 Dylan is built around two central concepts: {\b objects} and {\b functions}.\par \pard \s2\li480\ri480\sa120 {\b Objects} are the data that exist in Dylan programs. Objects are mapped into a {\b class heterarchy}.{\fs16\up6 \chftn {\footnote \pard\plain \s246\li480\ri480 \f16\fs20 {\fs18\up6 \chftn }A heterarchy\emdash also called a directed acyclic graph (DAG)\emdash is like a hierarchy, except that nodes can have multiple parents. Dylan classes are in a heterarchy because Dylan supports multiple inheritance.}} The class heterarchy describes the inheritanc e relationships among {\b classes}. The class {\f22 } is the root of the heterarchy. Every class inherits from its {\b direct superclasses} (except for {\f22 } which has no superclasses). Classes may also have {\b subclasses} , which inherit from them, either directly or indirectly. The {\b superclasses} of a class are the class itself, its direct superclasses, their direct superclasses, and so on back to the class {\f22 }.\par \pard \s2\li480\ri480\sa120 Every object in the Dylan world is a {\b direct} {\b instance} of exactly one class. This class is referred to as \ldblquote the class of\rdblquote the instance. For each object {\i O} that is a direct instance of class {\i C}, the object {\i O }is also an {\b indirect instance} of all the superclasses of {\i C} (except {\i C} itself). (It follows that every object in Dylan is an indirect instance of {\f22 }.) The term {\b general instance} means \ldblquote either a direct or indirect instance.\rdblquote \par \pard \s2\li480\ri480\sa120 Dylan defines four categories of classes:\par \pard\plain \s14\fi-2160\li2640\ri480\sb120\sa120\tx2640 \f16 {\b Abstract Class}\tab A class that is not intended to have direct instances. Its general instances obey an interface implemented by subclasses. The opposite of an abstract class is a {\b concrete class}.\par \pard\plain \s15\fi-2160\li2640\ri480\sa160\tx2640 \f16 {\b Instantiable Class}\tab A class that can be used as the first argument to {\f22 make}. The opposite of an instantiable class is an {\b uninstantiable class} . Note that an abstract class may be instantiable.\par \pard\plain \s16\fi-2160\li2640\ri480\sa240\tx2640 \f16 {\b Sealed Class}\tab A class that cannot be subclassed, other than those subclasses explicitly defined in the same library. The opposite of a sealed class is an {\b open class}.\par \pard \s16\fi-2160\li2640\ri480\sa240\tx2640 {\b Primary Class}\tab A class that may be used only as the primary superclass in multiple inheritance. It is illegal for a class to have two primary superclasses unless one is a subclass of the other. The opposite of a primary class is a {\b free class}.\par \pard\plain \s2\li480\ri480\sa120 \f16 {\b Functions} are a class of objects used for performing actions in Dylan. Functions correspond to the functions, procedures, methods, and messages of other languages. Dylan supports two classes of functions: methods and generic functions.\par \pard \s2\li480\ri480\sa120 A {\b method} is a basic callable unit of code. It contains a typed argument list and a code body. The types in the argument list are called the {\b specializers} of the method. A method can only be called with arguments that match the specializers of the argument list. (In the most common case, the specializer is a class, and the corresponding argument must be a general instance of the class.) When the speciali zers of a method match a set of arguments, the method is said to be {\b applicable} to the arguments. If a method is called with inappropriate arguments, an error is signaled.\par \pard \s2\li480\ri480\sa120 A {\b generic function} contains zero or more methods. When a generic function is called, the arguments are compared to the specializers of all the methods in the generic function. The method with the most appropriate specializers is run. In this way, a generic function chooses a method based on the classes and identities of its arguments. This is the basic technique for object-oriented dispatch in Dylan.\par \pard \s2\li480\ri480\sa120 In contrast to methods in other object-oriented languages, Dylan methods can also be called directly. They do not need to be added to generic functions. Used this way, methods play the role of blocks in Smalltalk, closures in Lisp, and functions in C.\par \pard \s2\li480\ri480\sa120 Classes and functions are themselves first-class objects, which can be manipulated by programs.\par \pard \s2\li480\ri480\sa120 \par \pard \s2\li480\ri480\sa120 \page \par \pard\plain \s255\li475\ri475\sb80\sa360\keepn\pagebb\brdrt\brdrs \b\f16\fs48 2. Syntax\par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Lexical Notation\par \pard\plain \s2\li480\ri480\sa120 \f16 Here are the lexical conventions used in Dylan programs.\par \pard\plain \s14\fi-2160\li2640\ri480\sb120\sa120\tx2640 \f16 \tab White space (including spaces, tabs, newlines, and newpage characters) serves as a delimiter. The amount of contiguous white space is not significant. Thus, any number of contiguous white space characters can be used for formatting.{\f22 \par }\pard\plain \s15\fi-2160\li2640\ri480\sa160\tx2640 \f16 {\b\f22 foo}\tab Variable names are a series of alphanumeric characters, and characters from the set {\b\f22 ! & * < = > | ^ $ % @ _ - + ~ ? /} , that do not begin with one of the special characters {\b\f22 - + ~ ? /}. If a variable name begins with a numeric character, the name must contain two alphabetic characters in a row. Variable names are not case-sensitive.\par \pard \s15\fi-2160\li2640\ri480\sa160\tx2640 {\b\f22 =}\tab Infix operators are the following one- or two-character sequences: {\b\f22 + - * / - = == < > <= >= ~= ~ & | ^}. \par \pard \s15\fi-2160\li2640\ri480\sa160\tx2640 {\b \\}{\b\f22 =}\tab An infix operator variable name is the corresponding infix operator, prefixed with the infix operator escape character {\b\f22 \\} . This allows an infix operator to be used wherever a variable name is allowed (for example, as the variable name in a {\b\f22 define method} or as a function argument).\par \pard\plain \s4\li480\ri480\tx2640 \f16 {\b\f22 123}\tab The syntax of numbers is described in the numerics\par {\b\f22 1.5}\tab section of this manual.{\f22 \par }\pard \s4\li480\ri480\tx2640 {\b\f22 -4.0\par +57\par }\pard\plain \s12\li480\ri480\sa240\tx2640 \f16 {\b\f22 #x1f4e\par }\pard\plain \s15\fi-2160\li2640\ri480\sa160\tx2640 \f16 {\b\f22 "abc"}\tab Literal strings are delimited by double-quote marks. The characters between the quotes are the elements of the string. Within the string, white space is significant (it contributes to the string). Double quotes can be included in strings by preceding them with a backslash character. Within a string, backslash ({\b\f22 \\} ) has the general effect of quoting the following character. To include a backslash in a string, the backslash must be preceded by another backslash.\par \pard \s15\fi-2160\li2640\ri480\sa160\tx2640 {\b\f22 #"Hello"}\tab Symbols are interned strings. They are preceded by a .i.sharp-sign. See the section on symbols and keywords for more details.\par \pard \s15\fi-2160\li2640\ri480\sa160\tx2640 {\b\f22 Hello:}\tab An alternative syntax for symbols is \ldblquote keyword syntax.\rdblquote See the section on symbols and keywords for more details.\par \pard \s15\fi-2160\li2640\ri480\sa160\tx2640 {\b\f22 #(1, 2, 3)}\tab Literal lists are delimited by open and close parentheses, and preceded by a sharp-sign. The items between the parentheses are the elements of the list. They are separated with commas. \par {\b\f22 #[1, 2, 3]}\tab Literal vectors (one-dimensional arrays) are delimited with square brackets, and preceded by a sharp-sign. The items between the square brackets are the elements of the vector. They are separated with commas.\par \pard \s15\fi-2160\li2640\ri480\sa160\tx2640 {\b\f22 'M'}\tab Character constants are represented by delimiting the character with single quotes.\par \pard \s15\fi-2160\li2640\ri480\sa160\tx2640 {\b\f22 //}\tab A pair of slashes indicates the start of a comment. The comment continues until the end of the line. At the start of a new line, regular parsing begins again.\par {\b\f22 /* ... */}\tab Slash-star pairs delimit extended comments. These comments can run over multiple lines. Extended comments can be nested.\par \pard \s15\fi-2160\li2640\ri480\sa160\tx2640 {\b\f22 #t}\tab The hash-sign t syntax is used to indicate the canonical true value.{\f22 \par }{\b\f22 #f}{\f22 \tab }The hash-sign f syntax is used to indicate the canonical false value.\par \pard\plain \s4\li480\ri480\tx2640 \f16 {\b\f22 #key}\tab {\f22 #key}, {\f22 #rest}, {\f22 #next} and {\f22 #all-keys} are used to indicate \line {\b\f22 #rest}{\f22 \tab }special tokens in parameter lists.{\f22 \par }\pard \s4\li480\ri480\tx2640 {\b\f22 #next\par }\pard\plain \s12\li480\ri480\sa240\tx2640 \f16 {\b\f22 #all-keys}\par \pard\plain \s253\li475\ri480\sb120\sa120\keepn\brdrt\brdrs \b\f16\fs28 Naming Conventions\par \pard\plain \s2\li480\ri480\sa120 \f16 Several conventions for naming variables help programmers identify the purposes of variables. The names of variables do not affect the semantics of a program, but are simply used to improve readability.\par \pard \s2\li480\ri480\sa120\keepn Dylan uses the following naming conventions:\par Variables used to hold classes begin and end with angle brackets.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 \tab \tab \par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 \tab \tab \par \pard\plain \s2\li480\ri480\sa120\keepn \f16 Module variables that are designed to have their values change in the course of a program (i.e., variables that are not read-only) begin and end with asterisks. \par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 *parse-level*\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 *incremental-search-string*\par *machine-state*\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 *window-count*\par \pard\plain \s2\li480\ri480\sa120\keepn \f16 Program constants (read-only module variables) begin with a dollar sign.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 $pi\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 $end-of-file\par \pard\plain \s2\li480\ri480\sa120\keepn \f16 The names of most predicate functions end with a question mark. Predicates are functions which return a true or false value.{\fs16\up6 \chftn {\footnote \pard\plain \s246\li480\ri480 \f16\fs20 {\fs18\up6 \chftn }Three notable exceptions to the terminal question mark convention are {\f22 <}, {\f22 >}, and =.}}\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 subclass?\tab even?\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 instance?\tab \par \pard\plain \s2\li480\ri480\sa120\keepn \f16 The names of most operations that destructively modify data structures end with an exclamation point.{\fs18\up6 \chftn {\footnote \pard\plain \s246\li480\ri480 \f16\fs20 {\fs18\up6 \chftn } Notable exceptions to the terminal exclamation point convention are := and all the setter variable names.}} These names sometimes contrast with versions that allocate new memory for the result.\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 reverse!\tab {\i\f16 (non-destructive version is }reverse{\i\f16 )}\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 sort!\tab {\i\f16 (non-destructive version is }sort{\i\f16 )}\par \pard\plain \s2\li480\ri480\sa120 \f16 Operations that retrieve a value from a location are called {\b getters}. Operations that store into a location are called {\b setters} . In general, getters and setters come in pairs. Setter variable names are derived by appending \line \ldblquote {\f22 -setter}\rdblquote to the corresponding getter variable name. Actually, this is not simply a conventi on. The rule is exploited to generate setter names from getter names automatically, and it is used to expand calls to {\f22 :=}.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx3420\tx4800\tx6720 \f22\fs20 {\i\f16 Getter \tab Setter }\par \pard\plain \s6\li960\ri480\keepn\tx3420\tx4800\tx6720 \f22\fs20 window-position\tab window-position-setter\par table-size\tab table-size-setter\par \pard\plain \s8\li960\ri480\sa320\tx3420\tx4800\tx6720 \f22\fs20 window-color\tab window-color-setter\par \pard\plain \s2\li480\ri480\sa120 \f16 These two expressions are equivalent; they both set the color of \line {\f22 my-window} to {\f22 green}:\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 window-color-setter (green, my-window)\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 my-window.window-color := green\par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Expressions\par \pard\plain \s2\li480\ri480\sa120\keep\keepn \f16 Dylan programs are composed of {\b expressions}. When an expression is {\b evaluated}, it {\b returns} zero or more v alues. Evaluating an expression may have side effects and may involve evaluating sub-expressions. \par \pard \s2\li480\ri480\sa120\keep\keepn An {\b outer expression} is an expression that is not a sub-expression of any expression. A {\b top-level expression} is either an outer expression or a direct sub-expression of a {\f22 begin} syntax form that itself is a top-level expression. (Such a {\f22 begin} cannot have local declarations in it, although the grammar allows it.) Definitions (see Syntax Forms, below) are restricted to be top-level expressions. Other expressi ons may appear either as top-level expressions, or as sub-expressions of other expressions.\par \pard \s2\li480\ri480\sa120\keep There are four kinds of expressions: literal constants, variable references, function calls, and syntax forms.{\fs16\up6 \chftn {\footnote \pard\plain \s246\li480\ri480 \f16\fs20 {\fs18\up6 \chftn }The term \ldblquote syntax form\rdblquote describes both special forms and macros. They are equivalent to macros in other languages.}}\par \pard\plain \s253\li475\ri480\sb120\sa120\keepn\brdrt\brdrs \b\f16\fs28 Literal Constants\par \pard\plain \li480\ri480 \f16 A {\b literal constant }is an object that is specified explicitly in program text.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ? }"abc"\par \pard\plain \s6\li960\ri480\tx2880\tx4800\tx6720 \f22\fs20 {\i "abc"\par }\pard \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 {\i ?} 123\par \pard \s6\li960\ri480\tx2880\tx4800\tx6720 {\i 123\par }\pard \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 {\i ?} foo:\par \pard \s6\li960\ri480\tx2880\tx4800\tx6720 {\i foo:\par }\pard \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 {\i ?} 'M'\par \pard \s6\li960\ri480\tx2880\tx4800\tx6720 {\i 'M'\par }\pard \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 {\i ?} #t\par \pard \s6\li960\ri480\tx2880\tx4800\tx6720 {\i #t\par }\pard \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 {\i ?} #f\par \pard \s6\li960\ri480\tx2880\tx4800\tx6720 {\i #f\par }\pard \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 {\i ?} #(1, 2, 3)\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 {\i #(1, 2, 3)\par }\pard\plain \li480\ri480 \f16 Literal constants are immutable. The keys and elements of collections that are literal constants are immutable. Attempting t o modify an immutable object has undefined consequences. Immutable objects may share structure. Literal constants that are = may or may not be ==.\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 \par \pard\plain \s253\li475\ri480\sb120\sa120\keepn\brdrt\brdrs \b\f16\fs28 Variable References\par \pard\plain \s2\li480\ri480\sa120 \f16 When an expression is a variable name, the expression indicates a variable reference. The variable name evaluates to the value of the variable.\par \pard \s2\li480\ri480\sa120 Dylan supports two kinds of variables: lexical variables and module variables. Lexical variables are created locally. They roughly correspond to local variables in other languages. Module varia bles are created by using a definition such as {\f22 define variable} or {\f22 define method}.{\f22 }They roughly correspond to global variables in other languages.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ? }\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i \{the class \}\par ?} concatenate\par {\i \{the generic function concatenate\}\par ?} define variable my-variable = 25;\par {\i my-variable\par ?} my-variable\par \pard \s6\li960\ri480\tx2880\tx4800\tx6720 {\i 25\par }\pard \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 {\i ?} begin\par let x = 50;\par x + x;\par end;\par \pard \s6\li960\ri480\tx2880\tx4800\tx6720 {\i 100\par \par }\pard\plain \s2\li480\ri480\sa120 \f16 Note that Dylan classes and functions are first-class objects and are named by variables, like other objects.\par See the chapter on Variables for detailed information about lexical and module variables.\par \pard\plain \s253\li475\ri480\sb120\sa120\keepn\brdrt\brdrs \b\f16\fs28 Function Calls\par \pard\plain \s2\li480\ri480\sa120\keepn \f16 Function calls generally have the syntax\par \pard \s2\li480\ri480\sa120 {\i expression}{\f22 (}{\i arg}{\i\fs18\dn4 1}, {\i arg}{\i\fs18\dn4 2}, \'c9 {\i arg}{\i\fs18\dn4 n}{\f22 )}\par \pard \s2\li480\ri480\sa120 {\i expression} must evaluate to a function. This is the function to be called. The arguments to the function are the values of the elements in the list.\par \pard \s2\li480\ri480\sa120 The arguments to a function are evaluated in left-to-right order. The function is evaluated before the arguments.{\fs18\up6 \chftn {\footnote \pard\plain \s246\li480\ri480 \f16\fs20 {\fs18\up6 \chftn } Except in the special slot reference syntax {\f22 argument.function}, where {\f22 argument} is evaluated first, followed by {\f22 function}.}} Once the function and all the arguments are evaluated, the function is called with the arguments.\par \pard \s2\li480\ri480\sa120 A function call evaluates to the values returned by the function.\par \pard \s2\li480\ri480\sa120 {\i expression} is commonly a variable name. In the following example, the function being called is the value of the variable {\f22 factorial}:\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 factorial(x, y)\par \par \pard\plain \s2\li480\ri480\sa120 \f16 However, the expression in the function position does not have to be a variable reference; it can be any expression that evaluates to a function. In this way, Dylan is like C and Scheme, and unlike Common Lisp. The following example has a complex express ion in the "function position." The complex expression creates and returns a function which adds one to its argument. This function is then applied to 99.\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ?} (method(x) x + 1 end) (99)\par 100\par \pard\plain \s35\li360\keep\keepn\tx3240 \f22\fs20 \par \pard\plain \s253\li475\ri480\sb120\sa120\keepn\brdrt\brdrs \b\f16\fs28 Syntax Forms\par \pard\plain \s2\li480\ri480\sa120 \f16 A {\b syntax form} is a form which has its own rule for evaluation. Syntax forms are introduced by {\b syntax operators}. Some examples of syntax operators are {\f22 define variable}, {\f22 for}, {\f22 method} , and {\f22 :=}.\par \pard \s2\li480\ri480\sa120 Syntax forms may have different evaluation rules than function calls. In a function call, all the subexpressions are evaluated and passed to the function as arguments. In contrast, a syntax form can examine its subexpressions literally and can choose to evaluate some and give special meaning to others. The special evaluation rules for each syntax form are listed along with that form\rquote s definition.\par \pard \s2\li480\ri480\sa120 Generally, the syntax operator is the first word or words appearing in the syntax form. For example, the {\f22 define variable} syntax form begins with the words {\f22 define variable}, and the {\f22 method} syntax form begins with the word {\f22 method}.\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 define variable foo = 3;\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 method (a :: , b :: )\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 list (a - b, a + b);\par end method;\par // Creates an anonymous method, which expects 2 \par // numeric arguments.\par {\b \par }\pard\plain \s2\li480\ri480\sa120 \f16 However, the syntax operator does not have to be the first word in the form. For example, the assignment operator, :=, is an infix operator that is also a syntax form with special evaluation rules.\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 my-variable := 12;\par // Sets the value to 12\par {\b \par }\pard\plain \s2\li480\ri480\sa120 \f16 There are three kinds of syntax forms:\par \pard \s2\fi-360\li840\ri480\sa120 \bullet \tab Most syntax forms are {\b macros} (introduced by {\b macro operators}). Some macros (like the {\f22 for} iteration construct) are part of the core Dylan language. Others are created by individual implementations or by programmers. Macros can be thought of as expanding into other, equivalent, expressions.\par \pard \s2\fi-360\li840\ri480\sa120 {\b \bullet \tab Special forms} (introduced by {\b special form operators}) are basic, built-in syntax forms that cannot be reduced to other expressions. Examples are {\f22 :=} and {\f22 method}.\par \pard \s2\fi-360\li840\ri480\sa120 {\b \bullet \tab Definitions} (introduced by {\b definition} {\b operators}) are declarative parts of a program. Definitions are restricted to be top level expressions (see the next section). They do not return values. Examples are {\f22 define method} and {\f22 define variable}.\par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Statement bodies\par \pard\plain \s2\li480\ri480\sa120\keepn \f16 Many kinds of expressions include {\b implicit bodies} , which may contain one or more other expressions, separated by semicolons. When an implicit body is evaluated, the expressions in the implicit body are evaluated in order (left to right). The value of the implicit body is the value of the last expressio n.\par \pard \s2\li480\ri480\sa120\keepn The simplest expression containing an implicit body is {\f22 begin}.\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keep\keepn\tx480\tx720\tqr\tx9360 \f16 {\b\f22 begin}\tab [Special Form]\line {\i body}\line {\b\f22 end}\line {\f23 \'de}{\i values}\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 The {\i body } consists of any number of expressions separated by semicolons. {\f22 begin} executes the expressions in the {\i body} sequentially. The values of the last expression are returned. If there are no expressions in the {\i body}, {\f22 #f} is returned.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 begin\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 close(airlock.outer-door);\tab // this\par pressurize(airlock);\tab // is an\par open(airlock.inner-door);\tab // implicit body\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 end;\par \pard\plain \s2\li480\ri480\sa120 \f16 Programs only need to use {\f22 begin} explicitly in order to limit the scope of a lexical variable, or in situations where a single expression is expected, such as an argument to a function call. Many other expressions contain implicit bodies. For example, the body of a method definition is an implicit body, and the {\f22 if} statement has an implicit body in each sub-part.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 if (moon-phase == #"full")\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 wolf-form(werewolf);\tab // this is an\par howl-at-moon(werewolf);\tab // implicit body\par else\par human-form(werewolf);\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 end if;\par \pard\plain \s2\li480\ri480\sa120 \f16 Some statements use the word {\f22 end} to mark the end of an implicit body. Other statements use other markers, such as the {\f22 else} that marks the end of the first implicit body in an {\f22 if} statement. The syntax description for each statement defines where implicit bodies may appear, and how the boundary is marked.\par \pard\plain \s253\li475\ri480\sb120\sa120\keepn\brdrt\brdrs \b\f16\fs28 Semicolons\par \pard\plain \li480\ri480\keepn \f16 Multiple expressions, such as top-level expressions appearing in source files, are separated by a semicolon \ldblquote {\f22 ;}\rdblquote . Semicolons are also required to separate all but the last expression in implicit bodies. The semicolon after the last expression is optional.\par \pard \li480\ri480\keepn \par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Special function call syntax\par \pard\plain \s253\li475\ri480\sb120\sa120\keepn\brdrt\brdrs \b\f16\fs28 Infix Operators\par \pard\plain \s2\li480\ri480\sa120\keepn \f16 Certain predefined operators are called using standard algebraic syntax. Operators and their arguments must be separated by whitespace or parentheses. All binary operators are left-associative, except for the assignment operator {\f22 :=}, which is right-associative. \par \pard \s2\li480\ri480\sa120\keepn These are listed in descending order of precedence. Operators within a group share the same precedence. \par \pard\plain \s36\li360\keep\keepn\tx1800\tx3240\tx4680 \f16\fs20 {\i \par }\pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i unary}\tab -\tab negative\par {\i unary}\tab ~\tab not (boolean)\par \par {\i binary}\tab ^\tab exponentiation\par \par {\i binary}\tab *\tab multiplication\par {\i binary}\tab /\tab division\par \par {\i binary}\tab +\tab addition\par {\i binary}\tab -\tab subtraction\par \par {\i binary}\tab =\tab equality\par {\i binary}\tab ==\tab identity\par {\i binary}\tab ~=\tab not equals\par {\i binary}\tab <\tab less than\par {\i binary}\tab >\tab greater than\par {\i binary}\tab <=\tab less than or equals\par {\i binary}\tab >=\tab greater than or equals\par \par {\i binary}\tab &\tab and\par {\i binary}\tab |\tab or\par \par {\i binary}\tab :=\tab assign\par \pard\plain \li480\ri480 \f16 \par \pard\plain \s2\li480\ri480\sa120 \f16 Except for the last three, all of these infix operators are functions. These functions are first class, like all Dylan functions, but in order to use one where a variable name is expected (for example, as the variable name in a {\f22 define method,} or as a function argument) the operator must be prefixed with the infix operator escape character ({\b \\}).\par \pard \s2\li480\ri480\sa120\keepn The order of evaluation for general infix expressions is as follows:\par In an expression of the form \par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\plain \f22 arg}{\plain \f22\dn4 1}{\plain \f22 op \'c9 op arg}{\plain \f22\dn4 n}{\plain \f22 \par }\pard \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \par \pard\plain \s2\li480\ri480\sa120 \f16 {\f22 arg}{\f22\dn4 1}{\f22 \'c9 arg}{\f22\dn4 n}{\dn4 }are evaluated in that order. The evaluation time of module variables that {\f22 op} \rquote s are defined to invoke (e.g. the plus function for {\f22 +} ), is unspecified.\par The last three infix operators listed here ({\f22 &}, {\f22 |}, and {\f22 :=}) are syntax forms. They have special evaluation rules which are described where these forms are defined.\par \pard\plain \s253\li475\ri480\sb120\sa120\keepn\brdrt\brdrs \b\f16\fs28 Slot Reference\par \pard\plain \li480\ri480\keepn \f16 Dylan provides a shorthand syntax for slot reference. The syntax {\f22 argument.function} applies {\i function} to {\i argument}. This syntax is usually used for slot reference, to access the {\i function } slot of the object {\i argument}.\par \pard \li480\ri480\keepn This can be cascaded and is left associative (i.e., {\f22 moo.goo.gai.pan} gets the {\i pan} slot of the {\i gai} slot of the {\i goo } slot of {\i moo}.).\par \pard \li480\ri480\keepn Examples: \par \pard\plain \s35\li360\keep\keepn\tx3240 \f22\fs20 \par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ?} america.capital\par "Washington, D.C."\par {\i ?} me.mother.current-husband.favorite-child == me\par #t\par \pard\plain \li480\ri480\keepn \f16 \par \pard \li480\ri480\keepn Dylan\rquote s left-to-right evaluation rule applies here: {\f22 argument} is evaluated first, followed by {\f22 function}.\par \pard\plain \s253\li475\ri480\sb120\sa120\keepn\brdrt\brdrs \b\f16\fs28 Array Reference\par \pard\plain \li480\ri480\keepn \f16 Dylan provides a shorthand syntax for array reference. The syntax {\f22 foo[i]} is equivalent to the function call {\f22 element(foo, i)}. The syntax foo[i{\fs18\dn4 1}, i{\fs18\dn4 2}, ... i{\fs18\dn4 n}] where n \'ad 1 is equivalent to the function call aref (foo, i{\fs18\dn4 1}, i{\fs18\dn4 2}, ... i{\fs18\dn4 n}).\par \pard \li720\ri480 \par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ?} define constant vect = #[7, 8, 9]\par defined vect\par {\i ?} vect[0]\par 7\par \pard\plain \s2\li480\ri480\sa120 \f16 \par In expressions of the form\par \pard \s2\li480\ri480\sa120 {\f22 \tab sequence[arg}{\f22\fs18\dn4 1}{\f22 , \'c9, arg}{\f22\fs18\dn4 n}{\f22 ]\par }\pard \s2\li480\ri480\sa120 {\f22 sequence} is evaluated first, then {\f22 arg}{\f22\fs18\dn4 1}{\f22 \'c9 arg}{\f22\fs18\dn4 n} in that order.\par \pard \s2\li480\ri480\sa120 The evaluation time of the variable <{\f22\fs20 element} in Dylan module> or <{\f22\fs20 aref} in Dylan module>, which this expression is defined to invoke, is unspecified.\par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Syntax of Dylan Files\par \pard\plain \s2\li480\ri480\sa120 \f16 A {\b Dylan source code file} is a standard portable file format for publishing Dylan source code. Such a file has two parts, the {\b header }and the {\b body}. The header comes before the body.\par \pard \s2\li480\ri480\sa120 The body consists of zero or more outer expressions and comments.\par The header consists of one or more keyword-value pairs, as follows:\par \pard\plain \s21\fi-360\li840\ri480\sa160 \f16 1) \tab The syntax of a keyword is as follows: a keyword is a letter, followed by zero or more letters, digits, and hyphens, followe d by a colon, contains only characters from the ISO 646 7-bit character set, and is case-independent.\par \pard \s21\fi-360\li840\ri480\sa160 2) \tab A keyword begins on a new line, and cannot be preceded by whitespace.\par \pard \s21\fi-360\li840\ri480\sa160 3) \tab All text (excluding padding whitespace) between the keyword and the newline is considered to be the value. Additional lines can be added by having the additional lines start with whitespace. Leading whitespace is ignored on all lines.\par \pard \s21\fi-360\li840\ri480\sa160 4) \tab Interpretation of the value is determined by the keyword.\par \pard \s21\fi-360\li840\ri480\sa160 5) \tab Implementations must recognize and handle standardized keywords properly, unless the specification for a keyword explicitly states that it can be ignored.\par \pard \s21\fi-360\li840\ri480\sa160 6) \tab When importing a file, implementations are free to ignore any non-standard keyword/value pair that they do not recognize.\par 7) \tab When exporting a file, implementations must use standard keywords properly. Implementations are free to use non-standard keywords. \par \pard \s21\fi-360\li840\ri480\sa160 8) \tab The definition of a keyword may specify that the keyword may occur more than once in a single file header. If it does n ot, then it is an error for the keyword to occur more than once. If it does, it should specify the meaning of multiple occurences.\tab \par \pard\plain \s2\li480\ri480\sa120 \f16 The header cannot contain comments.\par \pard \s2\li480\ri480\sa120 Blank lines may not appear in the header. A blank line defines the end of the header and the beginning of the code body. The blank line is not part of the code body. (A "blank line" is a line consisting of zero or more space or tab characters, ending in a newline character.)\par \pard\plain \li480\ri480\sl240 \f16 \par \pard\plain \s2\li480\ri480\sa120 \f16 The following standard keywords are defined:\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tqr\tx9360 \f16 {\b\f22 module:} {\i module-name\tab }[Header keyword]{\i \tab \par }\pard\plain \s2\li480\ri480\sa120 \f16 Expressions in the file are associated with the named module. This keyword is required.\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tqr\tx9360 \f16 {\b\f22 author:}{\f22 }{\i arbitrary text\tab }[Header keyword]\par {\b\f22 copyright: }{\i arbitrary text\tab }[Header keyword]\par {\b\f22 version: }{\i arbitrary text\tab }[Header keyword]\par \pard\plain \s2\li480\ri480\sa120 \f16 These are provided for standardization. These are optional, and can be ignored by the implementation.\par \pard \s2\li480\ri480\sa120 \par A typical Dylan source file might look like this:\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 module:\tab quickdraw\par author: \tab J. Random Rect\par \tab Linear Wheels, Inc., "Where quality is a slogan!"\par \tab rect@linear.com\par copyright: (c) 1993 Linear Wheels, Inc., All rights reserved\par version: \tab 1.3 alpha (not fully tested)\par \par define constant $black-color = ...\par ...\par \par \pard\plain \s255\li475\ri475\sb80\sa360\keepn\pagebb\brdrt\brdrs \b\f16\fs48 3. Variables\par \pard\plain \s2\li480\ri480\sa120 \f16 Dylan supports two kinds of variables: {\b lexical variables} and {\b module variables}.\par \pard \s2\li480\ri480\sa120 The term {\b environment} is used to refer to the set of variables that are available to a given part of a program. It includes both module variables and lexical variables.\par \pard \s2\li480\ri480\sa120 Variables can be set to new values with the {\f22 :=} special form, which is described in this chapter's section on Assignment.\par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Module Variables\par \pard\plain \s2\li480\ri480\sa120 \f16 Module variables can be referenced from anywhere inside the module. They play the role assumed by global variables in other languages.\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tx1080\tqr\tx9360 \f16 {\b\f22 define variable} {\i bindings}{\b\f22 = }{\i init}\tab [Definition]\par \pard\plain \s2\li480\ri480\sa120 \f16 {\f22 define variable} creates module variables in the current module. \par {\i bindings} are the variable(s) to be created, and mayhave the following forms:\par \tab \tab {\i variable}\par or \tab {\b\f22 ( }\{ {\i variable} \}* [ {\b\f22 #rest} {\i rest-variable-name } ]{\b\f22 )}\par \pard \s2\li480\ri480\sa120 where {\i variable} may be either {\i variable-name} or {\i variable-name} {\b\f22 ::} {\i type}. The {\i variable-names } and {\i rest-variable-name } must have the syntax of variable names, and are not evaluated. The values returned by {\i init} provide the initial values for the variable(s) specified by {\i bindings}.\par \pard \s2\li480\ri480\sa120 In the simplest case, {\i bindings} is just a variable name, and {\i init} returns one value which is used to initialize that variable. See Checking Types and Binding Multiple Values for more complex cases.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ? }define variable foo = 10;\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i foo\par ?} foo + foo;\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 20\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tx1080\tqr\tx9360 \f16 {\b\f22 define constant} {\i bindings}{\b\f22 = }{\i init}\tab [Definition]\par \pard\plain \s2\li480\ri480\sa120 \f16 {\f22 define constant} creates read-only module variables in the current module. This form has the same syntax and semantics as {\f22 define variable} except that the variables created are read-only.\par \pard \s2\li480\ri480\sa120 \par \pard \s2\li480\ri480\sa120 Several other forms create module variables. See {\f22 define class}, {\f22 define generic}, and {\f22 define method} for more information.\par \pard \s2\li480\ri480\sa120 Source code is associated with a specific module through the programming environment. This association occurs at development time and cannot be changed at run-time. See the Modules chapter for more details about modules. \par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 A given module variable can only be defined once, except that multiple {\f22 define method} definitions with different specializers are allowed, together with at most one {\f22 define generic} definition.\par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Lexical Variables\par \pard\plain \s2\li480\ri480\sa120 \f16 Lexical variables are created locally and can be referenced only in a limited range of program text. They correspond to local variables in other languages.\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tx720\tqr\tx9360 \f16 {\b\f22 let}{\f22 }{\i bindings }{\f22 =} {\i init}\tab [Special Form]\par \pard\plain \s2\li480\ri480\sa120 \f16 {\f22 let} creates new lexical variables within the smallest enclosing implicit body containing the {\f22 let}. \par \pard \s2\li480\ri480\sa120 {\i bindings} are the variable(s) to be created, and may have the following forms:\par \tab \tab {\i variable}\par or \tab {\b\f22 ( }\{ {\i variable} \}* [ {\b\f22 #rest} {\i rest-variable-name } ]{\b\f22 )}\par \pard \s2\li480\ri480\sa120 where {\i variable} may be either {\i variable-name} or {\i variable-name} {\b\f22 ::} {\i type}. The {\i variable-names } and {\i rest-variable-name } must have the syntax of variable names, and are not evaluated. The values returned by {\i init} provide the initial values for the variable(s) specified by {\i bindings}.\par \pard \s2\li480\ri480\sa120\keepn In the simplest case, {\i bindings} is just a variable name, and {\i init} returns one value which is used to initialize that variable. See Checking Types and Binding Multiple Values for more complex cases.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ?} begin\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 let foo = 35;\par foo + foo\par end\par {\i 70\par \par }\pard\plain \s2\li480\ri480\sa120 \f16 A lexical variable shadows any module variable with the same name and any surrounding lexical variable with the same name. This rule means that the innermost version of a variable is the one referenced.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ? }define variable foo = 10;\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i foo\par ?} foo\par {\i 10\par ?} begin \par let foo = 20;\par let foo = 50;\par foo + foo\par end\par 100\par {\i ?} foo\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 10\par \pard\plain \s2\li480\ri480\sa120 \f16 The bindings introduced by the {\f22 let} are in effect until the end of the smallest enclosing implicit body containing the {\f22 let}.\par Several other syntax forms, including {\f22 local} and {\f22 block}, also create lexical variables as part of their operation.\par \pard \s2\li480\ri480\sa120 Method parameters are another example of lexical variables; they can be referenced only from the body of the method. See the chapter on Functions for more information on method parameters.\par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Checking Types\par \pard\plain \s2\li480\ri480\sa120 \f16 Variable bindings appearing in parameter lists and in statements like {\f22 let} and {\f22 define variable} may be specialized or unspecialized. Specializing a variable indicates that the variable may only hold values of the specified type. Specialized variables have the syntax {\i variable-name }{\b\f22 ::}{\i type . } Leaving a variable unspecialized indicates that it may hold values of any type. Unspecialized variables simply appear as {\i variable-name}.\par \pard \s2\li480\ri480\sa120 Each {\i type} is any expression that evaluates to a valid Dylan type (that is, an instance of {\f22 }). The {\i types} are evaluated before the {\i init}, in the same environment as {\i init}. Each {\i type} specifies the type of the corresponding {\i variable}. Attempts to initialize or assign the {\i variable} to a value which is not an instance of the corresponding {\i type} results in a type error being signalled.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ? }begin\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 let x :: = sqrt (2);\par x\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 end\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 error: 1.4142135623730951 is not an instance of \par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ? }define variable foo :: = 10;\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i foo\par ?} foo := sqrt (2)\par {\i 10\par error: 1.4142135623730951 is not an instance of \par }\pard\plain \s28\keep \f16 \par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Binding Multiple Values\par \pard\plain \s2\li480\ri480\sa120 \f16 Evaluating a Dylan expression can yield one value, more than one value, or no values at all. This capability is called {\b multiple values}.\par \pard \s2\li480\ri480\sa120 Multiple values are supported through two capabilities:\par \pard\plain \s21\fi-360\li840\ri480\sa160 \f16 \bullet \tab The function {\f22 values} is used to return multiple values.\par \pard \s21\fi-360\li840\ri480\sa160 \bullet \tab In the {\i bindings} parts of statements such as {\f22 let, define variable,} and{\f22 define constant}, {\i bindings} may have one of the following forms:\par \pard \s21\fi-360\li840\ri480\sa160 \tab \tab {\i variable}\tab or \tab {\b\f22 ( }\{ {\i variable} \}* [ {\b\f22 #rest} {\i rest-variable-name } ]{\b\f22 )}\par \tab The second form is used to receive multiple values.\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tqr\tx9360 \f16 {\b\f22 values} {\f22 #rest} {\i the-values} {\f23 \'de}{\i\f23 }{\i the-values}\tab [Function]\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 Returns {\i the-values} as multiple values.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ? }values(1, 2, 3);\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i 1\tab }// first value returned{\i \par 2\tab }// second value returned\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 {\i 3\tab }// third value returned\par \pard\plain \s2\li480\ri480\sa120 \f16 When variables are initialized to values returned by an {\i init} in a statement such as {\f22 let, define variable,} and{\f22 define constant}, the number of {\i variables} and the number of values are compared: \par \pard \s2\fi-360\li840\ri480\sa120 \bullet \tab If there are the same number of {\i variables} and values, the variables are initialized to the corresponding values.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ? }begin\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 let (foo, bar, baz) = values (1, 2, 3);\par list (foo, bar, baz)\par end\par #(1, 2, 3)\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ?} define method opposite-edges (center :: ,\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 radius :: );\par let (min, max) = edges (center, radius);\par values (max, min);\par end method;\par {\i opposite-edges\par ?} opposite-edges (100, 2);\par {\i 102\par }\pard\plain \s8\li960\ri480\sa120\tx2880\tx4800\tx6720 \f22\fs20 {\i 98}\par \pard\plain \s2\fi-360\li840\ri480\sa120 \f16 \bullet \tab If there are more {\i variables} than there are values returned by {\i init} , the remaining {\i variables} are initialized to {\f22 #f}. (If a specialized {\i variable} defaults to {\f22 #f} , and {\f22 #f} is not an instance of that variable's type, an error is signaled.)\par \bullet \tab If there are more values returned than there are {\i variables}, the excess values are placed in a sequence which is used as the initial value for {\i rest-variable}; if there is no {\i rest-variable}, these excess values are discarded. \par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ? }begin\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 let (#rest nums) = edges (100, 2);\par nums;\par end\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 #(98, 102)\par \pard\plain \s2\fi-360\li840\ri480\sa120 \f16 \bullet \tab If there is a {\i rest-variable} but there are no excess values, {\i rest-variable} is initialized to an empty sequence.\par \pard\plain \s6\li960\ri480\tx2880\tx4800\tx6720 \f22\fs20 {\i \par }\pard\plain \s2\li480\ri480\sa120 \f16 Multiple values can be used to perform parallel binding:\par \pard\plain \s5\li960\ri480\sb60\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ? }begin\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 let x = 10;\par let y = 20;\par let (x, y) = values (y, x);\par list (x, y);\par end\par \pard\plain \s8\li960\ri480\sa120\tx2880\tx4800\tx6720 \f22\fs20 {\i #(20, 10)\par }\pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Assignment\par \pard\plain \s2\li480\ri480\sa120 \f16 The Dylan special form {\f22 := }is used to set variables to new values. It can also be used as an alternate syntax for calling setter functions and macros.\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tqr\tx9360 \f16 {\i place }{\b\f22 := }{\i new-value} {\f23 \'de} {\i new-value}\tab [Special Form]\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 {\f22 :=} stores {\i new-value} in {\i place}. Subsequent evaluations of {\i place} will yield {\i new-value}. {\f22 :=} returns {\i new-value}.\par \pard \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 {\i place} commonly has the syntax of a variable name. Dylan also allows extended formats (described below). If {\i place} is not a variable name or one of these extended formats, an error is signaled. \par \pard \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 If {\i place} is a variable name, then {\i new-value} is stored in the corresponding variable (which may be a lexical or module variable). An error is signaled if there is no lexical or module variable corresponding to the {\i place} , if the corresponding variable is a read-only variable, or if the corresponding variable is specialized to some type and the {\i new-value} is not an instance of that type.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ? }define variable foo = 10;\par \pard\plain \s6\li960\ri-1260\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i 10\par ?} foo // this is a variable\par {\i 10 }// this is the variable\rquote s contents{\i \par ?} foo := 10 + 10;\par {\i 20\par ?} foo\par \pard \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 {\i 20\par \par }\pard\plain \s253\li475\ri480\sb120\sa120\keepn\brdrt\brdrs \b\f16\fs28 Extended form\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 If {\i place} is not a variable name, then it may have the syntax of a call to a function. This is called the {\b extended form} of {\f22 :=} . In this case, the function call indicates an accessible location. The call to {\f22 :=} should change what future accesses of the location will return.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ? }define variable foo = vector (10, 6, 8, 5);\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i foo\par ?} element(foo, 2)\par {\i 8\par ?} element(foo, 2) := "bar"\par {\i "bar"\par ?} foo\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 {\i #[10, 6, "bar", 5]}\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 In the general case,\par \pard\plain \s22\li960\ri480\sb120\sa240\tx2880\tx4800\tx6720 \f22\fs20 {\plain \i\f16 name}{\plain \f22 ( }{\plain \i\f16 arg}{\plain \i\f16\dn4 1}{\plain \i\f16 , ... arg}{\plain \i\f16\dn4 n}{\plain \f22\dn4 }{\plain \f22 ) := }{\plain \i\f16 new-value}{\plain \f22 \par }\pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 means roughly\par \pard\plain \s22\li960\ri480\sb120\sa240\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\plain \i\f16 name}{\plain \f22 -setter ( }{\plain \i\f16 new-value, arg}{\plain \i\f16\dn4 1}{\plain \i\f16 , ... arg}{\plain \i\f16\dn4 n}{\plain \f22\dn4 }{\plain \f22 ) \par }\pard\plain \s2\li480\ri480\sa120 \f16 In functional {\f22 :=}{\fs28 }expressions\par \pard \s2\li480\ri480\sa120 {\f22 \tab function-name(arg}{\f22\dn4 1}{\f22 , ..., arg}{\f22\dn4 n}{\f22 ) := value\par }\pard \s2\li480\ri480\sa120 where {\f22 function-name} is not a macro name, {\f22 arg}{\f22\fs18\dn4 1}{\f22 ... arg}{\f22\fs18\dn4 n} are evaluated first in that order, followed by {\f22 new-value}.\par The evaluation time of the variable {\f22 function-name-setter}, which this expression is defined to invoke, is unspecified.\par \pard\plain \s253\li475\ri480\sb120\sa120\keepn\brdrt\brdrs \b\f16\fs28 Extended form using array reference or slot reference syntax\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 The special syntactic shorthands for array reference and slot reference are also allowed as part of the extended form of :=.\par \pard \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 For example, the two forms\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 foo[2] := "quux"\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 element (foo, 2) := "quux"\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 are equivalent to\par \pard\plain \s22\li960\ri480\sb120\sa240\keepn\tx2880\tx4800\tx6720 \f22\fs20 element-setter ("quux", foo, 2).\par \pard\plain \s2\li480\ri480\sa120 \f16 In {\f22\fs20 .} := expressions\par \pard \s2\li480\ri480\sa120 {\f22 \tab argument.function-name := value\par }\pard \s2\li480\ri480\sa120 where {\f22 function-name} is not a macro name, {\f22 argument} is evaluated first, followed by {\f22 value}.\par The evaluation time of the variable {\f22 function-name-setter}, which this expression is defined to invoke, is unspecified.\par \pard \s2\li480\ri480\sa120 In implicit element {\f22 :=} expressions\par \pard \s2\li480\ri480\sa120 {\f22 \tab sequence[arg}{\f22\dn4 1}{\f22 , \'c9, arg}{\f22\dn4 n}{\f22 ] := value}\par \pard \s2\li480\ri480\sa120 {\f22 sequence} is evaluated first, then {\f22 arg}{\f22\fs18\dn4 1}{\f22 \'c9 arg}{\f22\fs18\dn4 n} in that order, then {\f22 value}. The evaluation time of the variable {\f22 element-setter} , which this expression is defined to invoke, is unspecified.\par \pard \s2\li480\ri480\sa120 \par \pard\plain \s255\li475\ri475\sb80\sa360\keepn\pagebb\brdrt\brdrs \b\f16\fs48 4. Control Constructs\par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 True and False\par \pard\plain \s2\li480\ri480\sa120 \f16 In Dylan, there is a single object that counts as false. This object is indicated by the syntax {\b\f22 #f}{\fs16\up6 \chftn {\footnote \pard\plain \s246\li480\ri480 \f16\fs20 {\fs18\up6 \chftn } This is in sharp distinction to C, which equates 0 and the false value, and some dialects of Lisp, which equate the empty-list and the false value.}} . All other values are considered true for the sake of true/false testing. The canonical true object, indicated by the syntax {\b\f22 #t}, can be used for clarity of code.\par \pard \s2\li480\ri480\sa120 Throughout the manual, the phrases \ldblquote true,\rdblquote \ldblquote false,\rdblquote \ldblquote returns true,\rdblquote and \ldblquote returns false\rdblquote are used. It\rquote s important to remember that \ldblquote returns true\rdblquote does not necessarily mean \ldblquote returns the object {\f22 #t}.\rdblquote It simply means \ldblquote returns any object besides {\f22 #f}.\rdblquote Sometimes for efficiency, debuggability, or informational value, some value besides {\f22 #t} is returned.\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tqr\tx9360 \f16 {\b\f22 #t}\tab \tab [Object]\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 This is the canonical true value. In Dylan, all objects besides {\f22 #f} count as true, not just {\f22 #t}.\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tqr\tx9360 \f16 {\b\f22 #f}\tab \tab [Object]\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 This is the only false value.\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tqr\tx9360 \f16 {\b\f22 ~} {\i thing} {\f23 \'de} {\i boolean}\tab [Function]\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 Returns true if {\i thing} is {\f22 #f}; otherwise returns false.\par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Conditionals\par \pard\plain \s2\li480\ri480\sa120 \f16 The following syntax forms are used to perform conditional execution.\par \pard\plain \s9\fi-480\li480\ri480\sa120\keepn\tx480\tx720\tqr\tx9360 \f16 {\b\f22 if} {\b\f22 (}{\f22 }{\i test}{\f22 }{\b\f22 )}{\f22 }{\i consequent}{\f22 \line }[{\b\f22 elseif} {\b\f22 (}{\f22 }{\i elseif-test}{\f22 }{\b\f22 )}{\i elseif-consequent}]*{\f22 \line }[{\b\f22 else} {\i alternate}]{\f22 \line \tab }{\b\f22 end} [{\b\f22 if} ] {\f23 \'de} {\i values}\tab [Special Form]\par \pard\plain \s2\li480\ri480\sa120 \f16 {\f22 if} evaluates {\i test}. If {\i test} evaluates to a true value, {\i consequent} is evaluated. \par \pard \s2\li480\ri480\sa120 If {\i test} evaluates to {\f22 #f} (the only false value), and any {\f22 elseif} clauses were supplied, the first {\i elseif-test } is evaluated. If that{\i elseif-test} evaluates to a true value, its {\i elseif-consequent} is evaluated. If it evaluates to {\f22 #f}, the next {\i elseif-test} is evaluated, and so on.\par \pard \s2\li480\ri480\sa120 Finally, if {\i test} and each {\i elseif-test} evaluates to #f, and {\i alternate} was supplied, the {\i alternate} is evaluated. If {\i alternate} was not supplied, #f is returned.\par \pard \s2\li480\ri480\sa120 The {\i consequent} , {\i elseif-consequent}, and {\i alternate} sections of an {\f22 if} statement are all implicit bodies (may contain any number of expressions, separated by semicolons). The values of the last expression in the appropriate {\i consequent} , {\i elseif-consequent}, or {\i alternate} form (whichever is evaluated) are returned.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ? }define method test (thing :: ){\i \par }\pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 if (thing)\par #t\par else\par #f\par end if\par end method;\par {\i test\par ?} test ("hello")\par {\i #t\par ?} test (#t)\par {\i #t\par ?} test (#f)\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 #f\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 {\i ? }define method show-and-tell (thing :: );\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 if (thing)\par print (thing);\par #t\par else\par #f\par end if\par end method;\par {\i show-and-tell\par ?} show-and-tell("hello")\par {\i hello\par }\pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 #t\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tx720\tqr\tx9360 \f16 {\b\f22 unless}{\f22 }{\b\f22 (}{\f22 }{\i test}{\f22 }{\b\f22 )}\tab [Macro]\line \tab {\i body}{\f22 \line \tab }{\b\f22 end} [{\b\f22 unless}]{\i \line }{\f23 \'de} { \i values}\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 {\f22 unless} evaluates {\i test}. {\i body} is an implicit body. If {\i test} evaluates to a true value, then none of the expressions in the {\i body} are evaluated and {\f22 #f} is returned. If {\i test} evaluates to {\f22 #f}, then all the expressions in the {\i body} are evaluated and the values of the last expression are returned.\par \pard \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 If there are no expressions in the {\i body}, then {\f22 #f} is returned.\par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 unless(detect-gas? (nose))\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 light(match)\par end unless\par {\b \par }\pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tx720\tqr\tx9360 \f16 {\b\f22 case}{\f22 }\tab [Macro]{\f22 \line \tab }{\i test}{\i\fs14\dn4 1} {\b\f22 =>} {\i body}{\i\fs14\dn4 1}{\fs14\dn4 }{\b\f22 ;}{\f22 \line \tab }... \line \tab {\i test}{\i\fs14\dn4 n} {\b\f22 =>} {\i body}{\i\fs14\dn4 n} {\b\f22 ;}\line \tab [{\b\f22 otherwise => }{\i otherwise-body} ] [{\b\f22 ;}]\line \tab {\b\f22 end}{\f22 }{\fs28 [}{\b\f22 case}{\fs28 ]\line }{\f23 \'de}{\i values}\par \pard\plain \s2\li480\ri480\sa120 \f16 {\f22 case} evaluates the {\i tests} in order. \par \pard \s2\li480\ri480\sa120 If a {\i test} returns true, the corresponding {\i body }is evaluated, and the last expression in the {\i body} is returned. If there is no {\i body } in the {\i test} that succeeds, then the first value of the {\i test} is returned. Subsequent {\i tests} are not evaluated.\par \pard \s2\li480\ri480\sa120 If no {\i test} is true, then {\f22 case} returns {\f22 #f}.\par \pard \s2\li480\ri480\sa120 As a special case, the keyword {\f22 otherwise} may appear as a {\i test}. This {\i test} always succeeds if there are no other successful {\i tests}. \par \pard\plain \s5\li960\ri480\sb120\keepn\tx2880\tx4800\tx6720 \f22\fs20 case\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 player1.money <= 0\par => end-game(player1);\par player2.money <= 0\par => end-game(player2);\par otherwise\par => move(player1);\par move(player2);\par \pard\plain \s8\li960\ri480\sa320\tx2880\tx4800\tx6720 \f22\fs20 end case;\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tx720\tqr\tx9360 \f16 {\b\f22 select} {\b\f22 (}{\f22 }{\i target-form} [{\b\f22 by} {\i test }] {\b\f22 )}\tab [Macro]\line \tab {\i match}{\i\fs18\dn4 1a}{\fs18 }{\b\f22\fs18 , }{\fs18 ... }{\i match-}{\i\fs18\dn4 1n} {\b\f22 => } {\i body}{\i\fs18\dn4 1};\line \tab ...\line \tab {\i match}{\i\fs18\dn4 ma}{\fs18 }{\b\f22\fs18 ,}{\fs18 ... }{\i match-}{\i\fs18\dn4 mn} {\b\f22 =>} {\i body}{\i\fs18\dn4 m};\line \tab [{ \b\f22 otherwise =>} {\i otherwise-body} ] [{\b\f22 ;}]\line \tab {\b\f22 end} [{\b\f22 select}]\line {\f23 \'de}{\i values}\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 Each {\i match-list} must be a sequence of expressions separated by commas, or the keyword {\f22 otherwise}. The {\i bodies} are implicit bodies (may be any number of expressions separated by semicolons).\par \pard\plain \s2\li480\ri480\sa120 \f16 When the {\f22 select} form is entered, the {\i target-form} is evaluated, generating a {\b target value}. \par \pard\plain \fi-360\li840\ri480 \f16 The{\f22\fs20 }clauses are tested in order, using the following procedure:\par \par \pard\plain \s21\fi-360\li840\ri480\sa160 \f16 \bullet \tab Each {\i match i}s evaluated to produce a match object. The match expressions are evaluated in order. \par \pard \s21\fi-360\li840\ri480\sa160 \bullet \tab If a {\i test} function was supplied, this is used to compare the target object to the match objects. Otherwise, the match objects are compared to the target value with {\f22 ==}.\par \bullet \tab If a match is found, the corresponding {\i body } form is evaluated and the values of the last expression in the {\i body} are returned. If there is no {\i body } form in the matching clause, {\f22 #f} is returned.\par \pard \s21\fi-360\li840\ri480\sa160 \bullet \tab Once a match is found, the following match-object and body expressions, in this and subsequent clauses, are not evaluated at all.\par \pard\plain \s11\fi-360\li840\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 \bullet \tab If there is no matching clause, an error is signaled. \par \pard \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 As a special case, the keyword {\f22 otherwise} may appear instead of a match-list. This clause will be considered a match if no other match is found. Because an {\f22 otherwise} clause matches when no other clause matches, a {\f22 select} form that includes an {\f22 otherwise} clause will never signal an error for failure to match.\par \pard\plain \s2\li480\ri480\sa120 \f16 Since testing stops when the first match is found, it is irrelevant whether the test function would also have returned true if called on later match-list elements of the same clause or on match-list elements of later clauses.\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 select ( career-choice(student) )\par art:, music:, drama:\par => "Don\rquote t quit your day job";\par literature:, history:, linguistics:\par => "That really is fascinating";\par science:, math:, engineering:\par => "Say, can you fix my VCR?";\par otherwise => "I wish you luck";\par end select;\par \pard\plain \li720\ri480 \f16 {\f3\fs20 \par }\pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 Dylan\rquote s {\f22 select} with a test of {\f22 instance?} is similar in effect to a Common Lisp {\f22 typecase} statement.\par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 select ( my-object by instance? )\par , , => "a graphical object";\par , , => "a computational object";\par otherwise => "I don\rquote t know";\par end select\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tqr\tx9360 \f16 {\i form1} {\b\f22 |} {\i form2} {\f23 \'de}{\i values}\tab [Macro]\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 {\f22 |} (\ldblquote or\rdblquote ) evaluates the {\i forms} from left to right. If {\i form1} returns true as its first value, that value is returned and {\i form2} is not evaluated. If {\i form1} returns {\f22 #f }as its first value, {\i form2} is evaluated and its values are returned.\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tqr\tx9360 \f16 {\i form1 } {\b\f22 &} {\i form2} {\f23 \'de}{\i values}\tab [Macro]\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 {\f22 &} (\ldblquote and\rdblquote ) evaluates the {\i forms} from left to right. If {\i form1} returns {\f22 #f} as its first value, {\f22 #f }is returned and {\i form2} is not evaluated. If {\i form1} returns true as its first value, {\i form2} is evaluated and its values are returned.\par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Iteration Constructs\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tx720\tx900\tqr\tx9360 \f16 {\b\f22 while} {\b\f22 (}{\f22 }{\i test} {\b\f22 )}{\f22 }\tab [Macro]\line {\i \tab \tab body\line }\tab {\b\f22 end} {\fs28 [}{\b\f22 while}{\fs28 ]} \line { \f23 \'de}{\i\f23 }{\i }{\f22 #f}\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 {\f22 while} evaluates the expressions in the {\i body} over and over until the {\i test} returns false.\par \pard \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 Each pass through the loop begins by evaluating {\i test}. If {\i test} evaluates to a true value, the expressions in the {\i body} {\i }are evaluated and the looping continues. If {\i test} evaluates to {\f22 #f}, the loop terminates and {\f22 while} returns {\f22 #f}.\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tx720\tx900\tqr\tx9360 \f16 {\b\f22 until} {\f22 }{\b\f22 (} {\i test} {\b\f22 )}{\f22 }\tab [Macro]\line \tab \tab {\i body}\line \tab {\b\f22 end} {\fs28 [}{\b\f22 until}{\fs28 ]}{\i } \line {\f23 \'de}{\i\f23 }{\i }{\f22 #f}\par \pard\plain \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 \f16 {\f22 until} evaluates the expressions in the {\i body} over and over until the {\i test} returns true.\par \pard \s11\li480\ri480\sa120\tqr\tx8640\tqr\tx9360 Each pass through the loop begins by evaluating {\i test}. If {\i test} evaluates to {\f22 #f}, the expressions in the {\i body} {\i }are evaluated and the looping continues. If {\i test} evaluates to a true value, the loop terminates and {\f22 until} returns {\f22 #f}.\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keep\keepn\tx480\tqr\tx9360 \f16 {\b\f22 for}{\f22 }{\b\f22 (}{\f22 }{\i clauses }\tab [Special Form]\line {\f22 }[\{{\b\f22 until} | {\b\f22 while}\} {\i end-test}] {\b\f22 )} \line {\i body}\line { \f22 [}{\b\f22 finally}{\f22 }{\i result-body}{\f22 ] \line }{\b\f22 end} [{\b\f22 for}]\line {\f23 \'de} {\i values\par }\pard\plain \li480\ri480 \f16 {\f22 for} allows one or more {\i clauses}. Each {\i clause}{\f22\fs20 }controls one {\b iteration variable} throughout the iteration. The optional {\i end-test}{\i\f22\fs20 } controls whether the iteration continues or terminates. It does not control any iteration variables.\par \pard \li1440\ri480 {\fs20 \par }\pard \li480\ri480 The kinds of {\i clauses}{\i\f22\fs20 }include {\b explicit step clauses}, {\b collection clauses}{\fs18\up6 \chftn {\footnote \pard\plain \s246\li480\ri480 \f16\fs20 {\fs18\up6 \chftn } Note that collection clauses are implemented using forward-iteration-protocol.}}, and {\b numeric clauses}: \par \pard \li480\ri480 \par {\ul Explicit step clauses\par }\par {\f22 \{}{\i variable}{\i\f22 }{\f22 | }{\i variable}{\f22 :: }{\i type}{\f22 \} }{\b\f22 =}{\f22 }{\i init-value}{\i\f22 }{\b\f22 then}{\f22 }{\i next-value}{\i\f22 \par }\pard \li1440\ri480 {\fs20 \par }\pard \li480\ri480 {\ul Collection clauses\par }\pard \li1440\ri480 {\fs20 \par }\pard \li480\ri480 {\f22 \{}{\i variable}{\i\f22 }{\f22 | }{\i variable}{\f22 :: }{\i type}{\f22 \} }{\b\f22 in}{\f22 }{\i collection}{\i\f22 \par }\pard \li1440\ri480 {\fs20 \par }\pard \li480\ri480 {\ul Numeric clauses}\par \pard \li480\ri480 {\f22\fs20 \par }\pard \li480\ri480\tx3780 {\f22 \{}{\i variable}{\i\f22 }{\f22 | }{\i variable}{\f22 :: }{\i type}{\f22 \}\tab }{\b\f22 from}{\f22 }{\i start}{\i\f22 \par }\pard \li480\ri480 {\i\f22 \tab }{\f22 [\{}{\b\f22 to}{\f22 | }{\b\f22 above}{\f22 | }{\b\f22 below}{\f22 \} }{\i bound}{\f22 ]\par \tab \tab \tab [}{\b\f22 by}{\f22 }{\i increment}{\f22 ]\par }\pard \li480\ri480 {\f22 \par }\pard \li2160\ri480 {\fs20 \par }\pard \li480\ri480 Iteration with {\f22 for}{\fs20 }proceeds through the following steps:\par \par \pard \fi-360\li1080\ri480 1) \tab Evaluate the expressions that are evaluated just once, in left to right order as they appear in the {\f22 for}{\f22\fs20 }statement. \par \pard \fi-360\li1440\ri280 \bullet \~\tab For explicit step clauses, these expressions are {\i type}{\i\f22\fs20 }and {\i init-value}{\fs28 .}\par \pard \fi-360\li1440\ri480 \bullet \~\tab For collection clauses, these are {\i type}{\i\f22\fs20 }and {\i collection}. If the value of {\i collection}{\i\f22\fs20 }is not a collection, signal an error. \par \bullet \~\tab For numeric clauses, these are {\i type}, {\i start}, {\i bound} if it is supplied, and {\i increment} if it is supplied. If {\i increment}{\i\f22\fs20 }is not supplied, it defaults to 1.\par \pard \fi-360\li1080\ri480 \par 2)\tab Bind the iteration variables of explicit step and numeric clauses.\par \pard \fi-360\li1440\ri480 \bullet \tab For each explicit step clause, bind {\i variable }to the value of {\i init-value}. If {\i type }is supplied and the value of {\i init-value }is not of the specified type, signal an error. \par \bullet \tab For each numeric clause, bind {\i variable }to the value of {\i start}. If {\i type }is supplied and the value of {\i start }is not of the specified type, signal an error.\par \pard \fi-360\li1080\ri480 \par \pard \fi-360\li1080\ri480 3)\tab Check numeric and collection clauses for exhaustion. If a clause is exhausted, go to step 9.\par \pard \fi-360\li1440\ri480 \bullet \tab A collection clause is exhausted if its collection has no next element.\par \pard \li1440\ri480 {\fs20 \par }\pard \fi-360\li1440\ri480 \bullet \tab Numeric clauses cannot be exhausted if {\i bound }is not supplied. If {\i bound }is supplied, the following table gives the conditions for exhaustion:\par \pard \fi-360\li1440\ri480 \par \trowd \trqc\trgaph14\trleft1426 \clbrdrt\brdrs \clbrdrl\brdrs \clbrdrb\brdrs \clbrdrr\brdrs \clshdng0\cellx3010\clbrdrt\brdrs \clbrdrl\brdrs \clbrdrb\brdrs \clbrdrr\brdrs \clshdng0\cellx5890\clbrdrt\brdrs \clbrdrl\brdrs \clbrdrb\brdrs \clbrdrr\brdrs \clshdng0\cellx8650\pard \qr\li180\ri376\intbl {\fs20 \cell }\pard \qc\li180\ri180\intbl {\i\f22\fs20 increment }{\f22\fs20 >= 0}{\fs20 \cell }\pard \qc\li180\ri180\intbl {\i\f22\fs20 increment }{\f22\fs20 < 0}{\fs20 \cell }\pard \intbl {\fs20 \row }\trowd \trqc\trgaph14\trleft1426 \clbrdrt\brdrs \clbrdrl\brdrs \clbrdrb\brdrs \clbrdrr\brdrs \clshdng0\cellx3010\clbrdrt\brdrs \clbrdrl\brdrs \clbrdrb\brdrs \clbrdrr\brdrs \clshdng0\cellx5890\clbrdrt\brdrs \clbrdrl\brdrs \clbrdrb\brdrs \clbrdrr\brdrs \clshdng0\cellx8650\pard \qr\li180\ri376\intbl {\f22\fs20 to}{\fs20 \cell }\pard \qc\li180\ri180\intbl {\i\f22\fs20 variable }{\f22\fs20 > }{\i\f22\fs20 bound}{\fs20 \cell }\pard \qc\li180\ri180\intbl {\i\f22\fs20 variable }{\f22\fs20 < }{\i\f22\fs20 bound}{\fs20 \cell }\pard \intbl {\fs20 \row }\pard \qr\li180\ri376\intbl {\f22\fs20 above}{\fs20 \cell }\pard \qc\li180\ri180\intbl {\i\f22\fs20 variable }{\f22\fs20 <= }{\i\f22\fs20 bound}{\fs20 \cell }\pard \qc\li180\ri180\intbl {\i\f22\fs20 variable } {\f22\fs20 <= }{\i\f22\fs20 bound}{\fs20 \cell }\pard \intbl {\fs20 \row }\trowd \trqc\trgaph14\trleft1426 \clbrdrt\brdrs \clbrdrl\brdrs \clbrdrb\brdrs \clbrdrr\brdrs \clshdng0\cellx3010\clbrdrt\brdrs \clbrdrl\brdrs \clbrdrb\brdrs \clbrdrr\brdrs \clshdng0 \cellx5890\clbrdrt\brdrs \clbrdrl\brdrs \clbrdrb\brdrs \clbrdrr\brdrs \clshdng0\cellx8650\pard \qr\li180\ri376\intbl {\f22\fs20 below}{\fs20 \cell }\pard \qc\li180\ri180\intbl {\i\f22\fs20 variable }{\f22\fs20 >= }{\i\f22\fs20 bound}{\fs20 \cell }\pard \qc\li180\ri180\intbl {\i\f22\fs20 variable }{\f22\fs20 >= }{\i\f22\fs20 bound}{\fs20 \cell }\pard \intbl {\fs20 \row }\pard \fi-360\li1080\ri480 \par \pard \fi-360\li1080\ri480 4)\tab Bind the iteration variables of collection clauses. Fresh bindings are created each time through the iteration.\par \pard \fi-360\li1440\ri480 \bullet \tab For each collection clause, bind {\i variable }to the next element of the collection for that clause. If {\i type }is supplied and this next element of the collection{\i } is not of the specified type, signal an error.\par \pard \fi-360\li1440\ri480 \par \pard \fi-360\li1080\ri480 5) \tab If {\i end-test} is supplied, evaluate it. The termination conditions depend on the symbol used to introduce the {\i end-test }in the {\f22 for }statement.\par \pard \fi-360\li1440\ri180 \bullet \tab If the value of {\i end-test }is false and the symbol is {\f22 while}, go to step 9.\par \bullet \tab If the value of {\i end-test }is true and the symbol is {\f22 until}, go to step 9.\par \pard \fi-360\li1080\ri480 \par \pard \fi-360\li1080\ri480 6) \tab Execute the expressions in the {\i body} in sequence. The expressions in the {\i body} are used to produce side-effects.\par \pard \fi-360\li1080\ri480 \par \pard \fi-360\li1080\ri480 7) \tab Obtain the next values for explicit step and numeric clauses. Values are obtained in left to right order, in the environment produced by step 6.\par \pard \fi-360\li1440\ri480 \bullet \tab For each explicit step clause, evaluate {\i next-value}.\par \pard \fi-360\li1440\ri100 \bullet \tab For each numeric clause, add the values of {\i variable }and {\i increment}.\par \pard \fi-360\li1080\ri480 \par \pard \fi-360\li1080\ri480 8)\tab Bind the iteration variables of explicit step and numeric clauses to the values obtained in step 7. For each clause, if {\i type }is supplied and the next value for that clause{\i } is not of the specified type, signal an error. Fresh bindings are created each time through the iteration. After variables are bound, go to step 3.\par \pard \fi-360\li1080\ri480 \par \pard \fi-360\li1080\ri480 9) \tab Evaluate the expressions in the {\i result-body} in sequence. Bindings created in step 2 and 8 {\ul are} visible during the execution of {\i result-body} . Bindings created in step 4 (i.e. the iteration variables of collection clauses) {\ul are not} visible during the execution of {\i result-body}. The values of the last expression in the {\i result-body} are returned by the {\f22 for } statement. If there are no expressions in the {\i result-body}, {\f22 for} returns {\f22 #f}.\par \pard \li480\ri480 {\b \par Examples of }{\b\f22 for}{\b :\par \par }\pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 for ( thing = first-thing then next(thing),\par until done?(thing) )\par do-some(thing)\par end;\par \pard\plain \s2\li480\ri480\sa120 \f16 \par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 for (j :: from 0 to height)\par for (i :: from 0 to width)\par erase(i,j);\par plot (i,j);\par end for;\par end for;\par \pard\plain \s2\li480\ri480\sa120 \f16 \par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 for (city in olympic-cities,\par year from start-year by 4)\par schedule-olympic-game(city, year)\par finally notify(press);\par sell(tickets);\par end;\par \pard\plain \s2\li480\ri480\sa120 \f16 \par \pard\plain \s6\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 for (i from 0 below 100,\par zombies from 0 below 100,\par normals from 100 above 0 by -1)\par population[i] := zombies + normals\par end;\par \pard\plain \s2\li480\ri480\sa120 \f16 \par \pard\plain \s254\li480\ri480\sb240\sa240\keepn\brdrt\brdrs \b\f16\fs36 Non-local Exits\par \pard\plain \s9\fi-480\li480\ri480\sb240\sa120\keepn\tx480\tx720\tx900\tqr\tx9360 \f16 {\b\f22 block} {\b\f22 ( }[{\i exit-var}] {\b\f22 )} \tab [Special Form]\line \tab \tab {\i body} \line \tab \tab [{\b\f22 cleanup} {\i cleanup-clause} | {\b\f22 exception} {\i exception-clause} ]*\line {\f22 \tab }{\b\f22 end}{\f22 }[ {\b\f22 block}{\f22 }]\line {\f23 \'de} {\i values}\par \pard\plain \s2\li480\ri480\sa120 \f16 {\f22 block} is a construct which combines the functionality of non-local exits, protected forms, and exception handling. block executes the expressions in the {\i body} in sequence, and then the optional {\i cleanup-clauses}. Any number of {\i cleanup-clauses} may be provided, and they may be arbitrarily interleaved with {\i exception-clauses} (described in the chapter on Conditions). Normally, the value returned by {\f22 block} is the value of the last expression in the {\i body}. If there are no expressions in the {\i body}, {\f22 #f} is returned.\par If {\i exit-var} is provided, {\i exit-var} is bound to an {\b exit procedure} during the execution of the {\i body} and {\i clauses}. At any point in time before the last {\i clause} returns, the exit procedure can be called. Calling the exit procedure has the effect of immediately terminating the execution of the {\i body} . The exit procedure accepts any number of arguments. These arguments are used as the return values of block. Calling an exit procedure is known as performing a {\b non-local exit.}\par \pard \s2\li480\ri480\sa120 Generally, the {\i cleanup-clauses} are guaranteed to be executed. Even if one of the expressions in the {\i body} is terminated by a non-local exit out of the {\f22 block}, the {\i cleanup-clauses} are executed before the non-local exit can complete. \par \pard \s2\li480\ri480\sa120 For example, the following code fragment ensures that files are closed even in the case of an error causing a non-local exit from the block body:\par \pard\plain \s25\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 block (return)\par open-files();\par ...\par if (error)\par return(#f);\par end if;\par ...\par result\par cleanup\par close-files();\par end block\par \pard\plain \li480\ri480 \f16 \par \pard\plain \s2\li480\ri480\sa120 \f16 However, if one of the {\i cleanup-clauses} is terminated by a non-local exit out of the {\f22 block}, any following {\i cleanup-clauses} within the same {\f22 block} are not executed.\par \pard\plain \li480\ri480 \f16 \par \pard \li480\ri480 {\b\fs28 Restrictions on the use of exit procedures\par }\pard \li480\ri480 \par \pard \li480\ri480 The exit procedure is a first-class object. Specifically, it can be passed as an argument to functions, stored in data structures, etc. Its use is not restricted to the lexical body of the {\b establishing block} (the block in which that exit procedure was established). Ho wever, invocation of the exit procedure is valid only during the execution of the establishing block. It is an error to invoke an exit procedure after its establishing block has returned, or after execution of the establishing block has been terminated by a non-local exit.\par \pard \li480\ri480 \par \pard \li480\ri480 In the following example, the {\f22 block} establishes an exit procedure and binds {\f22 bar} to that exit procedure. The {\f22 block} returns an anonymous method containing a call to {\f22 bar}. The anonymous method is then bound to {\f22 foo}. Calling the {\f22 foo} method is an error because it is no longer valid to invoke {\f22 bar} after its establishing block returns.\par \pard \li480\ri480 \par \pard\plain \s25\li960\ri480\keepn\tx2880\tx4800\tx6720 \f22\fs20 ? define constant foo =\par block (bar)\par method (n) bar(n) end;\par end block;\par ? foo(5)\par {\i error or other undefined consequences\par }\pard\plain \li480\ri480 \f16 \par \pard \li480\ri480 When an exit procedure is called, it initiates a non-local exit out of its establishing block. Before the non-local exit can complete, however, the cleanup clauses of {\b intervening blocks} (blocks that have been entered, but not exited, since the establishing block was entered) must b e executed, beginning with the most recently entered intervening block. Once the cleanup clauses of an intervening block have been executed, it is an error to invoke the exit procedure established by that block. Finally, the cleanup clauses of the estab lishing block are executed, further invocation of the exit procedure becomes invalid, and the establishing block returns with the