Generics and nullable types


#1

I'm still trying to come to grips with nullable types and generics. Let's say I want to create a function that takes a nullable type, and returns a non-null version of that say type. For simplicity, something like:

fun toNonNull<T>(t: T): T {   if (t == null) throw NullPointerException()   return t!! }

Obviously, this doesn’t work, but I’m not sure how I would write something that would achieve the same effect. How do I write a generic function that accepts a nullable version of a type but returns the non-nullable version of that same type.


#2

OK, I think I got it. I was getting confused with the nullable upper bound warning that comes up. Here's a version that works:

fun toNonNull<T:Any>(t: T?): T {   if (t == null) throw NullPointerException()   return t }

I suppose once the plugin matures, you would offer to make the fix for me? (ie. Add Non-Null Upper Bound)

#3

Feel free to file a request about the quick fix