type-safety

Snippets

Kotlin Contracts: Teach Your Compiler About Your Function’s Behavior

What Are Kotlin Contracts? If you’ve worked with Kotlin for a while, you’ve likely encountered functions from the standard library that seem to have almost magical powers. Take require() and check()—call them with a condition, and the compiler somehow knows that if execution continues past that line, your variable is no longer nullable. Or use […]

, , , ,
The best of both worlds: The type safety of a wrapper class with the zero-overhead performance of a primitive.
Snippets

Type-Safe Zero-Cost Wrappers with Kotlin Value Classes

The Problem: Primitive Obsession You’ve probably seen this pattern in production code: database IDs represented as Long, user emails as String, currency amounts as Double. On the surface, it’s simple and pragmatic. But it introduces a subtle bug vector—primitive obsession. Imagine you have a function that accepts a user ID and a post ID, both

, , , ,
Scroll to Top