Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Marcin Moskała

Rating No ratings yet

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. About the book 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. Who this book is for This book is for developers with Kotlin experience who want to learn about its advanced features. Readers are expected to have experience in Kotlin development. All the required knowledge can be discovered in the books Kotlin Essentials and Functional Kotlin. What is covered This book focuses on advanced Kotlin features, including: Generic variance modifiers Interface and property delegation Kotlin Contracts Kotlin and Java interoperability Multiplatform development Using Kotlin/JS Kotlin reflection Annotation Processor Kotlin Symbol Processor Kotlin Compiler Plugins Static Analysis with custom rules

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical deep-dive into the Kotlin features you already use but may not fully understand—variance, delegation, contracts, Java interop, multiplatform, reflection, and the processor/compiler-plugin toolchain. Best for experienced Kotlin developers who want to reason about consequences and build tooling, not just write app code. 【Book Arc】 - **Opening (~0%–10%)**: Sets scope and prerequisites, then moves into generic variance—why `in`/`out` exist, what they forbid, and how `UnsafeVariance` and star projections work around those limits. - **Early (~10%–32%)**: Delegation as a design tool: interface and property delegation, how delegates compile to accessor code, and the standard `lazy`, `Lateinit`, and `vetoable` delegates with their trade-offs. - **Early–Middle (~32%–42%)**: Kotlin Contracts as compiler hints (`returns`, `implies`, `callsInPlace`), followed by Kotlin–Java interop: nullability annotations, annotation targets, `@JvmName`/`@JvmOverloads`, and `fun interface`. - **Middle (~42%–48%)**: Multiplatform structure—shared modules, source sets, expect/actual, and using shared code from Java, JS, and native targets. - **Late (~48%–65%)**: Reflection and metaprogramming: breaking encapsulation, then building annotation processors and KSP processors, including testing, dependencies, incremental and multi-round processing. - **Ending (~65%–100%)**: Compiler plugins (frontend/backend, extensions, examples like making classes open or generating wrappers) and static analysis with custom detekt rules, closing with exercises and solutions. 【Key Takeaways】 - **Variance modifiers carry real consequences** (Opening): `in`/`out` are not decoration—they restrict where type parameters may appear, and the book shows the workarounds (`UnsafeVariance`, star projections, `Nothing`) and when they are legitimate. - **Delegation beats inheritance for composition** (Early): you can delegate to many objects but inherit from one; inheritance is a strong, encapsulation-breaking relationship, so delegation is often the safer default. - **Property delegates are just generated accessors** (Early): understanding the compiled `getValue`/`setValue` shape explains receiver types, `KProperty` access, and why `ReadWriteProperty<Any?, T>` is the usual contract. - **Standard delegates have hidden knobs** (Early): `lazy` thread-safety modes trade speed for safety; `vetoable` validates state changes; `Lateinit` shows how to build your own delegate. - **Contracts make the compiler smarter** (Early): `returns(false) implies (...)` and `callsInPlace` enable smart casts and non-local returns that would otherwise be impossible—worth knowing when writing inline APIs. - **Interop friction is structural, not accidental** (Middle): nullability annotations, platform types, annotation targets, and default arguments all need explicit handling; `fun interface` fixes Java call-site ergonomics. - **Multiplatform is about source-set layout** (Middle): shared modules and correctly named source sets let one library serve JVM, JS, and native consumers. - **Tooling is a ladder** (Late–Ending): reflection → annotation processing → KSP → compiler plugins → static analysis; each step trades simplicity for power and build complexity. 【Reading Tips】 - Deep-read the variance and delegation chapters; they underpin later material and repay re-reading with the exercises. - Skim the interop chapter if you work purely in Kotlin, but keep the annotation-target and `@JvmOverloads` sections as reference. - Treat the processor/KSP/compiler-plugin chapters as hands-on: build the smallest working example before reading the advanced sections on rounds and incremental processing. - Do the end-of-chapter exercises—solutions are included—especially the serialization and DI-library ones, which consolidate reflection and DSL skills. - Use the detekt chapter as a practical entry point to static analysis even if you never write a compiler plugin. 【Coverage Limits】 The excerpts cover the table of contents and sampled sections on variance, delegation, contracts, interop, multiplatform, reflection, processors, compiler plugins, and detekt; Kotlin/JS specifics and several exercise details are only partially represented, so this guide does not assess their depth.
Page 7
rounds processing 302 Using KSP onmultiplatformprojects 305 Summary 306 Exercise: Kotlin Symbol Processing 306 Kotlin Compiler Plugins 308 Compiler frontend...
View in text
Excerpt 2
on(user: User): Boolean fun showOnPage(page: Page): Boolean fun showOnArticle(article: Article): Boolean } class ShowOnPerson( val authorKey: String, val pre...
View in text
Excerpt 3
lic inline fun measureTimeMillis(block: () -> Unit): Long { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } val start = System.currentTimeMilli...
View in text
Excerpt 4
efinitelyworth all the amazing features that Kotlin offers. Exercise: Adjust Kotlin for Java usage Consider the following Kotlin elements: ²¹Java’s first sta...
View in text
Excerpt 5
efix.This indicates that this type ispartofKotlinReflection and differentiates these classes from Java Reflection. The type Class is part of Java Reflection,...
View in text
Excerpt 6
s are similar.We can transformbetween these two hierarchies using extension properties accessible on Kotlin/JVM. For ex- ample, we can use the java property...
View in text
Excerpt 7
Kotlin Compiler can use plugins that change its behavior.We define a Kotlin Compiler plugin by extending a special class, called an extension, and then regis...
View in text
Excerpt 8
ated logger and want to ensure all logging goes through it. Your inspector can ask to visit all the nodes in your AST that are of type “function call”. On th...
View in text
Tags
AI categories
Programming LanguageKotlinSoftware
Language: English
File Format: PDF
File Size: 12.2 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…