Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahhaggarty committed Aug 15, 2023
1 parent b38c19c commit 60608a1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 53 deletions.
54 changes: 11 additions & 43 deletions docs/topics/generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,67 +327,35 @@ 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:

```java
import org.jetbrains.annotations.*;

public interface A<T> {
public T foo(T x) { return x; }
public interface Game<T> {
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<T1> : A<T1> {
override fun foo(x: T1): T1
//Subtype T1 is definitely non-nullable
override fun bar(x: T1 & Any): T1 & Any
interface ArcadeGame<T1> : Game<T1> {
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 <T> elvisLike(x: T, y: T & Any): T & Any = x ?: y

fun main() {
// T is String
elvisLike<String>("", "").length // OK
elvisLike<String>("", null).length // Error: 'null' for non-nullable type

// T is nullable String (String?)
elvisLike<String?>(null, "").length // OK
elvisLike<String?>(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

Expand Down
20 changes: 10 additions & 10 deletions docs/topics/jvm/java-to-kotlin-nullability-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,21 @@ For example, consider this `bar()` method in Java:
```java
import org.jetbrains.annotations.*;

public interface A<T> {
public T foo(T x) { return x; }
@NotNull
public T bar(@NotNull T x) {}
public interface Game<T> {
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<T1> : A<T1> {
override fun foo(x: T1): T1
//Subtype T1 is definitely non-nullable
override fun bar(x: T1 & Any): T1 & Any
interface ArcadeGame<T1> : Game<T1> {
override fun save(x: T1): T1
// T1 is definitely non-nullable
override fun load(x: T1 & Any): T1 & Any
}
```

Expand Down

0 comments on commit 60608a1

Please sign in to comment.