# What's the proper way to use Array\<T?\>

**URL:** <https://discuss.kotlinlang.org/t/whats-the-proper-way-to-use-array-t/954>\
**Category:** Uncategorized\
**Created:** [January 1, 2013, 8:41pm UTC](https://discuss.kotlinlang.org/t/whats-the-proper-way-to-use-array-t/954 "2013-01-01T20:41:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![saltnlight5](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/saltnlight5/32/1525_2.png) [@saltnlight5](https://discuss.kotlinlang.org/u/saltnlight5)\
**Post date:** [January 1, 2013, 8:41pm UTC](https://discuss.kotlinlang.org/t/whats-the-proper-way-to-use-array-t/954/1 "2013-01-01T20:41:54Z")

</div>

Hi there,

The following code generate a compiler **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**

What is a proper way to rewrite it so there is no WARNING?

```kotlin
class MyStack<T> {
    private var currentIndex : Int = -1
    private var elements = Array<T?>(16) { i -> null }

  private fun ensureCapacity() {
  if (currentIndex + 1 >= elements.size) {
           val tmp = elements
           elements = Array<T?>(elements.size + 2 * elements.size) { i -> null }
           for (i in tmp.indices)
           elements[i] = tmp[i]
  }
  }
}
```

Thanks  
Zemian

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [January 1, 2013, 9:37pm UTC](https://discuss.kotlinlang.org/t/whats-the-proper-way-to-use-array-t/954/2 "2013-01-01T21:37:48Z")

</div>

You can write: ``

```kotlin
class MyStack<T: Any> {
...
```

---

<div class="post-metadata">

**Author:** ![saltnlight5](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/saltnlight5/32/1525_2.png) [@saltnlight5](https://discuss.kotlinlang.org/u/saltnlight5)\
**Post date:** [January 1, 2013, 10:57pm UTC](https://discuss.kotlinlang.org/t/whats-the-proper-way-to-use-array-t/954/3 "2013-01-01T22:57:47Z")

</div>

Ah, that works. Thanks!

Would you able to explain to me why is that needed? It seems little un-intuitive to me.

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [January 2, 2013, 1:56pm UTC](https://discuss.kotlinlang.org/t/whats-the-proper-way-to-use-array-t/954/4 "2013-01-02T13:56:46Z")

</div>

Perhaps You will find the answer [here](http://youtrack.jetbrains.com/issue/KT-2991#comment=27-396348).

---

<div class="post-metadata">

**Author:** ![saltnlight5](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/saltnlight5/32/1525_2.png) [@saltnlight5](https://discuss.kotlinlang.org/u/saltnlight5)\
**Post date:** [January 5, 2013, 3:13pm UTC](https://discuss.kotlinlang.org/t/whats-the-proper-way-to-use-array-t/954/5 "2013-01-05T15:13:26Z")

</div>

Thanks for the link bashor. I have read through the comments on that Issue. So let me try to conclude my understand and see if I get this right.

1. Kotlin defualt generic type parameter with nullable upper bound, so when I write “class MyStack\<T\>”, it’s equavalent to “class MyStack\<T : Any?\>”. This is done so it can fit existing Java API and backward compatible. Am I right?

2. Now becuase of “class MyStack\<T : Any?\>”, and if I use things like “var elements = Array\<T?\>(16) { i -\> null }” in the class body, the compiler issue warnings about “T?” because it might confuse user. So to get rid of the warning one can re-declare the clas to “class MyStack\<T : Any\>”. Am I right?

3. With above change, how significant that has impact MyStack implementation? What I mean is that what is the real difference in behavior wise between “class MyStack\<T : Any?\>” to "“class MyStack\<T : Any\>”? This is not an easy concept for beginner to get and learn. As you can see, one is only interesting writing a Stack implementation, why should he/she care “T : Any” or “T: Any?” as generic type? Can Kotlin be improved on this area?

Thanks,  
Zemian

---

<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:** [January 9, 2013, 10:37am UTC](https://discuss.kotlinlang.org/t/whats-the-proper-way-to-use-array-t/954/6 "2013-01-09T10:37:09Z")

</div>

1. Yes 2. Yes 3. Why one should care: you are creating a library class, that must respect nulls or prohibit them, this is a part of the contract of the class, and the language makes you think about it. Feel free to suggest an improvement if you see one.
