Читать книгу Programming Kotlin Applications - Бретт Мак-Лахлин, Brett McLaughlin - Страница 88

TYPE SAFETY CHANGES EVERYTHING

Оглавление

The big takeaway here is that when a language seeks to really enforce type safety—as Kotlin does—then nearly everything in the language gets affected. Properties are required to have values, and have them early, so that Kotlin can ensure the correct types are used. The mutability of variables is tightly controlled—in Kotlin's case, through var and val being different—so that Kotlin can determine when it needs to keep up with a variable's type over time, and the values assigned to that type. And Kotlin doesn't convert types for you. All of these nuances to Kotlin are keeping type safety intact and making it much harder to break Kotlin at runtime.

Believe it or not, there is a lot more to Kotlin's type safety: things you'll learn in future chapters. Kotlin has a lot of specific rules around null values, checking for null, and the idea of null safety: keeping your code free from null pointer exceptions.

NOTE If you're not familiar with null pointers, or have never seen a NullPointerException in, for example, Java, then count yourself lucky. If you have, though, they're often the source of very difficult bugs. Kotlin attempts to add some extra work at the coding layer in favor of getting rid of those annoying and hard-to-find bugs. It's a trade-off, but one that works quite well for Kotlin.

Programming Kotlin Applications

Подняться наверх