# Using Generic as non-nullable property

**URL:** https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492
**Category:** Support
**Created:** [October 13, 2020, 9:02pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492 "2020-10-13T21:02:31Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![jano](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jano](https://discuss.kotlinlang.org/u/jano)
#### Post date: [October 13, 2020, 9:02pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/1 "2020-10-13T21:02:31Z")

</div>

It is possible to define a property, that is nullable regardless of the nullability of the generic:

```auto
interface Dut<T> {
   val field: T?
}

```

I would like to have a field the other way round, but I can not define the generic self as not nullable, as `field` may or may not be nullable:

```auto
interface Dut<T> {
   val field: T // may be nullable or not
   val listWithoutNulls: List<T!> // the exclamation mark is actually not working, but how may I define this?
}

```

---

<div class="post-metadata">

### Author: ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)
#### Post date: [October 13, 2020, 9:11pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/2 "2020-10-13T21:11:15Z")

</div>

You can define a base generic type as non-nullable:

```kotlin
interface Dut<T : Any> {
	val field1: T // will always be non-null
	val field2: T? // can be null
	val field3: List<T> // list of non-nulls
	val field4: List<T?> // list of potential nulls
}

```

---

<div class="post-metadata">

### Author: ![jano](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jano](https://discuss.kotlinlang.org/u/jano)
#### Post date: [October 13, 2020, 9:31pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/3 "2020-10-13T21:31:25Z")

</div>

I can’t define it like that, as mentioned in the example above.

At the call site the “user” determines if the property `field` may be null or not, but the property `listWithoutNulls` is always a list without nulls:

```auto
val dut1: Dut<String> // field is not nullable, the list contains strings
val dut2: Dut<String?> // field is nullable, the list contains strings

```

I can not achieve this with example.

---

<div class="post-metadata">

### Author: ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)
#### Post date: [October 13, 2020, 9:33pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/4 "2020-10-13T21:33:50Z")

</div>

You are a little wrong here. The line `val dut2: Dut<String?>` won’t even compile if you put the type constraint I showed 😉 `<T : Any>` means that a type can’t even be declared as nullable.

---

<div class="post-metadata">

### Author: ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)
#### Post date: [October 13, 2020, 9:35pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/5 "2020-10-13T21:35:44Z")

</div>

I think @jano wants to allow the caller to specify nullability for some member types but force others.

Currently you can force a return type to be a nullable version given a `T`, but he wants to force a non-nullable given any `T`.

---

<div class="post-metadata">

### Author: ![jano](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jano](https://discuss.kotlinlang.org/u/jano)
#### Post date: [October 13, 2020, 9:36pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/6 "2020-10-13T21:36:04Z")

</div>

Yes, I refer to my example above, where exactly this is (should be) possible.

---

<div class="post-metadata">

### Author: ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)
#### Post date: [October 13, 2020, 9:44pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/7 "2020-10-13T21:44:55Z")

</div>

Here’s a runnable example of the question:

```run-kotlin
interface Foo<T> {
    val couldBeNullableOrNot: T
    val nullableAlways: T? // Totally fine even if T is already nullable.
}

interface Bar<T> {
    val couldBeNullableOrNot: T
    val neverNull: T!! // ERROR How does one specify non-nullable
}

interface Bizz<T> {
    val couldBeNullableOrNot: T
    fun <B> neverNull(): B where 
    	B : T, 
    	B : Any // ERROR Can't use multiple bounds when bounded by another type param
}

fun main() { println("noop") }

```

---

<div class="post-metadata">

### Author: ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)
#### Post date: [October 13, 2020, 9:47pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/8 "2020-10-13T21:47:41Z")

</div>

Then how about this?

```auto
interface Dut<T : E?, E : Any> {
	val field: T
	val listWithoutNulls: List<E>
}

```

`E` must be non-nullable, but `T` can be either nullable or not depending on call-site. You can either use type inference or you must specify both types at once, eg:

```kotlin
Dut<Int?, Int>
Dut<Int, Int>

```

---

<div class="post-metadata">

### Author: ![jano](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jano](https://discuss.kotlinlang.org/u/jano)
#### Post date: [October 13, 2020, 9:58pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/9 "2020-10-13T21:58:46Z")

</div>

> [@madmax1028](#):
>
> ```auto
> interface Dut<T : E?, E : Any> {
> val field: T
> val listWithoutNulls: List<E>
> }
> 
> ```

Thanks a lot, that seems to do the trick. It may be nullable and both types have to be exactly the same.

I don’t like it very much, as I always have to specify both types, but it is a workaround. If anyone has a solution with a single generic I would be fully satisfied 🙂

---

<div class="post-metadata">

### Author: ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)
#### Post date: [October 13, 2020, 10:03pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/10 "2020-10-13T22:03:09Z")

</div>

Maybe it would be possible if they implement default generic parameters: [Default Types for Generics](https://discuss.kotlinlang.org/t/default-types-for-generics/7129). For now it’s the best I could think of 🙂

A similar feature already exists in TypeScript: [Generic Parameter Defaults in TypeScript — Marius Schulz](https://mariusschulz.com/blog/generic-parameter-defaults-in-typescript).

---

<div class="post-metadata">

### Author: ![ark1](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@ark1](https://discuss.kotlinlang.org/u/ark1)
#### Post date: [October 19, 2020, 8:13pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/11 "2020-10-19T20:13:11Z")

</div>

funny thing here is that the notation for the type already exists, but it cannot be specified explicitly, it exists just to present type information to the reader:  
 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/7/76a0c3a9ccba36c4b6d593474f8e79f54dce8637.png)

---

<div class="post-metadata">

### Author: ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)
#### Post date: [October 19, 2020, 9:16pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/12 "2020-10-19T21:16:47Z")

</div>

Maybe it’s time to add it for real then 😉

---

<div class="post-metadata">

### Author: ![ark1](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@ark1](https://discuss.kotlinlang.org/u/ark1)
#### Post date: [October 20, 2020, 8:48am UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/13 "2020-10-20T08:48:52Z")

</div>

and this one too? 🙂  
 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/e/ecca4afd1e469a0d74743077c220aa0703d8ca2b.png)

disclaimer: I’m not from Kotlin team, I’m just a user of Kotlin, like you, but from JetBrains

---

<div class="post-metadata">

### Author: ![ark1](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@ark1](https://discuss.kotlinlang.org/u/ark1)
#### Post date: [October 20, 2020, 8:56am UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/14 "2020-10-20T08:56:16Z")

</div>

and maybe this syntax should also be added?

 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/b/bfede1c7addabe035385b5ad52d3f52bbabb5846.png)  
(sorry for off-topic)

---

<div class="post-metadata">

### Author: ![jano](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jano](https://discuss.kotlinlang.org/u/jano)
#### Post date: [October 20, 2020, 5:37pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/15 "2020-10-20T17:37:52Z")

</div>

I created a [feature request](https://youtrack.jetbrains.com/issue/KT-42714).

It’s marked as duplicate of another [request](https://youtrack.jetbrains.com/issue/KT-26245) (that is not exactly the same, but the feature is included in the discussion - so good enough), that seems to be merged.

---

<div class="post-metadata">

### Author: ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)
#### Post date: [April 5, 2022, 8:12pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/16 "2022-04-05T20:12:05Z")

</div>

It seems like the problem now can be solved in much more straightforward way than before using the new [Definitely non-nullable types](https://blog.jetbrains.com/kotlin/2022/04/kotlin-1-6-20-released/#definitely-non-nullable-types) feature:

```kotlin
interface Dut<T> {
	val field: T
	val listWithoutNulls: List<T & Any>
}

```

---

<div class="post-metadata">

### Author: ![corbella83](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@corbella83](https://discuss.kotlinlang.org/u/corbella83)
#### Post date: [April 25, 2022, 11:05am UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/17 "2022-04-25T11:05:19Z")

</div>

I don’t think is good option the definitely non-nullable types:

[https://youtrack.jetbrains.com/issue/KT-52173/Unconsistency-of-generic-and-non-generic-types-using-Definitely-](https://youtrack.jetbrains.com/issue/KT-52173/Unconsistency-of-generic-and-non-generic-types-using-Definitely-)

---

<div class="post-metadata">

### Author: ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)
#### Post date: [August 5, 2022, 12:33pm UTC](https://discuss.kotlinlang.org/t/using-generic-as-non-nullable-property/19492/18 "2022-08-05T12:33:00Z")

</div>

Since Kotlin 1.7 it is also possible to use [Underscore operator for type arguments](https://kotlinlang.org/docs/whatsnew17.html#underscore-operator-for-type-arguments) feature to simplify my previous solution:

```kotlin
data class Dut<T : E?, E : Any>(
	val field: T,
	val listWithoutNulls: List<E>,
)

val dut = Dut<Int?, _>(null, listOf(1, 2, 3)))

```

Although for this case probably [Definitely non-nullable types](https://blog.jetbrains.com/kotlin/2022/04/kotlin-1-6-20-released/#definitely-non-nullable-types) still seems better to me.
