Advanced Kotlin:

You know how to use Kotlin, but do you know the advanced features? What are the consequences of using variance modifiers? What limitations do they introduce, and how can these be overcome? How does property delegation work? How to use reflection? How to implement a KSP or an annotation processor? How is a Kotlin Compiler Plugin implemented? If you want to know the answers to these questions, this book is for you.
This book focuses on the advanced aspects of Kotlin's development. These are often features and tools that developers use daily, even though they don’t know exactly how they work. This book explains them and gives you the knowledge to use these features in your projects.
The purpose

Chapter by Chapter
Clear path to progress
1.Introduction
- Who is this book for?
- The structure of this book
- That will be covered?
- The Kotlin for Developers series
- Conventions
- Code conventions
- Exercises and solutions
- Acknowledgments
2.Generic variance modifiers
- List variance
- Consumer variance
- Function types
- Exercise: Usage of generic types
- The Covariant Nothing Object
- The Covariant Nothing Class
- Variance modifier limitations
- UnsafeVariance annotation
- Variance modifier positions
- Star projection
- Summary
- Exercise: Generic Response
- Exercise: Generic Consumer
3.Interface delegation
- The delegation pattern
- Delegation and inheritance
- Kotlin interface delegation support
- Wrapper classes
- The decorator pattern
- Intersection types
- Limitations
- Conflicting elements from parents
- Summary
- Exercise: ApplicationScope
4.Property delegation
- How property delegation works
- Other getValue and setValue parameters
- Implementing a custom property delegate
- Provide a delegate
- Property delegates in Kotlin stdlib
- The notNull delegate
- Exercise: Lateinit delegate
- The lazy delegate
- Exercise: Blog Post Properties
- The observable delegate
- The vetoable delegate
- A map as a delegate
- Review of how variables work
- Summary
- Exercise: Mutable lazy delegate
5.Kotlin Contracts
- The meaning of a contract
- How many times do we invoke a function from an argument?
- Implications of the fact that a function has returned a value
- Using contracts in practice
- Summary
- Exercise: Coroutine time measurement
6.Java interoperability
- Nullable types
- Kotlin type mapping
- JVM primitives
- Collection types
- Annotation targets
- Static elements
- JvmField
- Using Java accessors in Kotlin
- JvmName
- JvmMultifileClass
- JvmOverloads
- Unit
- Function types and function interfaces
- Tricky names
- Throws
- JvmRecord
- Summary
- Exercise: Adjust Kotlin for Java usage
7.Using Kotlin Multiplatform
- Multiplatform module configuration
- Expect and actual elements
- Possibilities
- Multiplatform libraries
- A multiplatform mobile application
- Summary
- Exercise: Multiplatform LocalDateTime
8.JavaScript interoperability
- Setting up a project
- Using libraries available for Kotlin/JS
- Using Kotlin/JS
- Building and linking a package
- Distributing a package to npm
- Exposing objects
- Exposing Flow and StateFlow
- Adding npm dependencies
- Frameworks and libraries for Kotlin/JS
- JavaScript and Kotlin/JS limitations
- Summary
- Exercise: Migrating a Kotlin/JVM project to KMP
9.Reflection
- Hierarchy of classes
- Function references
- Parameter references
- Property references
- Class reference
- Serialization example
- Referencing types
- Type reflection example: Random value
- Kotlin and Java reflection
- Breaking encapsulation
- Summary
- Exercise: Function caller
- Exercise: Object serialization to JSON
- Exercise: Object serialization to XML
- Exercise: DSL-based dependency injection library
10.Annotation processing
- Your first annotation processor
- Hiding generated classes
- Summary
- Exercise: Annotation Processing execution measurement wrapper
11.Kotlin Symbol Processing
- Your first KSP processor
- Testing KSP
- Dependencies and incremental processing
- Multiple rounds processing
- Using KSP on multiplatform projects
- Summary
- Exercise: KSP execution measurement wrapper
12.Kotlin Compiler Plugins
- Compiler frontend and backend
- Compiler extensions
- Popular compiler plugins
- Making all classes open
- Changing a type
- Generate function wrappers
- Example plugin implementations
- Summary
13.Static Code Analysers
- What are Static Analysers?
- Types of analysers
- Kotlin Code Analysers
- Setting up detekt
- Writing your first detekt Rule
- Conclusion
14.Ending
15.Exercise solutions
16.Notes