# Nullable generic parameter

**URL:** <https://discuss.kotlinlang.org/t/nullable-generic-parameter/7553>\
**Category:** Uncategorized\
**Created:** [April 26, 2018, 7:30am UTC](https://discuss.kotlinlang.org/t/nullable-generic-parameter/7553 "2018-04-26T07:30:04Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![vagran](https://avatars.discourse-cdn.com/v4/letter/v/b5ac83/32.png) [@vagran](https://discuss.kotlinlang.org/u/vagran)\
**Post date:** [April 29, 2018, 5:02pm UTC](https://discuss.kotlinlang.org/t/nullable-generic-parameter/7553/21 "2018-04-29T17:02:10Z")

</div>

@darksnake I don’t think the approach with non-nullable front will work. It will throw exception in any case when `nullable` is null which is not what is needed if the generic type parameter is a nullable one.  
If you think you can plan architecture in a more clear way, please check [my related question on StackOverflow](https://stackoverflow.com/questions/50041766/how-to-implement-mutable-optional-in-kotlin) I have posted a moment before I have got the answer here. It has a clear requirement for `MutableOptional` class (the example I have started this topic with is a bit confusing, `MutableOptional` is much more understandable for people). Feel free to post your version of implementing such class.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [April 29, 2018, 5:16pm UTC](https://discuss.kotlinlang.org/t/nullable-generic-parameter/7553/22 "2018-04-29T17:16:23Z")

</div>

In this case you need explicit cast (though I am not sure that you need to suppress warning). But when I am talking about architecture, I propose to think of whether you need such class in the first place.

---

<div class="post-metadata">

**Author:** ![wernera77](https://avatars.discourse-cdn.com/v4/letter/w/ce7236/32.png) [@wernera77](https://discuss.kotlinlang.org/u/wernera77)\
**Post date:** [May 17, 2020, 8:03pm UTC](https://discuss.kotlinlang.org/t/nullable-generic-parameter/7553/23 "2020-05-17T20:03:30Z")

</div>

In Swift you have the concept of a recursive optional (an optional of an optional). That would neatly solve the issue I think.

```
class Foo<T> {
    value: T? = null // If T is an optional type (say Bar?), this would actually be an optional of an optional: Optional<Optional<Bar>> or Bar??
}

```

[Previous page](https://discuss.kotlinlang.org/t/nullable-generic-parameter/7553.md?page=1)
