# Few Questions Related to Generics

**URL:** https://discuss.kotlinlang.org/t/few-questions-related-to-generics/771
**Category:** Uncategorized
**Created:** [July 23, 2013, 11:52am UTC](https://discuss.kotlinlang.org/t/few-questions-related-to-generics/771 "2013-07-23T11:52:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![vietnorm](https://avatars.discourse-cdn.com/v4/letter/v/7cd45c/32.png) [@vietnorm](https://discuss.kotlinlang.org/u/vietnorm)
#### Post date: [July 23, 2013, 11:52am UTC](https://discuss.kotlinlang.org/t/few-questions-related-to-generics/771/1 "2013-07-23T11:52:09Z")

</div>

Hi,

First question is related to instantiating vars of generic type:

&nbsp;&nbsp;class A\<T\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// if T’s upper bound is ‘Any?’, why is this illegal:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var t : T = null

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// this compiles, but with a warning: “‘T’ has a nullable upper bound…”  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var t : T? = null  
&nbsp;&nbsp;}

BTW, what I actually want there is

&nbsp;&nbsp;var t: T = default(T)

but I don’t know how to do that in Kotlin.

Second question is about method overloading. Not terribly important, but I’m curious. I’d like to know if you’re planning to add support for this.

If I have a method like this one:

&nbsp;&nbsp;fun abc (a:String) : Any? {…}

Why can’t I have a generic version with same name:

&nbsp;&nbsp;fun abc\<T\> (a: String) : T {…}

Thank you for your time 🙂

---

<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: [July 23, 2013, 12:20pm UTC](https://discuss.kotlinlang.org/t/few-questions-related-to-generics/771/2 "2013-07-23T12:20:04Z")

</div>

> &nbsp;&nbsp;class A\<T\> { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// if T's upper bound is 'Any?', why is this illegal: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var t : T = null

Because it's only an \_upper\_ bound, i.e. T may be String, and assgning a null to String (not String?) is not allowed.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// this compiles, but with a warning: "'T' has a nullable upper bound..." &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var t : T? = null

T may be Foo?, and in that case T? is Foo? as well, so when you see null, you have no way to tell whether it comes as a value of T or as value of T?

> BTW, what I actually want there is
> 
> &nbsp;&nbsp;var t: T = default(T)

What is “default(some non-null type)”? For example, “default(PrintStream)”? We could agree on defaults for things like Int or String or Foo?, but not for an arbitrary non-null type.

> Second question is about method overloading. Not terribly important, but I'm curious. I'd like to know if you're planning to add support for this.
> 
> If I have a method like this one:
> 
> &nbsp;&nbsp;fun abc (a:String) : Any? {…}
> 
> Why can’t I have a generic version with same name:
> 
> &nbsp;&nbsp;fun abc\<T\> (a: String) : T {…}

Because of how JVM works: generics are [erased](http://docs.oracle.com/javase/tutorial/java/generics/erasure.html) and effective binary signatures of the two methods above are the same, which is not allowed.

---

<div class="post-metadata">

### Author: ![vietnorm](https://avatars.discourse-cdn.com/v4/letter/v/7cd45c/32.png) [@vietnorm](https://discuss.kotlinlang.org/u/vietnorm)
#### Post date: [July 23, 2013, 2:03pm UTC](https://discuss.kotlinlang.org/t/few-questions-related-to-generics/771/3 "2013-07-23T14:03:14Z")

</div>

Thank you, Andrey.

This is my 3rd or 4th attempt at replying 🙂 As I was trying to reply with more questions, I became more and more aware of how little I know, and how stupid those questions would be. Now I only have one simple question 🙂 How do I make sure T is a nullable type?

---

<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: [July 23, 2013, 2:15pm UTC](https://discuss.kotlinlang.org/t/few-questions-related-to-generics/771/4 "2013-07-23T14:15:50Z")

</div>

Currently there's no way to make sure thet a type variable will definitely hold a nullable type. What you can do is make sure it holds a non-null type, and then use T? to make sure the type is nullable:

```kotlin
class C<T: Any> { // T can not be nullable
  var x: T? = null // no warning here

} 
```
