Kotlin in Action Dmitry Jemerov Svetlana Isakova Copyright For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2017 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Development editor: Dan Maharry Review editor: Aleksandar Dragosavljević Technical development editor: Brent Watson Project editor: Kevin Sullivan Copyeditor: Tiffany Taylor Proofreader: Elizabeth Martin Technical proofreader: Igor Wojda Typesetter: Marija Tudor Cover designer: Marija Tudor ISBN 9781617293290 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – EBM – 22 21 20 19 18 17 Brief Table of Contents Copyright Brief Table of Contents Table of Contents Foreword Preface Acknowledgments About this Book About the Authors About the Cover Illustration 1. Introducing Kotlin Chapter 1. Kotlin: what and why Chapter 2. Kotlin basics Chapter 3. Defining and calling functions Chapter 4. Classes, objects, and interfaces Chapter 5. Programming with lambdas Chapter 6. The Kotlin type system 2. Embracing Kotlin Chapter 7. Operator overloading and other conventions Chapter 8. Higher-order functions: lambdas as parameters and return values Chapter 9. Generics Chapter 10. Annotations and reflection Chapter 11. DSL construction Appendix A. Building Kotlin projects Appendix B. Documenting Kotlin code Appendix C. The Kotlin ecosystem Index List of Figures List of Tables List of Listings Table of Contents Copyright Brief Table of Contents Table of Contents Foreword Preface Acknowledgments About this Book About the Authors About the Cover Illustration 1. Introducing Kotlin Chapter 1. Kotlin: what and why 1.1. A taste of Kotlin 1.2. Kotlin’s primary traits 1.2.1. Target platforms: server-side, Android, anywhere Java runs 1.2.2. Statically typed 1.2.3. Functional and object-oriented 1.2.4. Free and open source 1.3. Kotlin applications 1.3.1. Kotlin on the server side 1.3.2. Kotlin on Android 1.4. The philosophy of Kotlin 1.4.1. Pragmatic 1.4.2. Concise 1.4.3. Safe 1.4.4. Interoperable 1.5. Using the Kotlin tools 1.5.1. Compiling Kotlin code 1.5.2. Plug-in for IntelliJ IDEA and Android Studio 1.5.3. Interactive shell 1.5.4. Eclipse plug-in 1.5.5. Online playground 1.5.6. Java-to-Kotlin converter 1.6. Summary Chapter 2. Kotlin basics 2.1. Basic elements: functions and variables 2.1.1. Hello, world! 2.1.2. Functions 2.1.3. Variables 2.1.4. Easier string formatting: string templates 2.2. Classes and properties 2.2.1. Properties 2.2.2. Custom accessors 2.2.3. Kotlin source code layout: directories and packages 2.3. Representing and handling choices: enums and “when” 2.3.1. Declaring enum classes 2.3.2. Using “when” to deal with enum classes 2.3.3. Using “when” with arbitrary objects 2.3.4. Using “when” without an argument 2.3.5. Smart casts: combining type checks and casts 2.3.6. Refactoring: replacing “if” with “when” 2.3.7. Blocks as branches of “if” and “when” 2.4. Iterating over things: “while” and “for” loops 2.4.1. The “while” loop 2.4.2. Iterating over numbers: ranges and progressions 2.4.3. Iterating over maps 2.4.4. Using “in” to check collection and range membership 2.5. Exceptions in Kotlin 2.5.1. “try”, “catch”, and “finally” 2.5.2. “try” as an expression 2.6. Summary Chapter 3. Defining and calling functions 3.1. Creating collections in Kotlin 3.2. Making functions easier to call 3.2.1. Named arguments 3.2.2. Default parameter values 3.2.3. Getting rid of static utility classes: top- level functions and properties 3.3. Adding methods to other people’s classes: extension functions and properties 3.3.1. Imports and extension functions 3.3.2. Calling extension functions from Java 3.3.3. Utility functions as extensions 3.3.4. No overriding for extension functions 3.3.5. Extension properties 3.4. Working with collections: varargs, infix calls, and library support 3.4.1. Extending the Java Collections API 3.4.2. Varargs: functions that accept an arbitrary number of arguments 3.4.3. Working with pairs: infix calls and destructuring declarations 3.5. Working with strings and regular expressions 3.5.1. Splitting strings 3.5.2. Regular expressions and triple-quoted strings