Kotlin support
Kotlin is a statically-typed language targeting the JVM (and other platforms), which allows writing concise and elegant code while providing very good interoperability with existing libraries written in Java.
This section describes Reactor’s support for Kotlin.
1. Requirements
Reactor supports Kotlin 1.1+ and requires
kotlin-stdlib
(or one of its kotlin-stdlib-jdk7
or kotlin-stdlib-jdk8
variants).
2. Extensions
As of As a consequence, Kotlin extensions in
|
Thanks to its great Java interoperability and to Kotlin extensions, Reactor Kotlin APIs leverage regular Java APIs and are additionally enhanced by a few Kotlin-specific APIs that are available out of the box within Reactor artifacts.
Keep in mind that Kotlin extensions need to be imported to be used. This means
for example that the Throwable.toFlux Kotlin extension
is available only if import reactor.kotlin.core.publisher.toFlux is imported.
That said, similar to static imports, an IDE should automatically suggest the import in most cases.
|
For example, Kotlin reified type parameters provide a workaround for JVM generics type erasure, and Reactor provides some extensions to take advantage of this feature.
The following table compares Reactor with Java against Reactor with Kotlin and extensions:
Java |
Kotlin with extensions |
|
|
|
|
|
|
|
|
|
|
|
|
The Reactor KDoc API lists and documents all the available Kotlin extensions.
3. Null Safety
One of Kotlin’s key features is null safety,
which cleanly deals with null
values at compile time rather than bumping into the famous
NullPointerException
at runtime. This makes applications safer through nullability
declarations and expressive “value or no value” semantics without paying the cost of wrappers such as Optional
.
(Kotlin allows using functional constructs with nullable values. See this
comprehensive guide to Kotlin null-safety.)
Although Java does not let one express null safety in its type-system, Reactor now provides null safety
of the whole Reactor API through tooling-friendly annotations declared
in the reactor.util.annotation
package.
By default, types from Java APIs used in Kotlin are recognized as
platform types
for which null-checks are relaxed.
Kotlin support for JSR 305 annotations
and Reactor nullability annotations provide null-safety for the whole Reactor API to Kotlin developers,
with the advantage of dealing with null
-related issues at compile time.
You can configure the JSR 305 checks by adding the -Xjsr305
compiler flag with the following
options: -Xjsr305={strict|warn|ignore}
.
For kotlin versions 1.1.50+, the default behavior is the same as -Xjsr305=warn
.
The strict
value is required to have the Reactor API full null-safety taken into account
but should be considered experimental, since the Reactor API nullability declaration could evolve
even between minor releases, as more checks may be added in the future).
Nullability for generic type arguments, variable arguments, and array elements is not supported yet, but itshould be in an upcoming release. See this dicussion for up-to-date information. |