I need a function which will cast Map to Map<String, List<Int>>
Currently I use unsafe "as" but I receive a fair warning about it. I want to fix it.
For list part I was able to implement function
inline fun <reified T : Any> List<*>.checkItemsAre(): List<T> {
return this.filterIsInstance<T>().takeIf { it.size == this.size }
?: throw IllegalArgumentException("blablabla")
}
But I can't fund analog for map.
How can I fix it ?
If you are looking for something similar to the
checkItemsAreforLists that you showed, you can write such a method forMaps.Just like
checkItemsAre, this creates a newMapif all the keys are of typeKand all the values are of typeV.However, this wouldn't completely check nested generics like
Map<String, List<Int>>. If you want to handle that, you would have to add special cases forList, e.g.If you are only going to handle maps with values of type
List, I would just assume the map's values are lists: