Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Title Page
Copyright and Credits
Mastering Kotlin
Dedication
About Packt
Why subscribe?
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Code in Action
Conventions used
Get in touch
Reviews
Section 1: Kotlin – A Modern Solution to Application Development
A New Challenger Approaches
Technical requirements
Creating a modern language for the JVM
What is Kotlin?
Kotlin is flexible
Kotlin is expressive and concise
Kotlin is powerful
Hello Kotlin
Who created Kotlin?
Announcing Kotlin
Motivations for Kotlin
Community involvement
Moving beyond the JVM
Kotlin for Android
Kotlin for the web
Server-side Kotlin
Kotlin to JavaScript
Native and multiplatform Kotlin
Designing Kotlin with best practices in mind
Learning from Java
Best practice by design
Checking in on the current state of Kotlin
Developing Kotlin in the open
Increasing popularity for Kotlin
Learning Kotlin
Summary
Questions
Further reading
Programmers' Multi-Tool – Flexible, Expressive, and Concise
Technical requirements
Picking your programming paradigm
Object-Oriented Programming
Functional programming
Reactive programming
Embracing first-class functions
Function types
Top-level functions
Extension functions
Higher-order functions
Standard library
Fixing the billion-dollar mistake
Defining null and non-null types
Working with null
Null-safe calls
Non-null assertion
Conditional checks
The Elvis operator
Integrating with Java
Using Java from Kotlin
Creating and working with Java classes from Kotlin
Calling static Java methods from Kotlin
Using Kotlin from Java
Creating and working with Kotlin classes from Java
Calling Kotlin functions from Java
Expanding on the interop story
Summary
Questions
Further reading
Section 2: Putting the Pieces Together – Modeling Data, Managing State, and Application Architecture
Understanding Programming Paradigms in Kotlin
Technical requirements
Classifying programming languages
Multi-paradigm languages
Imperative programming languages
Objected-oriented programming languages
Declarative programming languages
Domain Specific Languages
Other programming paradigms
Event-driven programming
Generic programming
Understanding object-oriented programming
Key concepts
Encapsulation
Inheritance
Polymorphism
Object-oriented programming with Kotlin
Functional programming
Key concepts
First-class functions
Pure functions
Functional programming with Kotlin
More to learn
Reactive programming
RxJava
Key concepts in RxJava
Observables
Operators
Threading
RxKotlin
Reactive coroutines
Channels and flow
Summary
Questions
Further reading
First-Class Functions
Technical requirements
Hello functions – first-class support for functions
Writing a basic function
First-class functions
Functions as variables
Read-only function variables
Mutable function variables
Function properties
Functions as arguments
Functions as return values
Top-level functions
Flexibility by design – reducing boilerplate with effective parameters
Adding function parameters
Multiple function parameters
The vararg modifier
Limitations of vararg
Named parameters
Default parameter values
A function for every job – understanding function variations
Top-level functions
Class functions
Local functions
Single expression functions
Infix functions
Extension functions
Summary
Questions
Further reading
Modeling Real-World Data
Technical requirements
Object-oriented programming in Kotlin
Defining a class
Adding properties
Property basics
Working with properties
Custom accessors
Primary constructor properties
Initialization
Primary constructor initialization
Property assignments and initializer blocks
Adding methods
Customizing construction
Primary constructors
Secondary constructors
Private constructors
Controlling visibility
The public modifier
The internal modifier
The protected modifier
The private modifier
Organizing your code
Files and classes
Packages
Modules
Achieving inheritance and composition
Interfaces
Defining a simple interface
Defining an interface property
Interface inheritance
Implementing multiple interfaces
Default methods
Subclasses and abstract classes
Nested classes
Delegation
Implementation delegation
Type checking
Any
Type casting
Smart casting
Understanding data classes
Creating a data class
Generated code
equals()
hashCode()
toString()
copy()
Destructuring
Working with enums, sealed classes, and objects
Enums
Sealed classes
Object classes
Singletons
Companion objects
Object expressions
Summary
Questions
Further reading
Section 3: Play Nice – Integrating Kotlin With Existing Code
Interoperability as a Design Goal
Technical requirements
Understanding why Kotlin was designed with interop in mind
Why is interop so important for Kotlin?
How is interop achieved?
Adding Kotlin to an existing Java project
Adding Kotlin to an existing IntelliJ project
Converting Java into Kotlin
Converting Java files into Kotlin files
Converting pasted Java code
Summary
Questions
Crossing Over – Working across Java and Kotlin
Technical requirements
Working with Kotlin from Java
Basic interop
Creating class instances across languages
Inheritance across languages
Property access
Handling null
Function arguments
Lack of static
Working with Java from Kotlin
Using existing code
Enforcing null safety
Embracing Kotlin idioms
Goodbye helpers – embracing top-level and extension functions and properties
Top-level functions
Extension functions
Top-level properties
Top-level constant properties
Top-level mutable properties
Companion objects
Are two languages better than one?
Project structure
Increased build size and speed
Annotation processing
Performance
Complexity
Summary
Questions
Further reading
Controlling the Story
Technical requirements
Improving generated class names
How are class names generated?
Renaming the generated class
Renaming a generated class name for top-level functions
Renaming a generated class name for top-level properties
Leveraging default values in Java
Better companions
How do companion objects work?
When should we use a companion object?
Modifying companion object names
Bringing static to Kotlin
Where can we use JvmStatic?
@JvmStatic properties
@JvmStatic methods
When should we use the @JvmStatic annotation?
Use-site annotation targets
What are use-site targets?
Specifying a use-site target
Default targets
Summary
Questions
Further reading
Baby Steps – Integration through Testing
Technical requirements
Test-first integration
Feature integration
Advantages
Disadvantages
Test integration
Advantages
Disadvantages
General impact of integration
Testing interop
Exercising your Kotlin code
Testing closed classes
Using Mockito
Using the compiler plugin
Exercising Java code from Kotlin
Exercising Kotlin code from Java
Improved testing with Kotlin
The kotlin.test library
Annotations
Helper functions
Mockito-Kotlin
Summary
Questions
Further reading
Section 4: Go Beyond – Exploring Advanced and Experimental Language Features
Practical Concurrency
Technical requirements
Understanding async patterns
Threading primitives
Thread
ExecutorService
Advanced threading
CompletableFuture
RxJava
Coroutines
The foundations of coroutines
What are coroutines?
Coroutines with Kotlin
Coroutines in practice
Coroutine primitives
Coroutine scopes
Coroutine builders
Suspending functions
Working with blocking code
Fetching data
Summary
Questions
Further reading
Building Your Own Tools – Domain-Specific Languages (DSLs)
Technical requirements
What is a DSL?
Domain-specific languages
Where are DSLs used?
HTML
Testing
Dependency injection
The building blocks of a DSL in Kotlin
Top-level and extension functions
Top-level functions
Extension functions
Function types with receivers
Scope control
Creating your first Kotlin DSL
What problem are you trying to solve?
Creating your starting point
Adding elements
Adding sodas
Adding pizzas
Making it easy to use
Summary
Questions
Further reading
Fully Functional – Embracing Functional Programming
Technical requirements
Understanding functional programming
Pure functions
Immutability
Limited side effects
Reduced complexity
Understanding advanced functions
Working with functional types
Functional variables
Functional arguments
Improving the performance of higher-order functions
The inline modifier
The noinline modifier
Leveraging the standard library
Manipulating collections
Filtering
Mapping
Associating
Searching
Exploring other useful functions
Functional programming with Arrow
What is Arrow?
Typeclasses
Data types
Effects
Summary
Questions
Further reading
Section 5: The Wide World of Kotlin – Using Kotlin across the Entire Development Stack
Kotlin on Android
Technical requirements
First class Kotlin for Android
Adopting Kotlin for Android
Kotlin first
The future of Android
Hello Android Kotlin
Creating an Android app with Kotlin support
Taking advantage of Kotlin on Android
Configuring a view reference
Responding to click events
Creating factory methods for activities and fragments
Handling savedInstanceState
Building with Kotlin
The Gradle Kotlin DSL
Migrating to the Kotlin buildscript
Simplifying dependency management with Kotlin
First-party tooling
Exploring Android KTX
Adding Core KTX to your project
Using Core KTX
Using Fragment KTX
Using Kotlin Android Extensions
Binding views with Kotlin Android Extensions
Generating Parcelable implementations
Summary
Questions
Further reading
Kotlin and Web Development
Technical requirements
Kotlin for the web
Compiling Kotlin to JavaScript
Transpiling to JavaScript
Targeting JavaScript
Using the compiled JavaScript
Targeting JavaScript with Kotlin
Building a Hello Kotlin project
Creating a Kotlin project with a JavaScript target
Writing Hello World for Kotlin JavaScript
Examining the compiled JavaScript
Consuming Kotlin through compiled JavaScript
Integrating with existing JavaScript
Working with other JavaScript frameworks
Manipulating the DOM via Kotlin
Summary
Questions
Further reading
Introducing Multiplatform Kotlin
Technical requirements
Introducing Kotlin multiplatform
Understanding how Kotlin approaches multiplatform
Understanding the value of Kotlin multiplatform
Targeting multiple platforms
Differentiating Kotlin multiplatform
Sharing logic, not UI
Limiting risk
Creating your first Kotlin multiplatform project
Creating a shared module
Adding an Android app
Adding an iOS app
Adding a web page
Building a Kotlin multiplatform project
Writing multiplatform Kotlin
Leveraging multiplatform libraries
Understanding the limitations of Kotlin multiplatform
Understanding operational requirements
Understanding Kotlin multiplatform ecosystem limitations
Understanding framework and tooling limitations
Summary
Questions
Further reading
Taming the Monolith with Microservices
Technical requirements
Understanding microservices
Exploring the limits of the monolith
Embracing microservices
Writing microservices with Kotlin
Understanding Kotlin's relation to microservices
Choosing Kotlin for your microservices
Deploying your first Kotlin microservice
Creating a Ktor project
Writing a simple microservice
Adding a route
Deploying our microservice
Deploying Kotlin services in production
Deploying local Ktor services
Scaling our routes
Defining routes with extension functions
Installing additional routes
Summary
Questions
Further reading
Practical Design Patterns
Technical requirements
Understanding design patterns
Revisiting the Singleton pattern in Kotlin
Understanding the Singleton pattern
Implementing the Singleton pattern in Kotlin
Revisiting the Factory pattern in Kotlin
Understanding the Factory pattern
Implementing the Factory pattern in Kotlin
Revisiting the Builder pattern in Kotlin
Understanding the Builder pattern
Implementing the Builder pattern in Kotlin
Revisiting the Strategy pattern in Kotlin
Understanding the Strategy pattern
Implementing the Strategy pattern in Kotlin
Summary
Questions
Further reading
Assessments
Chapter 1: A New Challenger Approaches
Chapter 2: Programmer's Multi-Tool
Chapter 3: Understanding Programming Paradigms in Kotlin
Chapter 4: First-Class Functions
Chapter 5: Modeling Real-World Data
Chapter 6: Interoperability by Design
Chapter 7: Crossing Over – Working across Java and Kotlin
Chapter 8: Controlling the Story
Chapter 9: Baby Steps – Integration through Testing
Chapter 10: Practical Concurrency
Chapter 11: Building Your Own Tools – Domain-Specific Languages (DSLs)
Chapter 12: Fully Functional – Embracing Functional Programming
Chapter 13: Kotlin on Android
Chapter 14: Kotlin and Web Development
Chapter 15: Introducing Multiplatform Kotlin
Chapter 16: Taming the Monolith with Microservices
Chapter 17: Practical Design Patterns
Other Books You May Enjoy
Leave a review - let other readers know what you think
← Prev
Back
Next →
← Prev
Back
Next →