
Java 8, Scala, Groovy and Kotlin Optional/Option APIsįirst of all, this is not a new topic and a lot has already been discussed about it.“A clever person SOLVES a problem, a wise person AVOIDS it.” This post is part of Square’s “ Square Open Source ♥s Kotlin” series.Optional values are very useful but unfortunately not available in Android (Java and Kotlin Standard Library) so in this post we will see how we can make use of them presenting different practical use case scenarios. For these cases, in Java and Kotlin alike, the use of Optional has its place. As we’ve seen above, though, just having these annotations or a type system that can model nullability sometimes is not enough. Today’s Retrofit 2.3.0 release contains the same JSR 305 annotations for explicit nullability in Java and Kotlin as our recent Okio and OkHttp releases. interface MyApiService userSettings(): Observable**> With one of the Optional converters added alongside a serialization converter, requests whose bodies may deserialize to null can be changed to instead return an Optional. addCallAdapter(RxJava2CallAdapterFactory.create())

addConverterFactory(WireConverterFactory.create()) addConverterFactory(Java8OptionalConverterFactory.create()) Instead, they delegate to other converters for processing the bytes and then wrap the potentially-nullable result into an Optional. Unlike the other converters for libraries like Moshi, Gson, and Wire, these new ones are different in that they don’t actually convert bytes to objects. Retrofit 2.3.0 introduces two new delegating converters for the Optional types in Guava and Java 8.

Thus, in order to represent the absence of a value for the response body inside this stream we need an abstraction like Optional. If we’re using RxJava 2 and the converter for Settings returns null an exception will occur. RxJava 2 differs from RxJava 1 in that it does not allow null in its streams.


interface MyApiService userSettings(): Observable Just because you can explicitly express nullability, however, does not mean that null is always allowed.įor example, Retrofit provides adapters for RxJava 1.x and 2.x which allow modeling your requests as a single-element stream. With nullability being a first-class citizen in Kotlin’s type system, the need for an Optional type seems all but diminished.
