I’m playing with kotlin and trying to do something similar like in swift.
Basically having huge json parsed into map of maps of maps… and i’d like to do
myobject["key1"]["key2"]["key3"]
so i made simple operator function as extension function
private operator fun Any?.get(key: String): Any? = (this as? Map<*, *>).get(key)
problem is that the .get(key)
is calling recursively itself so ending in infinite loop,
I workaround it by defining another extension function out of the scope which works, but wondering if i can call something like super.get(key)
in case of inheritance to call concrete type implementation function directly (not again my extension function)
Following works, but of course not so nice
private fun Map<*,*>?._get(key: String) = (this as Map<*,*>)?.get(key)
class SomeClass(private val data: Map<String, Any>) {
private operator fun Any?.get(key: String): Any? = (this as? Map<*, *>)._get(key)
fun measureConfig(key: String): Map<String, *> {
return myobject["key1"]["key2"]["key3"]
}
}
Any ideas ?
Thank you