# Passing Map with Non-Null keys to function that takes map with nullable keys?

**URL:** <https://discuss.kotlinlang.org/t/passing-map-with-non-null-keys-to-function-that-takes-map-with-nullable-keys/14347>\
**Category:** Support\
**Created:** [September 20, 2019, 9:26pm UTC](https://discuss.kotlinlang.org/t/passing-map-with-non-null-keys-to-function-that-takes-map-with-nullable-keys/14347 "2019-09-20T21:26:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![diathesis](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/diathesis/32/4410_2.png) [@diathesis](https://discuss.kotlinlang.org/u/diathesis)\
**Post date:** [September 20, 2019, 9:26pm UTC](https://discuss.kotlinlang.org/t/passing-map-with-non-null-keys-to-function-that-takes-map-with-nullable-keys/14347/1 "2019-09-20T21:26:34Z")

</div>

Say I have a function:

```
fun doSomething( mapping: Map<String?,Boolean> ) 

```

And I have a map:

```
val mapping = mapOf( "One": true, "Two": false )

```

I can’t pass it in directly:

```
doSomething(mapping)

```

Because Kotlin says:

```
Required: Map<String?,Boolean>
Found: Map<String,Boolean>

```

I understand that a Map\<String,Boolean\> isn’t strictly the same thing as a Map\<String?,Boolean\> – so what’s the simplest way to convert the map so that I can pass it in? I feel like there ought to be a simple way to do this, but I haven’t figured out what the simple way is. 😉

---

<div class="post-metadata">

**Author:** ![rrlynx](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@rrlynx](https://discuss.kotlinlang.org/u/rrlynx)\
**Post date:** [September 20, 2019, 11:26pm UTC](https://discuss.kotlinlang.org/t/passing-map-with-non-null-keys-to-function-that-takes-map-with-nullable-keys/14347/2 "2019-09-20T23:26:33Z")

</div>

You have to either specify the type of `mapping` variable or call `mapOf` with explicit type parameters.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [September 20, 2019, 11:33pm UTC](https://discuss.kotlinlang.org/t/passing-map-with-non-null-keys-to-function-that-takes-map-with-nullable-keys/14347/3 "2019-09-20T23:33:19Z")

</div>

The problem is that the generic type for the key is invariant. You can however still pass the map like this

```auto
doSomething(mapping as Map<String?, Boolean>)

```

This will however generate an unchecked cast warning, which you can suppress.

---

<div class="post-metadata">

**Author:** ![diathesis](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/diathesis/32/4410_2.png) [@diathesis](https://discuss.kotlinlang.org/u/diathesis)\
**Post date:** [September 20, 2019, 11:45pm UTC](https://discuss.kotlinlang.org/t/passing-map-with-non-null-keys-to-function-that-takes-map-with-nullable-keys/14347/4 "2019-09-20T23:45:49Z")

</div>

> You have to either specify the type of `mapping` variable or call `mapOf` with explicit type parameters.

Yeah, sorry, the example is a simplification of reality. I have a `Map<String,morestuffhere>` that I get from somewhere else, I know how to make my own `Map<String?,morestuffhere>`, I’m just wondering what the cleanest way of converting the map I already have to the nullable-key version is.

---

<div class="post-metadata">

**Author:** ![diathesis](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/diathesis/32/4410_2.png) [@diathesis](https://discuss.kotlinlang.org/u/diathesis)\
**Post date:** [September 20, 2019, 11:48pm UTC](https://discuss.kotlinlang.org/t/passing-map-with-non-null-keys-to-function-that-takes-map-with-nullable-keys/14347/5 "2019-09-20T23:48:24Z")

</div>

> You can however still pass the map like this

Huh – I could swear I tried a cast (yay type erasure), but I just tried again and, yes, it works. Weird. Not sure why my attempt before failed now. 😉

---

<div class="post-metadata">

**Author:** ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)\
**Post date:** [September 21, 2019, 11:47am UTC](https://discuss.kotlinlang.org/t/passing-map-with-non-null-keys-to-function-that-takes-map-with-nullable-keys/14347/6 "2019-09-21T11:47:52Z")

</div>

Note that such a cast is unsafe. The implementation of `doSomething()` could call `get(null)` on the map (which would be perfectly legal, as its parameter explicitly allows a null key). The cast could then cause a null-pointer exception.

---

<div class="post-metadata">

**Author:** ![diathesis](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/diathesis/32/4410_2.png) [@diathesis](https://discuss.kotlinlang.org/u/diathesis)\
**Post date:** [September 22, 2019, 10:49pm UTC](https://discuss.kotlinlang.org/t/passing-map-with-non-null-keys-to-function-that-takes-map-with-nullable-keys/14347/7 "2019-09-22T22:49:11Z")

</div>

> The cast could then cause a null-pointer exception.

Sorry, where’s the potential NPE? The biggest potential problem to me is that doSomething() could insert a null into the map which the caller could then access, not expecting a potential null, but that’s always a bit of a risk with generics given type erasure.

```
>>> fun doSomething( mapping: Map<String?,Boolean> ) {
... println("mapping[null]: ${mapping[null]}")
... }
>>> doSomething(mapOf(null to false))
mapping[null]: false
>>> doSomething(mapOf("A" to true) as Map<String?,Boolean>)
mapping[null]: null

```

This seems … not terribad.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [September 23, 2019, 5:32am UTC](https://discuss.kotlinlang.org/t/passing-map-with-non-null-keys-to-function-that-takes-map-with-nullable-keys/14347/8 "2019-09-23T05:32:37Z")

</div>

The problem could be if the map is not backed by a java map but by a custom implementation in kotlin. In that case `mapping[null]` will fail with a NPE.

```auto
class MyMap<K: Any, V: Any>(val m: Map<K, V>): Map<K, V> by m {
   override operator fun get(key: K): V? = m[key] // kotlin will generate a null check for key, which would lead your code to fail
}

```

That said, as long as you are sure that you are only dealing with java standard library maps you should be fine.

---

<div class="post-metadata">

**Author:** ![diathesis](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/diathesis/32/4410_2.png) [@diathesis](https://discuss.kotlinlang.org/u/diathesis)\
**Post date:** [September 23, 2019, 2:22pm UTC](https://discuss.kotlinlang.org/t/passing-map-with-non-null-keys-to-function-that-takes-map-with-nullable-keys/14347/9 "2019-09-23T14:22:44Z")

</div>

Fair enough.

In this case it’s definitely being created by `mapOf()`, which in its current implementation seems to be a `LinkedHashMap`, although there’s no guarantee that can’t change in the future, so there’s still some risk there in theory.

---

<div class="post-metadata">

**Author:** ![temnur](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/temnur/32/7005_2.png) [@temnur](https://discuss.kotlinlang.org/u/temnur)\
**Post date:** [December 6, 2019, 2:32pm UTC](https://discuss.kotlinlang.org/t/passing-map-with-non-null-keys-to-function-that-takes-map-with-nullable-keys/14347/10 "2019-12-06T14:32:17Z")

</div>

For me it compiles, I had just seen it somewhere standard code:

```auto
fun doSomething(mapping: Map<out String?, Boolean>): Unit {}

val mapping = mapOf("One" to true, "Two" to false)
doSomething(mapping)

```

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [December 6, 2019, 4:06pm UTC](https://discuss.kotlinlang.org/t/passing-map-with-non-null-keys-to-function-that-takes-map-with-nullable-keys/14347/11 "2019-12-06T16:06:40Z")

</div>

You are using `Map<out String?, Boolean>` instead of `Map<String?, Boolean>`. But you might not always be in controll of the function.
