From 60608a11f9eb6f6d2765b04f08bf2a060b5e3d06 Mon Sep 17 00:00:00 2001 From: Sarah Haggarty Date: Tue, 8 Aug 2023 17:06:20 +0200 Subject: [PATCH] review comments --- docs/topics/generics.md | 54 ++++--------------- .../jvm/java-to-kotlin-nullability-guide.md | 20 +++---- 2 files changed, 21 insertions(+), 53 deletions(-) diff --git a/docs/topics/generics.md b/docs/topics/generics.md index d87f6964d9c..5e726365243 100644 --- a/docs/topics/generics.md +++ b/docs/topics/generics.md @@ -327,13 +327,9 @@ must implement _both_ `CharSequence` and `Comparable`. To make interoperability with generic Java classes and interfaces easier, Kotlin supports declaring a generic type parameter as **definitely non-nullable**. -To declare a generic type `T` as definitely non-nullable, declare the type with `& Any`. For example: `T & Any`. +To declare a generic type `T` as definitely non-nullable, declare the type with `& Any`. For example: `T & Any`. -> The syntax represents an intersection between type `T` and the non-nullable type `Any`. -> -{type="note"} - -A definitely non-nullable type must have a nullable [upper bound](#upper-bounds). In addition, `Any` must resolve to `kotlin.Any`. +A definitely non-nullable type must have a nullable [upper bound](#upper-bounds). The most common use case for declaring definitely non-nullable types is when you want to override a Java method that contains `@NotNull` as an argument. For example, consider the `bar()` method: @@ -341,53 +337,25 @@ contains `@NotNull` as an argument. For example, consider the `bar()` method: ```java import org.jetbrains.annotations.*; -public interface A { - public T foo(T x) { return x; } +public interface Game { + public T save(T x) {} @NotNull - public T bar(@NotNull T x) {} + public T load(@NotNull T x) {} } ``` -To override the `bar()` method in Kotlin successfully, you need `T` (and its subtypes) to be declared as definitely non-nullable: +To override the `bar()` method in Kotlin successfully, you need `T1` to be declared as definitely non-nullable: ```kotlin -interface B : A { - override fun foo(x: T1): T1 - //Subtype T1 is definitely non-nullable - override fun bar(x: T1 & Any): T1 & Any +interface ArcadeGame : Game { + override fun save(x: T1): T1 + // T1 is definitely non-nullable + override fun load(x: T1 & Any): T1 & Any } ``` When working only with Kotlin, it's unlikely that you will need to declare definitely non-nullable types explicitly because -Kotlin's type inference takes care of this for you. Here is an example of how to declare definitely non-nullable types -explicitly in a Kotlin function: - -```kotlin -fun elvisLike(x: T, y: T & Any): T & Any = x ?: y - -fun main() { - // T is String - elvisLike("", "").length // OK - elvisLike("", null).length // Error: 'null' for non-nullable type - - // T is nullable String (String?) - elvisLike(null, "").length // OK - elvisLike(null, null).length // Error: 'null' for non-nullable type - - // Kotlin infers T is nullable String (String?) - elvisLike("", "").length // OK - elvisLike("", null).length // Error: 'null' for non-nullable type - elvisLike(null, "").length // OK -} -``` - -In this example, the `elvisLike()` function has two arguments `x` and `y`. `x` is a generic type whereas `y` is a generic -type that is definitely non-nullable. The function returns a value of definitely non-nullable type. The function body -contains the [elvis operator](null-safety.md#elvis-operator) to check "if `x` is not `null`, use it, otherwise use `y`". - -The [`length`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/length.html) property of the `String` class -can't be called on a `null` value. The compiler prevents this from happening by reporting an error whenever -`y` is a `null` value, because `y` is declared as definitely non-nullable. +Kotlin's type inference takes care of this for you. ## Type erasure diff --git a/docs/topics/jvm/java-to-kotlin-nullability-guide.md b/docs/topics/jvm/java-to-kotlin-nullability-guide.md index 20ebc8c3e34..e7e63d81191 100644 --- a/docs/topics/jvm/java-to-kotlin-nullability-guide.md +++ b/docs/topics/jvm/java-to-kotlin-nullability-guide.md @@ -126,21 +126,21 @@ For example, consider this `bar()` method in Java: ```java import org.jetbrains.annotations.*; -public interface A { - public T foo(T x) { return x; } - @NotNull - public T bar(@NotNull T x) {} +public interface Game { + public T save(T x) {} + @NotNull + public T load(@NotNull T x) {} } ``` -To override the `bar()` method in Kotlin successfully, you need `T` (and its subtypes) to be declared as definitely -non-nullable (`T & Any`): +To override the `bar()` method in Kotlin successfully, you need `T1` to be declared as definitely +non-nullable (`T1 & Any`): ```kotlin -interface B : A { - override fun foo(x: T1): T1 - //Subtype T1 is definitely non-nullable - override fun bar(x: T1 & Any): T1 & Any +interface ArcadeGame : Game { + override fun save(x: T1): T1 + // T1 is definitely non-nullable + override fun load(x: T1 & Any): T1 & Any } ```