redrield
07/19/2017, 9:31 PMredrield
07/19/2017, 9:31 PMm.getOrPut(k) { mutableMapOf<String, Any>() } as? MutableMap<String, Any> ?: throw TotallyNotAClassCastExceptionException("This is fine")
raulraja
07/19/2017, 9:32 PMmutableMapOf
? xDraulraja
07/19/2017, 9:33 PMorangy
raulraja
07/19/2017, 9:33 PMraulraja
07/19/2017, 9:33 PMraulraja
07/19/2017, 9:36 PMAny
because the nesting of the path determines the level of nesting on the types.orangy
Any
is there to actually put values in the leaf mapRuckus
07/19/2017, 9:36 PMval list = listOf("a = 1", "a.b = 2")
fail?raulraja
07/19/2017, 9:36 PMorangy
jdiaz
07/19/2017, 9:41 PMorangy
listOf("a = 1", "a.b = 2")
?orangy
jdiaz
07/19/2017, 9:48 PMorangy
Ruckus
07/19/2017, 9:52 PMlistOf("a.b = 1", "a = 2")
. This wouldn't cause a crash, but it would overwrite data.orangy
alenkart
07/19/2017, 10:17 PMkevinmost
07/19/2017, 10:20 PMfun <T> foo(param: T) where T: Iface1, T: Iface2
raulraja
07/19/2017, 11:06 PMtailrec
and immutable maps. May be buggy but thought it illustrated his point and may give you more to think about.
tailrec fun loop(list: List<String>, acc: Pair<String, Any>): Map<String, Any> =
when {
list.isEmpty() -> mapOf(acc)
else -> {
val tail = list.drop(1)
loop(tail,
if (tail.isEmpty()) acc
else Pair(tail[0], mapOf(acc)))
}
}
fun main(args: Array<String>): Unit {
val s = "a.b.c.d = 20" //hacky cleanup, should be done with proper parsers
val exp = s.replace(" ", "").split("=")
val lhs = exp[0].split(".").reversed()
val rhs = exp[1]
val result = loop(lhs, Pair(lhs[0], rhs))
println(result) // {a={b={c={d=20}}}}
}
kirillrakhman
07/20/2017, 12:40 PMgalex
07/20/2017, 12:41 PManton.golovin
07/20/2017, 3:57 PMorangy
anton.golovin
07/20/2017, 3:59 PMnyxcode
07/20/2017, 5:16 PMnyxcode
07/20/2017, 5:19 PMgroostav
07/20/2017, 5:35 PMT::class
is a KClass<out T>
?