# Keep nullability of function parameter type on return type?

**URL:** https://discuss.kotlinlang.org/t/keep-nullability-of-function-parameter-type-on-return-type/15233
**Category:** Support
**Created:** [November 29, 2019, 8:03pm UTC](https://discuss.kotlinlang.org/t/keep-nullability-of-function-parameter-type-on-return-type/15233 "2019-11-29T20:03:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)
#### Post date: [November 29, 2019, 8:03pm UTC](https://discuss.kotlinlang.org/t/keep-nullability-of-function-parameter-type-on-return-type/15233/1 "2019-11-29T20:03:47Z")

</div>

You can create a function that maintains the nullability of the parameter, on the return type if the parameter and the return type are the same.

See this example:

```kotlin
fun <T : String?> foo(bar: T): T = TODO()

```

For this function the return type relates to the parameter type like this:

| Parameter type | Return type |
| --- | --- |
| String | String |
| String? | String? |

Now my question is if it is possible to do the same thing but with the parameter type and the return type being different.

Something like this:

| Parameter type | Return type |
| --- | --- |
| String | Int |
| String? | Int? |

Thanks in advance.

---

<div class="post-metadata">

### Author: ![nbness1337](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@nbness1337](https://discuss.kotlinlang.org/u/nbness1337)
#### Post date: [November 29, 2019, 8:31pm UTC](https://discuss.kotlinlang.org/t/keep-nullability-of-function-parameter-type-on-return-type/15233/2 "2019-11-29T20:31:07Z")

</div>

You would have to use 2 parameters

`fun <T: String?, R: Int?> foo(bar: T): R = TODO()`

I don’t see any other way of doing it without a huge mess of code

---

<div class="post-metadata">

### Author: ![Varia](https://avatars.discourse-cdn.com/v4/letter/v/b9e5f3/32.png) [@Varia](https://discuss.kotlinlang.org/u/Varia)
#### Post date: [November 29, 2019, 8:45pm UTC](https://discuss.kotlinlang.org/t/keep-nullability-of-function-parameter-type-on-return-type/15233/3 "2019-11-29T20:45:06Z")

</div>

That’s not type safe. There is nothing preventing you from writing `val i: Int = foo(null)` with that signature.

Unfortunately, the only way to achieve the desired result is by overloading the function with 2 versions:

```auto
fun foo(s: String) : Int
fun foo(s: String?) : Int?

```

---

<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: [November 30, 2019, 2:29am UTC](https://discuss.kotlinlang.org/t/keep-nullability-of-function-parameter-type-on-return-type/15233/4 "2019-11-30T02:29:03Z")

</div>

I recently discussed an idea that could solve this on the kotlin slack.

```kotlin
data class Test<?null>(
    val foo: Foo<?null>, // nullable depending on class parameter
    val bar: Bar, // never null
    val baz: Baz:? // allways nullable
)
val test1 = Test<?>(getFooOrNull(), Bar(), null)
val test2 = Test<!>(Foo(), Bar(), null)
test1.foo?.doSomething()
test2.foo.doSomething()

```

The idea is to have a generic parameter that just contains nullability data. That way your code could look something like

```kotlin
fun foo<?null, T: String>(bar: T<?null>): T<?null> = TODO()

```

The current syntax is terrible so if anyone has a better idea it would be great 😉

---

<div class="post-metadata">

### Author: ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)
#### Post date: [December 3, 2019, 10:10am UTC](https://discuss.kotlinlang.org/t/keep-nullability-of-function-parameter-type-on-return-type/15233/5 "2019-12-03T10:10:59Z")

</div>

You can introduce an overload for the non-null type. It will choose that one as it is more specific. The only issue there is that you must specify an `@JvmName` annotation for either (the non-null version preferably) as the JVM doesn’t see the difference in signature - it is valid Kotlin, for Kotlin the signature is different). Alternatively, you may be able to use [contracts](https://github.com/Kotlin/KEEP/blob/master/proposals/kotlin-contracts.md) to handle this instead.

---

<div class="post-metadata">

### Author: ![mstawick](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mstawick/32/6124_2.png) [@mstawick](https://discuss.kotlinlang.org/u/mstawick)
#### Post date: [December 6, 2019, 8:29am UTC](https://discuss.kotlinlang.org/t/keep-nullability-of-function-parameter-type-on-return-type/15233/6 "2019-12-06T08:29:50Z")

</div>

It would be interesting to see if you can come up with a contract to solve this. I had the same idea some time ago, but couldn’t figure it out (or simply it wasn’t / isn’t possible), and I’ve ended up with the overload approach.

---

<div class="post-metadata">

### Author: ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)
#### Post date: [December 6, 2019, 11:41am UTC](https://discuss.kotlinlang.org/t/keep-nullability-of-function-parameter-type-on-return-type/15233/7 "2019-12-06T11:41:18Z")

</div>

So far the overload approach is the only solution, because currently contracts only allow you to specify the nullability of the function parameters based on the return value of the function and not the other way around which is what we would need.
