About This E-Book
EPUB is an open, industry-standard format for e-books. However, support for EPUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturers Web site.
Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the e-book in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a Click here to view code image link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
The Dart Programming Language
Gilad Bracha
Boston Columbus Indianapolis New York San Francisco Amsterdam Cape Town
Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City
Sao Paulo Sidney Hong Kong Seoul Singapore Taipei Tokyo
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) 382-3419.
For government sales inquiries, please contact .
For questions about sales outside the United States, please contact .
Visit us on the Web: informit.com/aw
Library of Congress Control Number: 2015953614
Copyright 2016 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/.
ISBN-13: 978-0-321-92770-5
ISBN-10: 0-321-92770-2
Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.
First printing, December 2015
To my mother, Shoshana,
who taught me to be picky.
Contents
Foreword
In the early spring of 2006, I wrote a short blog post called Gilad is Right where, as a recovering typaholic, I admitted that Gilads idea of optional and layered type systems, where static types cannot change the runtime behavior of the program and do not prevent an otherwise legal program from compiling or executing, was a necessary design trade-off for programming languages aimed at millions of developers. At that time I was working on Visual Basic, which already supported a form of optional typing by means of the Option Strict Off
statement, but that feature was under heavy fire from static typing proponents. Type systems are often highly non-linear and after a certain point their complexity explodes while adding very little value to the developer and making life miserable for the language implementors. Optional and layered type systems enable a much more gradual approach by allowing strong static typing to coexist peacefully with dynamic typing. Now nearly a decade later, the vision Gilad pioneered has become mainstream under the name gradual typing. Many programming languages that have been created in the last few years, such as Hack, TypeScript, Flow, Racket, and of course Dart, are gradually typed. Even academics have embraced the idea and write papers about it with frivolous titles that include words such as threesomes and blame.
Another pragmatic aspect of Dart, but one that language purists have not yet accepted, is the fact that the Dart type system is deliberately unsound. In normal English this means that the Dart type checker will not flag certain type errors at compile time, but relies on runtime checking instead to ensure type safety. The main source of type unsoundness in Dart is covariant generics. To explain what variance is, lets first look at a vending machine from which we can only take drinks. If a cafeteria requires a vending machine with soda pop, we can legally install a vending machine that dispenses root beer since root beer is a type of soda pop (but it is illegal to install a vending machine for soda pop where a vending machine for root beer is required). In programming language speak we say that vending machines are covariant. Next lets take a look at garbage cans into which we can throw only garbage. If a cafeteria requires a garbage can for recyclables, we can legally install a garbage can for trash since recyclable garbage is a type of trash (but it is illegal to install a garbage can for recyclables where a garbage can for trash is required). In programming language speak we say that garbage cans are contravariant. If you are a little puzzled about contravariance you are not the only one, and you will appreciate Darts decision to make all generic types covariant. The consequence of that choice is that if you need a garbage can for trash, you can legally install a garbage can for recyclables, but that garbage can will reject all non-recyclable trash that people are trying to dump in it. While theoretically unsound, unsafe variance actually feels rather natural for most developers, and I applaud the choice the Dart designers made here. As anyone that has struggled with ?
super and ?
extends can attest, languages that have chosen in favor of static type safety for generics do so at the expense of their users.
The Dart language designers made additional pragmatic choices that make coding in Dart a smooth experience. For example Dart has no interfaces, abstract base classes, or normal classes. Instead Dart only has classes that can be used as interfaces by implementing them, or used as base classes by extending them, or have their implementation reused by mixing them in. Every type in Dart is an object, so there is no difference between primitive (e.g., numeric) types and regular object types. Even though everything in Dart is an object, it is possible to define top-level functions and variables, so one no longer needs the dreaded public static void main
incantation inside a top-level class to get going. Dart allows user-defined arithmetic operators, but does not support type-based overloading of methods. This significantly simplifies the language. In other programming languages that do support type-based overloading, the exact semantics of that feature often take up an unjustifiably large fraction of the language specification. Null-aware operators (even