# Kotlin null safety with generic types

**URL:** <https://discuss.kotlinlang.org/t/kotlin-null-safety-with-generic-types/22248>\
**Category:** Support\
**Created:** [June 30, 2021, 8:37am UTC](https://discuss.kotlinlang.org/t/kotlin-null-safety-with-generic-types/22248 "2021-06-30T08:37:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KappaCris](https://avatars.discourse-cdn.com/v4/letter/k/258eb7/32.png) [@KappaCris](https://discuss.kotlinlang.org/u/KappaCris)\
**Post date:** [June 30, 2021, 8:37am UTC](https://discuss.kotlinlang.org/t/kotlin-null-safety-with-generic-types/22248/1 "2021-06-30T08:37:50Z")

</div>

Hi, I am new to Kotlin, but familiar with Scala. I’ve been going though kotlinlang documentation.  
I came up to the point about generics where it says

> The default upper bound (if none specified) is `Any?`

Does this mean I cannot rely on compiler for null safety on B in the case of` A<B>`?

example

```kotlin

class Down<T>(val x:T)

fun <String> jump(x : Down<String>):Down<String> {
  println("Jumped")
  return x
}

val jump = jump(Down(null))
println(jump)
println(jump.x) - null

```

Would I have to change `fun <String>` jump to `fun <String:Any> jump`?  
I mean yes that would do it but is this the correct way to approach this?

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [June 30, 2021, 9:09am UTC](https://discuss.kotlinlang.org/t/kotlin-null-safety-with-generic-types/22248/2 "2021-06-30T09:09:52Z")

</div>

Yes, if you have a parameterized type declared as `MyClass<T>` then `T` could be nullable. And if you need to work with not null values only, then you need to declare it as: `MyClass<T : Any>`.

I’m not sure, why do you say that you can’t rely on the compiler for null-safety here. If you allowed null values then you need to handle them 🙂

Also, your example is very misleading and I’m not sure if you did this intentionally or not. I just want to make sure that you are aware that your `jump()` function isn’t at all related to strings and it could receive any `Down` instance? `x` could be nullable, it could be an integer, etc.

---

<div class="post-metadata">

**Author:** ![KappaCris](https://avatars.discourse-cdn.com/v4/letter/k/258eb7/32.png) [@KappaCris](https://discuss.kotlinlang.org/u/KappaCris)\
**Post date:** [June 30, 2021, 10:02am UTC](https://discuss.kotlinlang.org/t/kotlin-null-safety-with-generic-types/22248/3 "2021-06-30T10:02:41Z")

</div>

It seems a bit confusing that return type says `Down<String>`. Yet I can still have a null reference `Down.x`. I would somehow expect to be forced to say `Down<String?>` 🙂 . Or otherwise not be allowed a `x <-null`

I mean similarly to how you treat non-generics eg `Animal` vs `Animal?` ? Maybe my lack of understanding some key concepts here?

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [June 30, 2021, 10:10am UTC](https://discuss.kotlinlang.org/t/kotlin-null-safety-with-generic-types/22248/4 "2021-06-30T10:10:11Z")

</div>

Ok, so I guess your example wasn’t intentional, but it was a mistake 😉

By doing this: `fun <String>` you declared a parameter type `String`, overriding class `String`. This function has nothing to do with strings. It is an equivalent of:

```auto
fun <T> jump(x : Down<T>):Down<T>

```

So it can receive and return any `Down` objects, including ones where `T` is nullable. If you want your `jump()` function to work only with strings and only non-nullable, then just remove `<String>`. You will notice that you are no longer able to pass `Down(null)` to it and you can be sure that `x` is not null.

---

<div class="post-metadata">

**Author:** ![KappaCris](https://avatars.discourse-cdn.com/v4/letter/k/258eb7/32.png) [@KappaCris](https://discuss.kotlinlang.org/u/KappaCris)\
**Post date:** [June 30, 2021, 10:34am UTC](https://discuss.kotlinlang.org/t/kotlin-null-safety-with-generic-types/22248/5 "2021-06-30T10:34:37Z")

</div>

Ah yes. That was my mistake. I actually started from `fun <T> jump(x : Down<T>):Down<T>`, but I forgot to carry the 1 and ended up in a totally different place 😃

Thanks!
