Hi. I'm a Kotlin newbie and interested in hearing ...
# announcements
t
Hi. I'm a Kotlin newbie and interested in hearing is it possible to have an utility to deep-merge two data class instances of same type? Failed on inline + recursion...
c
I don't think you need reification here, so you can get rid of inline.
Copy code
fun <T : Any> T.merge(other: T): T {
  val nameToProperty = this::class.declaredMemberProperties.associateBy { it.name }
//...
I've posted full answer to SO
t
Big thanks!! Will answer to SO. There was a small thing with deeply nested data classes, but works like a charm.
👍 1
c
Yeah, indeed it was 🙂 Missed the bugger, fixed now:
return rightValue?.let { leftValue?.merge(it) } ?: rightValue ?: leftValue
t
Hi. Back in trouble. Wanted to add support for merging nested `Map`s. As the type information is lost, can’t figure out how to make it work. Here’s the problem statement: https://gist.github.com/ikitommi/c5e32bf1c3901b3ddf9f5fb6289c414e with a sample deepMerge, which doesn’t comple.
Jackson Kotlin module works, it uses
erasedType()
, but coudn’t figure out how to use that here. Anyway, here’s the code for it: https://github.com/FasterXML/jackson-module-kotlin/blob/master/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Types.kt
Any help would be appreciated.
c
Sorry, mate, no time. Try posting this as a separate question mentioning only merging maps.
t
ok, thanks!