152
pages
English
Ebooks
2020
Vous pourrez modifier la taille du texte de cet ouvrage
Obtenez un accès à la bibliothèque pour le consulter en ligne En savoir plus
Découvre YouScribe en t'inscrivant gratuitement
Découvre YouScribe en t'inscrivant gratuitement
152
pages
English
Ebook
2020
Vous pourrez modifier la taille du texte de cet ouvrage
Obtenez un accès à la bibliothèque pour le consulter en ligne En savoir plus
Publié par
Date de parution
07 mars 2020
Nombre de lectures
0
EAN13
9789389423235
Langue
English
Publié par
Date de parution
07 mars 2020
Nombre de lectures
0
EAN13
9789389423235
Langue
English
Kotlin In-Depth [Vol. II]
A Comprehensive Guide to Modern Multi-Paradigm Language
by
Aleksei Sedunov
FIRST EDITION 2020
Copyright © BPB Publications, India
ISBN: 978-93-89423-228
All Rights Reserved. No part of this publication may be reproduced or distributed in any form or by any means or stored in a database or retrieval system, without the prior written permission of the publisher with the exception to the program listings which may be entered, stored and executed in a computer system, but they can not be reproduced by the means of publication.
LIMITS OF LIABILITY AND DISCLAIMER OF WARRANTY
The information contained in this book is true to correct and the best of author’s & publisher’s knowledge. The author has made every effort to ensure the accuracy of these publications, but cannot be held responsible for any loss or damage arising from any information in this book.
All trademarks referred to in the book are acknowledged as properties of their respective owners.
Distributors:
BPB PUBLICATIONS
20, Ansari Road, Darya Ganj
New Delhi-110002
Ph: 23254990/23254991
MICRO MEDIA
Shop No. 5, Mahendra Chambers,
150 DN Rd. Next to Capital Cinema,
V.T. (C.S.T.) Station, MUMBAI-400 001
Ph: 22078296/22078297
DECCAN AGENCIES
4-3-329, Bank Street,
Hyderabad-500195
Ph: 24756967/24756400
BPB BOOK CENTRE
376 Old Lajpat Rai Market,
Delhi-110006
Ph: 23861747
Published by Manish Jain for BPB Publications, 20 Ansari Road, Darya Ganj, New Delhi-110002 and Printed by him at Repro India Ltd, Mumbai
Dedicated to
Tatiana, my guiding light and the incessant source of inspiration
About the Author
Aleksei Sedunov has been working as a Java developer since 2008. After joining JetBrains in 2012 he’s been actively participating in the Kotlin language development focusing on IDE tooling for the IntelliJ platform. Currently he’s working in a DataGrip team, a JetBrains Database IDE, carrying on with using Kotlin as a main development tool.
Acknowledgement
Above all others I would like to give my gratitude to the entire Kotlin team at JetBrains which has created such a beautiful language and continues to relentlessly work on its improvement – especially Andrey Breslav who’s been leading the language design from the very first day.
I’m really grateful to everyone at BPB publications for giving me this splendid opportunity for writing this book and lending a tremendous support in improving the text before it gets to the readers.
Last but not least I’d like to thank my beloved family for their support throughout the work on the book.
– Aleksei Sedunov
Preface
Since its first release in 2016 (and even long before that) Kotlin has been gaining in popularity as a powerful and flexible tool in a multitude of development tasks being equally well-equipped for dealing with mobile, desktop and server-side applications finally getting its official acknowledgment from Google in 2017 and 2018 as a primary language for Android development. This popularity is well-justified since language pragmatism, the tendency to choose the best practice among known solutions was one of the guiding principles of its design.
With the book you’re holding in your hands I’d like to invite you to the beautiful world of Kotlin programming where you can see its benefits for yourself. After completing this book you’ll have all the necessary knowledge to write in Kotlin on your own.
The second volume introduces you to the more advanced Kotlin features such as reflection, domain-specific languages and coroutines, discusses Java/Kotlin interoperability issues and explains how Kotlin can be used in various development areas, including testing, Android applications and Web. It’s divided into the following 8 chapters:
Chapter 10 addresses the use of annotations which allow you to accompany Kotlin code with various metadata and explains the basics of Reflection API which provides access to runtime representation of Kotlin declarations.
Chapter 11 describes some advanced features which help developer in composing flexible APIs in a form of the domain-specific languages: operator overloading, delegated properties and builder-style DSLs based on the higher-order functions.
Chapter 12 discusses common issues of combining Java and Kotlin code within the same codebase and explain the specifics of using Java declarations in Kotlin code and vice versa.
Chapter 13 introduces the reader to the Kotlin coroutines library which inroduces a set of building blocks for programming asynchronous computations. Additionally it describes some utilities simplifying the use of Java concurrency API in Kotlin code.
Chapter 14 is devoted to the KotlinTest, a popular testing framework aimed specifically at the Kotlin developers. It describes various specification styles, explains the use of assertion API and addresses more advanced issues like using fixtures and test configurations.
Chapter 15 serves as an introduction to using Kotlin for development on the Android platform. It guides the reader through setting up an Android Studio project and explains basic aspects of Android development on an example of a simple calculator application.
Chapter 16 explains the basic features of the Ktor framework aimed at development of connected applications which make heavy use of Kotlin featutes and asynchronous computations.
Chapter 17 describes how to build a microservice application using Spring Boot and Ktor frameworks.
Downloading the code bundle and coloured images:
Please follow the link to download the Code Bundle and the Coloured Images of the book:
https://rebrand.ly/rrq2rot
Errata
We take immense pride in our work at BPB Publications and follow best practices to ensure the accuracy of our content to provide with an indulging reading experience to our subscribers. Our readers are our mirrors, and we use their inputs to reflect and improve upon human errors if any, occurred during the publishing processes involved. To let us maintain the quality and help us reach out to any readers who might be having difficulties due to any unforeseen errors, please write to us at :
errata@bpbonline.com
Your support, suggestions and feedbacks are highly appreciated by the BPB Publications’ Family.
Table of Contents
10. Annotations and Reflection
Structure
Objective
Annotations
Defining and using annotation classes
Built-in annotations
Reflection
Reflection API overview
Classifiers and types
Callables
Conclusion
Questions
11. Domain-Specific Languages
Structure
Objective
Operator overloading
Unary operations
Increments and decrements
Binary operations
Infix operations
Assignments
Invocations and indexing
Destructuring
Iteration
Delegated properties
Standard delegates
Creating custom delegates
Delegate representation
Higher-order functions and DSLs
Fluent DSL with infix functions
Using type-safe builders
@DslMarker
Conclusion
Questions
12. Java Interoperability
Structure
Objectives
Using the Java code from Kotlin
Java methods and fields
Unit vs void
Operator conventions
Synthetic properties
Platform types
Nullability annotations
Java/Kotlin type mapping
Single abstract method interfaces
Using Java to Kotlin converter
Using the Kotlin code from Java
Accessing properties
File facades and top-level declarations
Objects and static members
Changing the exposed declaration name
Generating overloads
Declaring exceptions
Inline functions
Type aliases
Conclusion
Questions
13. Concurrency
Structure
Objective
Coroutines
Coroutines and suspending functions
Coroutine builders
Coroutine scopes and structured concurrency
Coroutine context
Coroutine control flow
Job lifecycle
Cancellation
Timeouts
Coroutine dispatching
Exception handling
Concurrent communication
Channels
Producers
Tickers
Actors
Using Java concurrency
Starting a thread
Synchronization and locks
Conclusion
Questions
14. Testing with Kotlin
Structure
Objective
KotlinTest specifications
Getting started with KotlinTest
Specification styles
Assertions
Matchers
Inspectors
Handling exceptions
Testing non-deterministic code
Property-based testing
Fixtures and configurations
Providing a fixture
Test configuration
Conclusion
Questions
15. Android Applications
Structure
Objective
Getting started with Android
Setting up an Android Studio project
Gradle build scripts
Activity
Using an Emulator
Activities
Designing the application UI
Implementing the activity class
Kotlin Android Extensions
Preserving the activity state
Anko Layouts
Conclusion
Questions
16. Web Development with Ktor
Structure
Objective
Introducing Ktor
Server features
Routing
Handling HTTP requests
HTML DSL
Sessions support
Client features
Requests and responses
Cookies
Conclusion
Questions
17. Building Microservices
Structure
Objectives
The microservice architecture
Introducing Spring Boot
Setting up a project
Deciding on the Services API
Implementing a random generator service
Implementing a password generator service
Microservices with Ktor
Using the JSON serialization feature
Implementing a password generator service
Conclusion
Questions
C HAPTER 10
Annotations and Reflection
I n this chapter, we will be covering two major topics. The first part will be devoted to annotations, which allow you to bind metadata to Kotlin declarations and access them later, at runtime. We will explain how to define and apply your own annotations and look at some built-in annotations, which affect the compilation of the Kotlin source code.
The second part will introduce us to the Reflection API, which defines a set of types comprising runtime representation of Kotlin declarations. We’ll discuss how to obtain reflection objects, access their attributes and use callables, to dynamicall