Eugen Martynov
02/19/2023, 10:08 AM@Suppress("UNCHECKED_CAST")
fun MutableMap<Any, Any>.translate(translations: Map<Int, String>) {
forEach { (key, value) ->
when (value) {
is String -> put(key, translations[value.hashCode()]!!)
is MutableMap<*, *> -> (value as MutableMap<Any, Any>).translate(translations)
is MutableList<*> -> (value as MutableList<Any>).translate(translations)
else -> {
// nothing
}
}
}
}
@Suppress("UNCHECKED_CAST")
fun MutableList<Any>.translate(translations: Map<Int, String>) = forEachIndexed { index, value ->
when (value) {
is String -> set(index, translations[value.hashCode()]!!)
is MutableMap<*, *> -> (value as MutableMap<Any, Any>).translate(translations)
is MutableList<*> -> (value as MutableList<Any>).translate(translations) <-- here is error on cast: type checking has run into a recursive problem. Easiest workaround: specify types of your declarations explicitly
else -> {
// nothing
}
}
}