3 ms·
In Kotlin null is always safe. The problem is that types imported from Java are "platform types", i.e. Kotlin doesn't know whether they're nullable or non-null
by nostrademons 10y ago
In Kotlin null is always safe. The problem is that types imported from Java are "platform types", i.e. Kotlin doesn't know whether they're nullable or non-nullable, and it's up to the programmer to check their API docs and insert the appropriate handling at the call site.
For example, in my own pure-Kotlin code, "val id: String" is a non-nullable type "String"; the compiler will guarantee that it is never null. "val url: URL?" is a nullable type "URL?"; the compiler checks for null, and I need to use ? or !! or a smart-cast to access it. HttpServletRequest.pathInfo is a platform type "String!", is marked as such in autocomplete, and could be either null or non-null based on the API; it's up to me to use it appropriately.
The difference between platform types and nullable types is that platform types can be silently coerced to non-null types, even though they may be null. This was an ergonomic decision; you use so many Java APIs in Kotlin that the language would be nearly unusable afterwards (kinda like how Scala, rather than being a "better Java", has developed its own ecosystem of idiomatic Scala code). It's a potential safety hole, but it's clearly documented; think of it like getting memory management right in a C FFI.
- lmm 10y ago> In Kotlin null is always safe. The problem is that types imported from Java... Well yeah. But Kotlin advocates say you're supposed to use those extensively. > think of it like getting memory management right in a C FFI. Yeah, that's pretty terrifying. I would never want to use a language that expected me to make extensive use of a C FFI. It was bad enough when I used Python, and that involved a lot less C than Kotlin does Java.
- nostrademons 10y agoYou would normally check null on language boundaries: val path: String = req.pathInfo ?: "" Now path is a non-nullable String, and you can be sure that it will never contain null, and you have specified exactly what the behavior is if the Java API returned it. > Yeah, that's pretty terrifying. I would never want to use a language that expected me to make extensive use of a C FFI. It was bad enough when I used Python, and that involved a lot less C than Kotlin does Java. You do this every day, regardless of which language you use. Your POSIX APIs and OS services are all in C. That they generally work is because numerous people have invested the time into putting appropriate error checks & memory safety into the host language bindings. It's the same story with Kotlin: when you write code that uses a language (Java) that doesn't have appropriate safety mechanisms, you read the docs & code carefully, and then you use the safety mechanisms that your language (Kotlin) gives you for all the code you write. There is really nothing Kotlin can do about this: the original code is written in a language that doesn't make the distinction between nullable and non-nullable, so there is no information in the type system about whether it should map to a nullable or non-nullable type.
- lmm 10y ago> You do this every day, regardless of which language you use. Your POSIX APIs and OS services are all in C. That they generally work is because numerous people have invested the time into putting appropriate error checks & memory safety into the host language bindings. Not when I'm working on ocaml unikernels, but sure. The point is I only use languages where those bindings already exist and are maintained; I wouldn't want to use a language that expected me to use a C FFI directly in day-to-day work. Whereas the Kotlin narrative is that you do use the Java "FFI" directly in day-to-day work.