# Nullable upper bound

**URL:** <https://discuss.kotlinlang.org/t/nullable-upper-bound/351>\
**Category:** Uncategorized\
**Created:** [December 16, 2014, 11:55pm UTC](https://discuss.kotlinlang.org/t/nullable-upper-bound/351 "2014-12-16T23:55:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [December 16, 2014, 11:55pm UTC](https://discuss.kotlinlang.org/t/nullable-upper-bound/351/1 "2014-12-16T23:55:43Z")

</div>

I was wondering if somebody could explain this to me, something I'm not understanding.

&nbsp;

```kotlin
public class Foo<T> {

```

  

This will provide the warning: T has a nullable upper bound. This means that a value of this type may be null. Using T? is likely to mislead the reader.

Is this because the parameterized type T could potentially be a nullable type? Such as Foo\<String?\>, which would make bar: T? redundant?

How would you write the following without warnings?&nbsp;&nbsp;(suppressing the warning doesn’t count)

```kotlin

```

---

<div class="post-metadata">

**Author:** ![orangy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/orangy/32/1462_2.png) [@orangy](https://discuss.kotlinlang.org/u/orangy)\
**Post date:** [December 17, 2014, 9:20am UTC](https://discuss.kotlinlang.org/t/nullable-upper-bound/351/2 "2014-12-17T09:20:00Z")

</div>

If you expect the type argument used to substitute T to be always non-null, like game objects or something, you can use type parameter constraints to specify it like this:

```kotlin
public class Foo<T : Any> {

```

`
`

---

<div class="post-metadata">

**Author:** ![mikehearn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mikehearn/32/1424_2.png) [@mikehearn](https://discuss.kotlinlang.org/u/mikehearn)\
**Post date:** [December 18, 2014, 11:18am UTC](https://discuss.kotlinlang.org/t/nullable-upper-bound/351/3 "2014-12-18T11:18:53Z")

</div>

It'd be nice if this warning had clearer wording. I encountered it yesterday and found it very confusing \*despite\* having already seen this thread! I had to come back to it for a reminder on what it was talking about and what the solution might be. I do not have any suggestions though.

---

<div class="post-metadata">

**Author:** ![steamstreet](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/steamstreet/32/1529_2.png) [@steamstreet](https://discuss.kotlinlang.org/u/steamstreet)\
**Post date:** [December 23, 2014, 6:14am UTC](https://discuss.kotlinlang.org/t/nullable-upper-bound/351/4 "2014-12-23T06:14:00Z")

</div>

I had a similar question. Let's say I have a generic class that I'd like to allow both nullable and non-nullable types. However, one of the methods I'd like to allow for a return type that could be null, regardless of the generic used. Something like this:

&nbsp;&nbsp;trait Test\<K\> {  
&nbsp;&nbsp;fun doSomething(): K?  
&nbsp;&nbsp;}

This seems like a reasonable type of construct, yet it creates a nullable upper warning. It doesn’t impact the execution of the code, but I’d like to not have this warning without having to do all kinds of comments. How could write the code to eliminate this warning?

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [December 23, 2014, 6:54am UTC](https://discuss.kotlinlang.org/t/nullable-upper-bound/351/5 "2014-12-23T06:54:12Z")

</div>

yeah, in the case of an output type, I don't think it's redundant, but internally it is

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [December 23, 2014, 11:25am UTC](https://discuss.kotlinlang.org/t/nullable-upper-bound/351/6 "2014-12-23T11:25:21Z")

</div>

Please note that clients of this class will not be able to distinguish a null returned by doSomething() as an indication that something went wrong from a null that is legitimately a value of T. Java's Map interface is a clasical example of this problem: when get() returns null, we don't know whether it means that the corresponding key is not present, or it is present, but the value for it is null. This warning is reported to let you avoid such problems in your APIs

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [January 18, 2015, 7:06pm UTC](https://discuss.kotlinlang.org/t/nullable-upper-bound/351/7 "2015-01-18T19:06:27Z")

</div>

Yeah, this is making sense now.

The challenge is balancing correctness with simplicity.  
Using map as an example, in order to avoid get() being ambiguous, the map implementation would need to take initialization functions for both keys and values. This would add a significant amount of complexity to creating a basic map. You’d also no longer be able to create a map with a nullable upper bound in a generic context.
