Quick Reference
| Topic | File |
|---|
| Null safety operators and patterns | INLINECODE0 |
| Coroutines, flows, structured concurrency |
coroutines.md |
| Collections, sequences, data classes |
collections.md |
| Scope functions, extensions, sealed classes |
idioms.md |
| Java interop and common Kotlin mistakes |
interop.md |
| Android lifecycle, Compose state |
android.md |
| Delegation, inline, reified, multiplatform |
advanced.md |
Critical Rules
Null Safety
- -
!! asserts non-null — crashes on null, use only when you've already checked - Platform types from Java are risky — add null checks or use
@Nullable/@NonNull annotations - Elvis with
return/throw for early exit — INLINECODE12
Coroutines
- -
viewModelScope auto-cancels on ViewModel clear — don't use GlobalScope in Android - Structured concurrency: child coroutine failure cancels parent — use
supervisorScope to isolate - INLINECODE16 needs initial value and never completes —
SharedFlow for one-shot events - Inject dispatchers for testability — don't hardcode INLINECODE18
Collections & Data Classes
- -
first() throws on empty — use firstOrNull() for safe access - Only constructor properties in
equals/hashCode — body properties ignored - INLINECODE23 for Compose — wrapping
mutableListOf in state won't track changes
Scope Functions & Extensions
- - Don't nest scope functions — readability drops fast, extract to named functions
- Extensions are resolved statically — not polymorphic, receiver type matters at compile time
Android/Compose
- -
repeatOnLifecycle(STARTED) for flow collection — launchWhenStarted is deprecated - INLINECODE27 survives recomposition only — use
rememberSaveable for config changes - INLINECODE29 is the gold standard — lifecycle-aware + Compose state
Java Interop
- -
== is structural equality in Kotlin — === for reference, opposite of Java - SAM conversion only for Java interfaces — Kotlin interfaces need explicit INLINECODE32
- INLINECODE33 ,
@JvmOverloads, @JvmField for Java-friendly APIs
快速参考
| 主题 | 文件 |
|---|
| 空安全操作符与模式 | nullsafety.md |
| 协程、Flow、结构化并发 |
coroutines.md |
| 集合、序列、数据类 | collections.md |
| 作用域函数、扩展、密封类 | idioms.md |
| Java互操作与常见Kotlin错误 | interop.md |
| Android生命周期、Compose状态 | android.md |
| 委托、内联、具化、多平台 | advanced.md |
关键规则
空安全
- - !! 断言非空——遇空则崩溃,仅在你已检查过后使用
- Java的平台类型存在风险——添加空检查或使用 @Nullable/@NonNull 注解
- 使用 return/throw 的Elvis操作符进行提前退出——val name = user?.name ?: return
协程
- - viewModelScope 在ViewModel清除时自动取消——Android中不要使用 GlobalScope
- 结构化并发:子协程失败会取消父协程——使用 supervisorScope 进行隔离
- StateFlow 需要初始值且永不完成——一次性事件使用 SharedFlow
- 注入调度器以便测试——不要硬编码 Dispatchers.IO
集合与数据类
- - first() 在空集合时抛出异常——使用 firstOrNull() 进行安全访问
- 只有构造函数属性参与 equals/hashCode——类体中的属性被忽略
- Compose中使用 mutableStateListOf——将 mutableListOf 包装在state中不会跟踪变化
作用域函数与扩展
- - 不要嵌套作用域函数——可读性急剧下降,提取为命名函数
- 扩展函数是静态解析的——不具有多态性,接收者类型在编译时确定
Android/Compose
- - 使用 repeatOnLifecycle(STARTED) 收集Flow——launchWhenStarted 已弃用
- remember 仅在重组时存活——配置变更使用 rememberSaveable
- collectAsStateWithLifecycle 是黄金标准——生命周期感知 + Compose状态
Java互操作
- - Kotlin中 == 是结构相等——=== 是引用相等,与Java相反
- SAM转换仅适用于Java接口——Kotlin接口需要显式声明 fun interface
- 使用 @JvmStatic、@JvmOverloads、@JvmField 创建Java友好的API