4 ms·
> What do you want from them? They have no ability whatsoever to control this. Either get annotations in the Java code if you can modify it (which is a good ide
by lmm 10y ago
> What do you want from them? They have no ability whatsoever to control this. Either get annotations in the Java code if you can modify it (which is a good idea anyways), or wrap it and provide nullability information.
What I want is consistency. In Scala optional is always safe and null is never safe, and that's true whether you're calling a Scala method or a Java method (even the most perverse Java libraries don't declare themselves as returning Optional<String> and then return null). In Kotlin null is safe some of the time and not safe other times, e.g. if you're passing it to a method it might be fine or might not.
- Nycto 10y agoI don't see a huge difference between Scala's Options and Kotlin's null safety. In Scala, you can still do this: val x: Option[Int] = null
- lmm 10y agoIn theory you can. But you'd know to immediately fail that line in code review (or better still, enforce it at build time via wartremover). Whereas in Kotlin someMethod(null) might be perfectly legitimate or might not, but you can't tell without knowing the details of someMethod (whether it's a Java method or not).
- afastow 10y agoMaybe I'm missing something but how is that different from Scala or Java? That will fail at compile time in Kotlin if the method is written in Kotlin and has a non-nullable parameter. Even if it's Java I think it will fail at compile time if the Java parameter is annotated with @NotNull. If it is a Java method and not annotated with @NotNull then of course it will be allowed at compile time because how could it possibly not be allowed? What are you saying is preferable in that case?
- lmm 10y agoThe trouble is in idiomatic Kotlin you have lines like that, because when you want to represent absence you use nullable parameters. So you can't assume a line like that is wrong. But you can't assume it's correct just because it compiled either, because as you say it might be an unannotated Java method. In idiomatic Scala you would never have a line like that (because you represent absence with optionals rather than null). So you can insta-fail any line like that in code review (or give very careful review in the rare case that it's calling a Java method that accepts a nullable parameter, but good modern Java libraries are moving away from those, so that case should go away).
- hota_mazi 10y agoThe main difference is that Kotlin can do both: nullable types or `Option` (and you'd never use `Option` unless you are dealing with it coming from Java, obviously). Scala can only do `Option` since it doesn't have built in support for nullable types. I fail to see how your point shows any superiority from Scala here, it's the other way around. At the end of the day, both languages have nullability support but since it's optional in Scala (because library based), Kotlin's approach is superior in that it requires developers to deal with nullability, while Scala developers can ignore it any time they want.
- lmm 10y agoNullable types are worse than Option in every way that matters (yes they save a few bytes of memory occasionally, but who the hell cares, if you care about shaving 8 bytes off an object you won't be using the JVM in the first place. It's certainly not worth the cost of working with a weird, second-class type that you can't abstract over like other types). Why do you want an extra way of representing the same thing with more special cases? > Kotlin's approach is superior in that it requires developers to deal with nullability But it doesn't force them to deal with nullability when interacting with Java which is the only case where it matters. Scala calling Scala: you never use null, you never hit a problem with null. Scala calling Java: you have to manually check return values for older libraries but at least passing in null can't happen. Kotlin calling Kotlin: you use null a lot, you never hit a problem with null. Kotlin calling Java: you have to manually check return values for older libraries and won't notice where you're passing in null because that's a normal, safe thing to do when calling Kotlin.
- esarbe 10y agoWhy on earth would you ever want to do this? This is not how you use an Option.
- Nycto 10y agoThat's not the point. The point is that any safety you think you have by using an Option is a little bit hand-wavy. That's not _bad_, but the argument being made by the OP was that nullability in Kotlin was unsafe and inconsistent.
- esarbe 10y agoMy argument is that while you use null assignment and nullability in Kotlin, in Scala you don't work with nulls.
- Nycto 10y agoIf your entire code base is in Kotlin, then nullability works exactly like Options. The difference is sugar. And the Kotlin compiler can handle null checks as well as the Scala compiler handles Options. The problems arise when you interop with Java. At that point, shit can be null and you just don't know. My point is that it's the same problem in Scala as it is in Kotlin: Java interop adds a measure of unpredictability that needs to be handled at the touch points.
- esarbe 10y agoI agree on all these points.
- nostrademons 10y agoIn 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.