Walkthrough - Kotlin Tour: Intermediate Null Safety
In this lesson, we advanced our understanding of null safety in Kotlin, covering safe casts, collection filtering, and handling mandatory non-null values.
Changes Made
Implementation
- Created IntermediateNullSafety.kt which implements:
- Safe Cast (
as?): Demonstrating how to attempt a cast and receivenullinstead of a crash if it fails. - Not-Null Assertion (
!!): Showing the operator that forces a value to be non-null (and how it can crash). - Collection Filtering: Using
filterNotNull()andlistOfNotNull()to manage lists with potential nulls cleanly. - Early Returns: Using the Elvis operator
?:to validate preconditions and return early (e.g.,val x = input ?: return). - Exercises:
- Notification preferences filtering.
- Finding a single active subscription with
singleOrNull. - Stock validation with early returns.
- Safe Cast (
- Updated App.java to execute these demonstrations.
Verification
- Build: Successfully executed
./gradlew :app:assemble. - Logic: Verified that
as?handles type mismatches safely and thatlistOfNotNullexcludes missing map entries.
See the Step-by-Step Explanation for technical details.
Comments
Post a Comment