Files
spring-framework/framework-docs/modules/ROOT/pages/languages/kotlin/extensions.adoc
Simon Baslé 8567402969 Extract recurring asciidoc links to attributes, cleanup old doc files
This commit extract spring-related links and recurring external links
into asciidoctor attributes to be used by the Antora toolchain.

It notably homogenizes links to:
 - IETF RFCs
 - Java Community Process JSRs
 - the Java API Documentation (on the Java 17 version)
 - Kotlin documentations (on the Kotlinlang.org version)
 - the Spring Boot reference guide (on the `html` version)

This commit also reworks most link attributes to follow a
Project-Category-Misc syntax. For example, `spring-boot-docs` rather
than `docs-spring-boot`.

Finally, it makes an effort to clean up remainders from the previous
documentation toolchain, namely the `docs/asciidoc` folder and 
`modules/ROOT/pages/attributes.adoc` file.

Closes gh-26864
Closes gh-31619
2023-11-21 15:59:24 +01:00

47 lines
1.9 KiB
Plaintext

[[kotlin-extensions]]
= Extensions
Kotlin {kotlin-docs}/extensions.html[extensions] provide the ability
to extend existing classes with additional functionality. The Spring Framework Kotlin APIs
use these extensions to add new Kotlin-specific conveniences to existing Spring APIs.
The {spring-framework-api-kdoc}/[Spring Framework KDoc API] lists
and documents all available Kotlin extensions and DSLs.
NOTE: Keep in mind that Kotlin extensions need to be imported to be used. This means,
for example, that the `GenericApplicationContext.registerBean` Kotlin extension
is available only if `org.springframework.context.support.registerBean` is imported.
That said, similar to static imports, an IDE should automatically suggest the import in most cases.
For example, {kotlin-docs}/inline-functions.html#reified-type-parameters[Kotlin reified type parameters]
provide a workaround for JVM {java-tutorial}/java/generics/erasure.html[generics type erasure],
and the Spring Framework provides some extensions to take advantage of this feature.
This allows for a better Kotlin API `RestTemplate`, for the new `WebClient` from Spring
WebFlux, and for various other APIs.
NOTE: Other libraries, such as Reactor and Spring Data, also provide Kotlin extensions
for their APIs, thus giving a better Kotlin development experience overall.
To retrieve a list of `User` objects in Java, you would normally write the following:
[source,java,indent=0]
----
Flux<User> users = client.get().retrieve().bodyToFlux(User.class)
----
With Kotlin and the Spring Framework extensions, you can instead write the following:
[source,kotlin,indent=0]
----
val users = client.get().retrieve().bodyToFlux<User>()
// or (both are equivalent)
val users : Flux<User> = client.get().retrieve().bodyToFlux()
----
As in Java, `users` in Kotlin is strongly typed, but Kotlin's clever type inference allows
for shorter syntax.